`
somefuture
  • 浏览: 1078570 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Spring Bean的prototype无用的解决

 
阅读更多

spring 的bean默认是单例,这个用spring的人基本都知道。

如果需要多个实例,又要使用ioc怎么办呢?

当然是使用@Scope注解,指明ConfigurableBeanFactory.SCOPE_PROTOTYPE了。

/**
	 * Scope identifier for the standard singleton scope: "singleton".
	 * Custom scopes can be added via {@code registerScope}.
	 * @see #registerScope
	 */
	String SCOPE_SINGLETON = "singleton";

	/**
	 * Scope identifier for the standard prototype scope: "prototype".
	 * Custom scopes can be added via {@code registerScope}.
	 * @see #registerScope
	 */
	String SCOPE_PROTOTYPE = "prototype";

 

但是在使用过程中 发现这样依然是单例。

难道spring的注解不稳定还是我使用有误?

 

首先推敲一下,为什么我们获取到的实例每次都一样。

spring出问题是不太可能了,这么常见的特性那么多人都用呢。

 

如果给一个组件加上

@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)

 每次请求它的实例,spring的确会给返回一个新的。

问题是这个多例对象是被其他单例服务依赖的。

而单例服务初始化的时候,多例对象就已经被创建好了。当你去使用单例服务的时候,多例对象也不会被再次创建了。

 

那怎么解决呢?

一个首先想到的方法当然是注入多个,就是写多个@Autowired。

这样的确可以。而且如果对单例服务写多个,他们也是一样的,只有对多例的才行。

 

但是我们的服务绝大多数时候都不知道需要多少多例服务,服务是动态创建的。

所以另一种方法就是使用getBean方法:

@Autowired
private ApplicationContext applicationContext;

applicationContext.getBean()

 只要每次调用getBean就可以获得一个新服务。

 

 其实spring的@Scope注解提供了在单例服务里使用多例对象的能力,它提供了一个代理字段

/**
	 * Specifies whether a component should be configured as a scoped proxy
	 * and if so, whether the proxy should be interface-based or subclass-based.
	 * <p>Defaults to {@link ScopedProxyMode#DEFAULT}, which typically indicates
	 * that no scoped proxy should be created unless a different default
	 * has been configured at the component-scan instruction level.
	 * <p>Analogous to {@code <aop:scoped-proxy/>} support in Spring XML.
	 * @see ScopedProxyMode
	 */
	ScopedProxyMode proxyMode() default ScopedProxyMode.DEFAULT;

 默认是不使用代理创建。我们只要把它改成使用代理即可:

@Component
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE, proxyMode = ScopedProxyMode.TARGET_CLASS)

 这样即使是在单例里面使用,每次获取多例对象也会拿到一个新的。

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics