2011-09-06 81 views
14

我有以下的Spring bean配置錯誤創建與java.io.File類型[不明確的構造函數參數類型]

<bean id="fileBean" class="java.io.File"> 
    <constructor-arg type="java.lang.String" 
        value="$prop{file.path.property}" />  
    </bean> 

我收到以下錯誤

org.springframework.beans.factory.UnsatisfiedDependencyException: 
Error creating bean with name 'fileBean' defined in class path resource [context.xml]: 
Unsatisfied dependency expressed through constructor argument with index 0 of type 
[java.net.URI]: Ambiguous constructor argument types - did you specify the correct 
bean references as constructor arguments? 

有bean時只有一個java.io.File的構造函數只有一個String參數,所以我不知道爲什麼這是不明確的。任何幫助讚賞。

回答

26

找到this link解釋發生了什麼。事實證明,如果沒有指定參數索引,spring將按類型匹配參數。在這種情況下,spring將使用我的單個String參數,並將其傳遞給java.io.File構造函數,該構造函數需要TWO字符串。這可以通過指定constructor-arg索引來解決。

<bean id="fileBean" class="java.io.File"> 
    <constructor-arg index="0" 
        type="java.lang.String" 
        value="$prop{file.path.property}" />  
</bean> 
4

只是我的兩分錢在這裏:我今天有完全相同的問題。我有一個單元測試來檢查Spring是否可以讀取我的XML配置並生成所有必需的bean。這是失敗的,因爲我正在編輯錯誤的XML文件。我正在編輯Ant構建的「dist」版本,而不是源代碼控制中的正確版本。

獲得的經驗:閱讀那些Spring異常消息(帶有XML文件路徑)非常密切

+0

同樣發生在我身上,異常竟然出現在我一直在編輯的另一個xml文件中 –

相關問題