我想调用两个 SOAP Web 服务并在我的 Spring REST 项目中获取数据。
我有两个 WSDL(VoucherService.wsdl 和 CGWebService.wsdl)文件,用于不同的两个(SOAP)Web 服务。
首先,我添加一个 WSDL(VoucherService.wsdl) 来使用“gradle wsdl2java”命令进行项目和生成类。
然后使用以下 Bean 更新 ModuleConfig 类。
@Bean
public Jaxb2Marshaller getVoucherServiceMarshaller() {
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
marshaller.setContextPath(environment.getProperty("voucher.service.marshaller.contextPath"));
rpr 返回编码器; }
@Bean
public WebServiceTemplate getVoucherServiceTemplate() {
WebServiceTemplate template = new WebServiceTemplate(getVoucherServiceMarshaller());
template.setDefaultUri(environment.getProperty("voucher.service.defaultUri"));
return template;
}
@Bean
public VoucherServiceProxy getVoucherServiceProxy() {
VoucherServiceProxy voucherServiceProxy = new VoucherServiceProxy();
return voucherServiceProxy;
}
然后创建 VoucherServiceProxy 类并添加这些 Autowiring 。
@Autowired
private WebServiceTemplate voucherServiceTemplate;
@Autowired
private Jaxb2Marshaller marshaller;
然后在 VoucherServiceProxy 类中创建所需的方法并进行部署,工作正常。
之后,我使用“gradle wsdl2java”命令为第二个 WSDL(CGWebService.wsdl)生成了类
然后在 ModuleConfig 类中创建以下 Bean。
@Bean
public ChargingGatewayServiceProxy getChargingGatewayServiceProxy() {
ChargingGatewayServiceProxy chargingGatewayServiceProxy = new ChargingGatewayServiceProxy();
return chargingGatewayServiceProxy;
}
@Bean
public Jaxb2Marshaller getChargingGatewayServiceMarshaller() {
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
marshaller.setContextPath(environment.getProperty("cg.service.marshaller.contextPath1"));
return marshaller;
}
@Bean
public WebServiceTemplate getChargingGatewayServiceTemplate() {
WebServiceTemplate template = new WebServiceTemplate(getChargingGatewayServiceMarshaller());
template.setDefaultUri(environment.getProperty("cg.service.url"));
return template;
}
然后创建 ChargingGatewayServiceProxy 并添加这些 Autowiring 。
@Autowired
private WebServiceTemplate cgServiceTemplate;
@Autowired
private Jaxb2Marshaller marshaller;
在 VoucherServiceProxy 类中我创建了必要的方法。
然后我尝试部署它,但出现此错误。
注入(inject) Autowiring 依赖项失败;嵌套异常是 org.springframework.beans.factory.BeanCreationException:无法 Autowiring 字段:私有(private) org.springframework.ws.client.core.WebServiceTemplate lk.ideahub.symphony.product.dapp.common.VoucherServiceProxy.voucherServiceTemplate;嵌套异常是 org.springframework.beans.factory.NoUniqueBeanDefinitionException:没有定义 [org.springframework.ws.client.core.WebServiceTemplate] 类型的合格 bean:期望单个匹配 bean,但找到 2:getVoucherServiceTemplate,getChargingGatewayServiceTemplate
上下文初始化失败 org.springframework.beans.factory.BeanCreationException:创建名称为“DAppSyncServiceImpl”的bean时出错: Autowiring 依赖项注入(inject)失败;嵌套异常是 org.springframework.beans.factory.BeanCreationException:无法 Autowiring 字段:私有(private) lk.ideahub.symphony.product.dapp.common.VoucherServiceProxy lk.ideahub.symphony.product.dapp.sync.service.DAppSyncServiceImpl.voucherServiceProxy;嵌套异常是 org.springframework.beans.factory.BeanCreationException:创建名称为“voucherServiceProxy”的 bean 时出错: Autowiring 依赖项注入(inject)失败;嵌套异常是 org.springframework.beans.factory.BeanCreationException:无法 Autowiring 字段:私有(private) org.springframework.ws.client.core.WebServiceTemplate lk.ideahub.symphony.product.dapp.common.VoucherServiceProxy.voucherServiceTemplate;嵌套异常是 org.springframework.beans.factory.NoUniqueBeanDefinitionException:没有定义 [org.springframework.ws.client.core.WebServiceTemplate] 类型的合格 bean:期望单个匹配 bean,但找到 2:getVoucherServiceTemplate,getChargingGatewayServiceTemplate
当我在 ModuleConfig 类中评论与一个服务代理相关的 bean 方法时,其他服务代理可以正常工作,不会出现错误。但无法同时部署两者。
有人可以帮我找到一种方法,在同一个项目中创建这两个服务代理类,而不会出现任何 bean 工厂错误。
最佳答案
向两个 WebServiceTemplate
添加一个 @Qualifier
,用于创建和使用,以便 Spring 可以区分它们并知道使用哪一个。
@Bean
@Qualifier("voucher")
public WebServiceTemplate getVoucherServiceTemplate() {...}
@Bean
@Qualifier("chargingGateway")
public WebServiceTemplate getChargingGatewayServiceTemplate() {...}
注入(inject),例如:
@Autowired
@Qualifier("chargingGateway")
public WebServiceTemplate chargingGatewayServiceTemplate;
关于java - org.springframework.beans.factory.BeanCreationException 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33992407/