创建类型为java.io.File的Bean时出错[不明确的构造函数参数类型] [英] Error when creating bean with type java.io.File [Ambiguous constructor argument types]

查看:277
本文介绍了创建类型为java.io.File的Bean时出错[不明确的构造函数参数类型]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下面的spring bean配置:

 < bean id =fileBeanclass =java.io.File > 
< constructor-arg type =java.lang.String
value =$ prop {file.path.property}/>
< / bean>

我收到以下错误:

<$在类路径资源[context.xml]中定义名称为'fileBean'的bean时出错:
表达式中的不满足依赖关系通过类型为
的索引0的构造函数参数:[java.net.URI]:模糊的构造函数参数类型 - 是否指定了正确的
bean引用作为构造函数参数?

java.io.File只有一个构造函数,只有一个String参数,所以我不是肯定为什么这是模棱两可的。任何帮助表示感谢。

解决方案

找到这个链接,解释发生了什么。事实证明,如果没有指定参数索引, spring将按类型匹配参数。在这种情况下,spring将使用我的单个String参数,并将其传递给使用 TWO 字符串的java.io.File构造函数。

 < bean id =fileBeanclass =java.io .File> 
< constructor-arg index =0
type =java.lang.String
value =$ prop {file.path.property}/>
< / bean>


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.

解决方案

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.

<bean id="fileBean" class="java.io.File">
  <constructor-arg index="0"
                   type="java.lang.String" 
                   value="$prop{file.path.property}" />    
</bean>

这篇关于创建类型为java.io.File的Bean时出错[不明确的构造函数参数类型]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆