马大头
2019-03-14 12:27SpringBoot使用@Bean配置某个类后,如果该类出错返回null,如何让其他@Resource注入该Bean的地方不出错?
使用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中做什么操作?
- 点赞
- 回答
- 收藏
- 复制链接分享
1条回答
为你推荐
- spring mvc 无法 注入 bean
- it技术
- 互联网问答
- IT行业问题
- 计算机技术
- 编程语言问答
- 0个回答
- 通过spring + junit进行单元测试,报配置文件找不到Failed to load ApplicationContext
- spring
- 2个回答
- Spring sessionFactory注入配置问题直接报容器错误,请大虾们看看
- spring
- 0个回答
- 在Spring中同时使用了Annotation配置和XML配置,spring优先使用哪一个
- spring
- 0个回答
- Spring 3.0+ Hibernate 3.3 泛型 注解管理Bean 出现问题 郁闷!
- spring
- 0个回答
换一换