2011-06-13 45 views
4

我有一個類註釋與春天Component例如:問題上的彈簧3自動掃描和需要註解

@Component 
public class ImportantClass{ 

@Autowired 
private DependentClassIF otherClass; 

//getters setters 

@Required 
public void setOtherClass(DependentClassIF c){ 
this.otherClass = c;  
} 

public interface DependentClassIF { 
    //methods here 
} 
@Component 
public class DependentClass implements DependentClassIF { 
    //implementation here 
} 

我用自動掃描檢測豆,而不是宣佈他們都在bean的conf文件。
我得到

org.springframework.beans.factory.BeanInitializationException: Property 'otherClass' is required for bean 'ImportantClass'

我的問題是: 在自動掃描,並放不是春確保所有需要屬性注入?
如果我刪除了@Required它可行,但我不確定我是否理解Spring的行爲。

歡迎任何輸入。

感謝

回答

3

@Autowiredrequired設置爲true,所以你不需要@Required

你不需要@Requried與基於註釋的注入。它意味着與xml配置一起使用,以表示必須在xml中設置一個屬性。

+0

你的意思是2個註釋在同一個地方嗎?如果我在'private DependentClassIF otherClass上執行'@Autowired @ Required';'我得到錯誤:'註釋@Required不允許用於這個位置' – Cratylus 2011-06-13 10:25:27

+0

@ user384706看到更新 – Bozho 2011-06-13 10:37:12