2011-11-10 28 views
3

在使用依賴注入創建Spring bean之後,然後在該Bean上運行某個方法的情況下,出現了最奇怪的Spring問題,在bean期間設置的實例變量方法調用都返回到它們的默認Java值。在我們從Spring 2.5.5遷移到Spring 3.0.5之後發現了這種情況。Spring 3.0.5原型Bean實例變量在方法調用後變爲空

所以爲了清晰起見,這裏的例子

bean原型:

@Component("bean1") 
@Scope("prototype") 
public class Bean1{ 
    String someString;//There are other instance variables but same happens to them 

    @Autowired 
    @Qualifier("otherBean") 
    private OtherBean otherBean; 

    public void doSomething(){ 
     someString="1234ABC"; 
    } 
    //setters and getters .... 
} 

而且從春天抓起豆和使用它的代碼:

Bean1 bean1 = (Bean1) applicationContext.getBean("bean1"); 
bean1.doSomething();//updates some instance variables in bean1 
String value = bean1.getSomeString(); //Instance variables is null 
Object otherObject = bean1.getOtherBean(); //This Spring injected bean is correctly initialized 

所以,如果我調試到代碼中,實例變量(someString)在bean中設置,而在doSomething方法調用中,但在我返回後,該值回到null。

最糟糕的是,這一切如預期在2.5.5但不是在更新的春天3.0.5

這是遺留代碼的作品,所以我知道,你應該代碼,接口等等Bean1應做一個接口,實現接口的類應該做實際的工作。我將代碼更改爲此模型,但仍然無效。

回答

0

嘗試@Scope(值= 「原型」,proxyMode = ScopedProxyMode.TARGET_CLASS)

0

我面臨着同樣的問題,但未能得到了確切的解決方案。然後我只是改變了要求的範圍。

相關問題