TL;DR:您必须将“marshaller()”方法的名称与客户端配置中的参数变量名称相匹配。
覆盖 bean 定义
发生的情况是,您的两个 @Configuration 类都对 Jaxb2Marshaller 使用相同的 bean 名称,即(使用 spring 示例):
@Bean
public Jaxb2Marshaller marshaller() { //<-- that name
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
marshaller.setContextPath("hello.wsdl");
return marshaller;
}
“marshaller()”方法名称是下面将在您的客户端中注入的 bean 名称:
@Bean
public QuoteClient quoteClient(Jaxb2Marshaller marshaller) { //<-- used here
QuoteClient client = new QuoteClient();
client.setDefaultUri("http://www.webservicex.com/stockquote.asmx");
client.setMarshaller(marshaller);
client.setUnmarshaller(marshaller);
return client;
}
如果您对第二个客户端使用“marshaller()”,Spring 会覆盖该 bean 定义。您可以在日志文件中发现这一点,查找类似以下内容:
INFO 7 --- [main] o.s.b.f.s.DefaultListableBeanFactory : Overriding bean definition for bean 'marshaller'
解决方案
因此,要创建更多拥有自己的编组器而不发生冲突的客户端,您需要有这样的 @Configuration:
@Configuration
public class ClientBConfiguration {
@Bean
public Jaxb2Marshaller marshallerClientB() {
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
marshaller.setContextPath("hello.wsdl");
return marshaller;
}
@Bean
public ClientB clientB(Jaxb2Marshaller marshallerClientB) {
ClientB client = new ClientB();
client.setDefaultUri("http://www.webservicex.com/stockquote.asmx");
client.setMarshaller(marshallerClientB);
client.setUnmarshaller(marshallerClientB);
return client;
}
}