Spring Javaconfig的旧文档说我可以使用
@Bean(scope=DefaultScopes.PROTOTYPE)
来获取一个原型bean,但是spring3.0.5的@Bean
似乎没有这个属性。
在Javaconfig中有任何方法来控制bean的作用域吗?
使用@Scope
代替。
此外,DefaultScopes
在Spring core中不可用,但为了方便,您可以使用BeanDefinition.SCOPE_PROTOTYPE
和BeanDefinition.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注释