我可以用Spring Javaconfig创建一个原型作用域bean吗?



Spring Javaconfig的旧文档说我可以使用

@Bean(scope=DefaultScopes.PROTOTYPE)

来获取一个原型bean,但是spring3.0.5的@Bean似乎没有这个属性。

在Javaconfig中有任何方法来控制bean的作用域吗?

使用@Scope代替。

此外,DefaultScopes在Spring core中不可用,但为了方便,您可以使用BeanDefinition.SCOPE_PROTOTYPEBeanDefinition.SCOPE_SINGLETON

您可以添加@Scope("prototype"),例如:

@Bean
@Scope("prototype")
public DemoDao getDao() {
    DemoDao dao = new DemoDao();
    dao.setAddress("annoted:address");
    dao.setName("annoted:name");
    return dao;
}

使用以下Java配置,

@Bean
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public SpringBean springBean(){
    SpringBean bean = new SpringBean();
    return bean;
}

或简单,

@Scope(value = "prototype")

引用@Scope注释

最新更新