3

我正在尝试创建这个bean:

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

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

我得到:

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 解决我希望它使用的正确方法所需的一切。

4

3 回答 3

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!
于 2013-05-10T09:55:45.507 回答
1

我相信问题是这种方法valueOf(char[] data)

您提到参数类型为对象。

并且 char[] 也是一个对象。

于 2013-05-10T09:49:03.770 回答
1

我对像这样的bean有同样的问题:

<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 并在那里添加了一个额外的参数之前,一切都为我工作

于 2014-11-20T16:10:15.343 回答