单="假" Spring 4.3.8不再支持。需要解决方案

时间:2018-04-17 13:38:44

标签: spring spring-bean

我们正在从Spring 3.2.4升级到Spring 4.3.8,其中singleton =" false"不再受支持。什么是设置单身'假'在Spring 4.3.8? 如果singeton =" false"那么这是否意味着spring bean范围已成为" Prototype"?

3 个答案:

答案 0 :(得分:2)

您可以使用@Scope指定原型bean。

例如:

@Bean @Scope("prototype")
public Person personPrototype() {
    return new Person();
}

进一步阅读link

答案 1 :(得分:0)

据我记忆,singleton=false是出于某些兼容性原因而保留的,在一些较旧的docs, eg 3.0.0.M3中也有说明:

  

<bean id="accountService" class="com.foo.DefaultAccountService"/>

     

以下是等效的,尽管是冗余的(单例范围是默认值);使用spring-beans-2.0.dtd
  <bean id="accountService"> class="com.foo.DefaultAccountService" scope="singleton"/>

     

以下内容是等效的,并保留为spring-beans.dtd 中的向后兼容性   <bean id="accountService"> class="com.foo.DefaultAccountService" singleton="true"/>

无论如何,默认的弹簧范围是singleton,所以即使未指定,但是can be changed to prototype (or whatever you need) with

  • XML:scope="prototype"
  • Java DSL:@Scope("prototype")@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)

答案 2 :(得分:0)

正确的,scope =“ prototype”与singleton =“ false”的直接等效。

  • 单例=“ false”的等效项是scope =“ prototype”。
  • 另一种选择是使用注释@Scope(“ prototype”)
  • 等同于singleton =“ true”将在spring config中消除此属性,因为scope =“ singleton”是默认设置。