我正在尝试使用类org.springframework.ws.client.core.WebServiceTemplate
制作两个测试用例。两个测试用例都属于不同的类,所以我为它们制作了两个不同的 bean。
在运行 junit 测试时,我遇到了这样的错误
Error creating bean with name 'testcases.TestAdminMethodsWebService': Unsatisfied dependency expressed through bean property 'admin': : No unique bean of type [org.springframework.ws.client.core.WebServiceTemplate] is defined: expected single matching bean but found 7: [admin, rules]; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [org.springframework.ws.client.core.WebServiceTemplate] is defined: expected single matching bean but found 2: [admin, rules]
我的bean是这样的:
<oxm:jaxb2-marshaller id="marshaller_admin" contextPath="a.com.b" />
<bean id="admin" class="org.springframework.ws.client.core.WebServiceTemplate">
<property name="marshaller" ref="marshaller_admin" />
<property name="unmarshaller" ref="marshaller_admin" />
<property name="defaultUri"
value="http://dev05:8080/.." />
</bean>
<oxm:jaxb2-marshaller id="marshaller_rules" contextPath="r.com.b" />
<bean id="rules" class="org.springframework.ws.client.core.WebServiceTemplate">
<property name="marshaller" ref="marshaller_rules" />
<property name="unmarshaller" ref="marshaller_rules" />
<property name="defaultUri"
value="http://dev05:8080/.." />
</bean>
请告诉我如何克服这个问题或者为什么会发生这个错误,任何帮助将不胜感激,谢谢。
最佳答案
使用@Qualifier
注解来帮助Spring确定应该注入(inject)哪个bean。
public class TestClass {
@Autowired
@Qualifier("admin")
WebServiceTemplate admin;
@Autowired
@Qualifier("rules")
WebServiceTemplate rules;
// ... Rest of your class
}
阅读文档 here在使用限定符微调基于注释的 Autowiring 部分下。
更新:
您还需要像这样更改 xml bean 定义:
<oxm:jaxb2-marshaller id="marshaller_admin" contextPath="a.com.b" />
<bean class="org.springframework.ws.client.core.WebServiceTemplate">
<qualifier value="admin"/>
<property name="marshaller" ref="marshaller_admin" />
<property name="unmarshaller" ref="marshaller_admin" />
<property name="defaultUri"
value="http://dev05:8080/.." />
</bean>
<oxm:jaxb2-marshaller id="marshaller_rules" contextPath="r.com.b" />
<bean class="org.springframework.ws.client.core.WebServiceTemplate">
<qualifier value="rules"/>
<property name="marshaller" ref="marshaller_rules" />
<property name="unmarshaller" ref="marshaller_rules" />
<property name="defaultUri"
value="http://dev05:8080/.." />
</bean>
请注意包含 <qualifier>
每个 bean 定义下的标记。
关于junit - 未定义 [org.springframework.ws.client.core.WebServiceTemplate] 类型的唯一 bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10797566/