2011-09-06 162 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文件中 –

相关问题