Why does Spring report a factory method arg as ambiguous?

12,319

Solution 1

Simply remove name attribute, XSD docs for name says:

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!

Solution 2

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

You have mentioned argument type as Object.

And char[] is also an Object.

Solution 3

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

Share:
12,319
Simeon
Author by

Simeon

Seasoned developer lead that loves distributed systems, Linux, static typed languages and drawing squares connected by arrows on a board. Generally biased towards the Java ecosystem, Spring and AWS.

Updated on June 11, 2022

Comments

  • Simeon
    Simeon about 2 years

    I'm trying to create this 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.

    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.

  • Simeon
    Simeon about 11 years
    It worked, thanks a lot :) I never suspected that giving it too much info could be a problem.