cyb*_*onk 14 java spring file ambiguous
我有以下spring bean配置
<bean id="fileBean" class="java.io.File">
<constructor-arg type="java.lang.String"
value="$prop{file.path.property}" />
</bean>
Run Code Online (Sandbox Code Playgroud)
我收到以下错误
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?
Run Code Online (Sandbox Code Playgroud)
java.io.File只有一个构造函数,只有一个String参数,所以我不确定为什么这是不明确的.任何帮助赞赏.
cyb*_*onk 26
找到这个链接,解释发生了什么.事实证明,如果没有指定参数索引,spring将按类型匹配参数.在这种情况下,spring接受我的单个String参数并将其传递给带有两个字符串的java.io.File构造函数.这可以通过指定constructor-arg索引来修复.
<bean id="fileBean" class="java.io.File">
<constructor-arg index="0"
type="java.lang.String"
value="$prop{file.path.property}" />
</bean>
Run Code Online (Sandbox Code Playgroud)
这里只有我的两分钱:我今天遇到了完全相同的问题。我有一个单元测试来检查 Spring 是否可以读取我的 XML 配置并生成所有必需的 bean。它失败了,因为我正在编辑错误的 XML 文件。我正在编辑来自 Ant 构建的“dist”版本,而不是来自源代码管理的正确版本。
经验教训:非常仔细地阅读那些 Spring 异常消息(带有 XML 文件路径)!