为什么Spring报告工厂方法arg不明确?

时间:2013-05-10 09:39:18

标签: java spring factory

我正在尝试创建这个bean:

<bean id="myBean" class="java.lang.String" factory-method="valueOf">
    <constructor-arg name="obj" value="a string" type="java.lang.Object"/>
</bean>

我希望Spring在创建bean时使用此方法java.lang.String#valueOf(Object obj)

我明白了:

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'myBean' defined in class path resource [META-INF/test-contexts/testManualDependencyBundleResolverContext.xml]: Unsatisfied dependency expressed through constructor argument with index 0 of type [char[]]: Ambiguous factory method argument types - did you specify the correct bean references as factory method arguments?
Related cause: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'myBean' defined in class path resource [META-INF/test-contexts/testManualDependencyBundleResolverContext.xml]: Unsatisfied dependency expressed through constructor argument with index 0 of type [char[]]: Ambiguous factory method argument types - did you specify the correct bean references as factory method arguments?
Related cause: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'myBean' defined in class path resource [META-INF/test-contexts/testManualDependencyBundleResolverContext.xml]: Unsatisfied dependency expressed through constructor argument with index 0 of type [boolean]: Ambiguous factory method argument types - did you specify the correct bean references as factory method arguments?
Related cause: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'myBean' defined in class path resource [META-INF/test-contexts/testManualDependencyBundleResolverContext.xml]: Unsatisfied dependency expressed through constructor argument with index 0 of type [long]: Ambiguous factory method argument types - did you specify the correct bean references as factory method arguments?
Related cause: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'myBean' defined in class path resource [META-INF/test-contexts/testManualDependencyBundleResolverContext.xml]: Unsatisfied dependency expressed through constructor argument with index 0 of type [char]: Ambiguous factory method argument types - did you specify the correct bean references as factory method arguments?
Related cause: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'myBean' defined in class path resource [META-INF/test-contexts/testManualDependencyBundleResolverContext.xml]: Unsatisfied dependency expressed through constructor argument with index 0 of type [double]: Ambiguous factory method argument types - did you specify the correct bean references as factory method arguments?
Related cause: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'myBean' defined in class path resource [META-INF/test-contexts/testManualDependencyBundleResolverContext.xml]: Unsatisfied dependency expressed through constructor argument with index 0 of type [float]: Ambiguous factory method argument types - did you specify the correct bean references as factory method arguments?
Related cause: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'myBean' defined in class path resource [META-INF/test-contexts/testManualDependencyBundleResolverContext.xml]: Unsatisfied dependency expressed through constructor argument with index 0 of type [int]: Ambiguous factory method argument types - did you specify the correct bean references as factory method arguments?
Related cause: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'myBean' defined in class path resource [META-INF/test-contexts/testManualDependencyBundleResolverContext.xml]: Unsatisfied dependency expressed through constructor argument with index 0 of type [java.lang.Object]: Ambiguous factory method argument types - did you specify the correct bean references as factory method arguments?

为什么呢?我想我已经指定了Spring所需的一切来解决我希望它使用的正确方法。

3 个答案:

答案 0 :(得分:18)

只需删除name属性,名称的XSD文档即:

Only needed to avoid ambiguities, e.g. in case of 2 arguments of 
 the exact same type. Note: This requires debug symbols to be 
 stored in the class file in order to introspect argument names!

答案 1 :(得分:1)

我认为问题在于此方法valueOf(char[] data)

您已将参数类型称为Object。

char []也是一个Object。

答案 2 :(得分:1)

我遇到了类似问题的问题:

<bean id="myBean" class="..MyBean">
    <constructor-arg ref="bean1/>
    <constructor-arg ref="bean2/>
    <constructor-arg>
        <bean class="MyEnum" factory-method="valueOf">
            <constructor-arg value="${configString}"/>
        </bean>
    </constructor-arg>
    <constructor-arg ref="bean3/>
</bean>

要解决问题,我需要删除所有名称属性,例如:

<constructor-arg name="arg1" ref="bean1/>

在我转移到java8并在那里添加了一个额外的参数之前,所有这些都为我工作了