ServiceLocatorFactoryBean spring scope prototype

来源:互联网 发布:ubuntu mate下载15.10 编辑:程序博客网 时间:2024/05/29 08:24

用法很简单如下:

@Bean    public FactoryBean serviceLocatorFactoryBean() {        ServiceLocatorFactoryBean factoryBean = new ServiceLocatorFactoryBean();        factoryBean.setServiceLocatorInterface(WorkerHolderFactory.class);        return factoryBean;    }

public interface WorkerHolderFactory {    WorkerHolder getHolder(String type);用type做beanname找,或者 WorkerHolder getHolder();用返回值类型找}

然后在用到的地注入factorybean就OK了

啥原理呢?serviceLocatorFactoryBean源码是酱婶说的:

private Object invokeServiceLocatorMethod(Method method, Object[] args) throws Exception {            Class serviceLocatorMethodReturnType = this.getServiceLocatorMethodReturnType(method);            try {                String beanName = this.tryGetBeanName(args);                return StringUtils.hasLength(beanName)?ServiceLocatorFactoryBean.this.beanFactory.getBean(beanName, serviceLocatorMethodReturnType):ServiceLocatorFactoryBean.this.beanFactory.getBean(serviceLocatorMethodReturnType);            } catch (BeansException var5) {                if(ServiceLocatorFactoryBean.this.serviceLocatorExceptionConstructor != null) {                    throw ServiceLocatorFactoryBean.this.createServiceLocatorException(ServiceLocatorFactoryBean.this.serviceLocatorExceptionConstructor, var5);                } else {                    throw var5;                }            }        }        private String tryGetBeanName(Object[] args) {            String beanName = "";            if(args != null && args.length == 1 && args[0] != null) {                beanName = args[0].toString();            }            if(ServiceLocatorFactoryBean.this.serviceMappings != null) {                String mappedName = ServiceLocatorFactoryBean.this.serviceMappings.getProperty(beanName);                if(mappedName != null) {                    beanName = mappedName;                }            }            return beanName;        }

代码解释的挺清除我就不多絮叨了。
(踩坑了个@Scope的坑,查了一下)

@Scope的proxyMode不能乱用

这个东西什么意思呢?官方是这么说的:

https://docs.spring.io/spring/docs/5.0.0.RC3/spring-framework-reference/core.html#beans-factory-scopes-other-injection点击打开链接

具体说啥呢?其中有营养的一句话是:

When declaring <aop:scoped-proxy/> against a bean of scope prototype, every method call on the shared proxy will lead to the creation of a new target instance which the call is then being forwarded to.


~~~

prototype模式下,每调用一次方法都会重建一个bean给我。。。

搞毛线啊。。。


这是其中一种方法:其他方法参考:https://docs.spring.io/spring/docs/5.0.0.RC3/spring-framework-reference/core.html#beans-factory-method-injection点击打开链接

参考:

https://docs.spring.io/spring/docs/5.0.0.RC3/spring-framework-reference/core.html#beans-factory-scopes-prototype点击打开链接

http://www.bubuko.com/infodetail-1344861.html点击打开链接

http://renxiangzyq.iteye.com/blog/2274291点击打开链接


https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/beans/factory/config/ServiceLocatorFactoryBean.html点击打开链接

http://renxiangzyq.iteye.com/blog/2274291点击打开链接