2011-11-14 85 views

回答

21

Use @Scope instead

另外,DefaultScopes不適用於Spring核心,但爲方便起見,您可以使用BeanDefinition.SCOPE_PROTOTYPEBeanDefinition.SCOPE_SINGLETON

+0

謝謝,谷歌出乎意料地難以爲之! –

+1

它現在'ConfigurableBeanFactory.SCOPE_PROTOTYPE' – sinu

2

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

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

這與接受的答案有何不同? –

+1

你應該解釋爲什麼OP應該這樣做。此外,如果您評論更多代碼,爲什麼不把它放入答案中,以便人們立即看到它? :) – LinusGeffarth

+1

就像我剛剛做的;) –

0

使用面向Java的配置如下,

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

或者乾脆,

@Scope(value = "prototype") 

參考@Scope註釋

相關問題