2013-05-10 56 views
3

我試圖創建這個bean:爲什麼Spring報告工廠方法arg模糊?

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

我想春天創建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所需的一切,以解決我希望它使用的正確方法。

+1

你應該考慮使用'MessageSource'作爲'String' bean。 – Mateusz 2013-05-10 09:50:20

回答

17

只需刪除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

它的工作,非常感謝:)我從來沒有懷疑,給它太多的信息可能是一個問題。 – Simeon 2013-05-10 09:59:46

1

我相信問題是這種方法valueOf(char[] data)

你提到的參數類型爲對象。

而char []也是一個Object。

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並添加之前,所有工作都是爲我工作的編輯一個額外的參數

相關問題