【问题标题】:Error when creating bean with type java.io.File [Ambiguous constructor argument types]创建 java.io.File 类型的 bean 时出错 [不明确的构造函数参数类型]
【发布时间】:2011-11-11 17:43:28
【问题描述】:

我有以下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 只有一个带有单个字符串参数的构造函数,所以我不确定为什么这是模棱两可的。任何帮助表示赞赏。

【问题讨论】:

    标签: java spring file ambiguous


    【解决方案1】:

    找到this link,它解释了正在发生的事情。事实证明,如果没有指定参数索引,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>
    

    【讨论】:

      【解决方案2】:

      只是我的两分钱:我今天遇到了完全相同的问题。我有一个单元测试来检查 Spring 是否可以读取我的 XML 配置并生成所有必要的 bean。它失败了,因为我编辑了错误的 XML 文件。我正在从 Ant 构建中编辑“dist”版本,而不是从源代码管理中编辑正确的版本。

      经验教训:阅读那些 Spring 异常消息(带有 XML 文件路径)非常接近

      【讨论】:

      • 同样的事情发生在我身上,异常出现在我一直在编辑的另一个 xml 文件中
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-14
      • 1970-01-01
      • 2010-12-05
      相关资源
      最近更新 更多