使用CXF调用远程服务,因为cxf创建连接方法速度比较慢,所以使用@Bean在CxfConfig类中进行配置:
@Component
@Slf4j
public class CxfConfig {
@Value("${wsdl-url}")
private String wsdlUrl;
@Bean
public JaxWsDynamicClientFactory jaxWsDynamicClientFactory() {
return JaxWsDynamicClientFactory.newInstance();
}
@Bean(autowire = Autowire.BY_NAME, value = "myClient")
public Client myClient() {
try {
return this.jaxWsDynamicClientFactory().createClient(wsdlUrl);
} catch (Exception e) {
log.error("【WebService连接异常】: " + e);
}
return null;
}
}
然后在其他类中使用@Resource进行注入
@Resource(name = "myClient")
private Client client;
现在发现一个问题,当WebService服务正常运行时不会有什么问题,但是当WebService服务端不可用时CxfConfig调用createClient时返回了一个null,然后项目启动时就会出现org.springframework.beans.factory.BeanNotOfRequiredTypeException:
Bean named 'myClient' is expected to be of type
'org.apache.cxf.endpoint.Client' but was actually of type 'org.springframework.beans.factory.support.NullBean' 错误,连项目都启动不起来。
请问有什么解决办法吗?比如允许@Bean注入空值 或者在config中做什么操作?