创建类型为 java.io.File 的 bean 时出错 [构造函数参数类型不明确]
我有以下 spring bean 配置,
<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?
There is only one constructor for java.io.File with a single Stringparameter 所以我不确定为什么这是不明确的。任何帮助表示赞赏。
I have the following spring bean configuration
<bean id="fileBean" class="java.io.File">
<constructor-arg type="java.lang.String"
value="$prop{file.path.property}" />
</bean>
I'm getting the following error
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?
There is only one constructor for java.io.File with a single String parameter so I'm not sure why this is ambiguous. Any help appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只是我的两分钱:我今天遇到了完全相同的问题。我有一个单元测试来检查 Spring 是否可以读取我的 XML 配置并生成所有必需的 bean。它失败了,因为我编辑了错误的 XML 文件。我正在编辑 Ant 构建中的“dist”版本,而不是源代码管理中的正确版本。
经验教训:非常仔细阅读那些 Spring 异常消息(带有 XML 文件路径)!
Just my two cents here: I had the exact same problem today. I have a unit test to check if Spring can read my XML config and generate all necessary beans. It was failing because I was editing the wrong XML file. I was editing a "dist" version from an Ant build, instead of the correct version from source control.
Lesson learned: Read those Spring exception messages (with XML file paths) very closely!
找到此链接解释了正在发生的情况。事实证明,如果没有指定参数索引,spring 将按类型匹配参数。在这种情况下,spring 接受我的单个 String 参数并将其传递给接受 TWO 字符串的 java.io.File 构造函数。这可以通过指定构造函数参数索引来修复。
Found this link that explains what is happening. It turns out that spring will match arguments by type if there is no argument index specified. In this case spring takes my single String argument and passes it to java.io.File constructor that takes TWO strings. This can be fixed by specifying the constructor-arg index.