创建类型为 java.io.File 的 bean 时出错 [构造函数参数类型不明确]

2023-12-22

我有以下 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?

java.io.File 只有一个带有单个 String 参数的构造函数,因此我不确定为什么这是不明确的。任何帮助表示赞赏。


Found 这个链接 https://jira.springsource.org/browse/SPR-263这解释了正在发生的事情。事实证明如果没有指定参数索引,spring将按类型匹配参数。在这种情况下,spring 接受我的单个 String 参数并将其传递给 java.io.File 构造函数,该构造函数接受TWO字符串。这可以通过指定构造函数参数索引来修复。

<bean id="fileBean" class="java.io.File">
  <constructor-arg index="0"
                   type="java.lang.String" 
                   value="$prop{file.path.property}" />    
</bean>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

创建类型为 java.io.File 的 bean 时出错 [构造函数参数类型不明确] 的相关文章

随机推荐