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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16479321/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 23:05:06  来源:igfitidea点击:

Why does Spring report a factory method arg as ambiguous?

javaspringfactory

提问by Simeon

I'm trying to create this bean:

我正在尝试创建这个 bean:

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

I want Spring to use this method java.lang.String#valueOf(Object obj)when creating the bean.

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

I get:

我得到:

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?

Why? I think I've specified everything needed for Spring to resolve the correct method I want it to use.

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

回答by Evgeniy Dorofeev

Simply remove nameattribute, XSD docs for name says:

只需删除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!

回答by Thihara

I believe the problem is this method valueOf(char[] data)

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

You have mentioned argument type as Object.

您已经将参数类型称为 Object。

And char[] is also an Object.

而 char[] 也是一个对象。

回答by Geniy

I had the same problem with a bean like:

我对豆子也有同样的问题:

<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>

To resolve a problem I needed to remove all name attributes like:

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

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

All was worked for me before I moved to java8 and added one additional argument there

在我转向 java8 并在那里添加一个额外的参数之前,一切都对我有用