Spring 使用@Async出现循环依赖Bean报错的解决方案
来源:互联网 发布:java中char 编辑:程序博客网 时间:2024/06/15 21:12
初现端倪
早前打算将项目阿里云上的Linux服务器上时,突然出现了以下错误:
Caused by:org.springframework.beans.factory.BeanCurrentlyInCreationException: Errorcreating bean with name 'asyncUpdate': Bean with name 'asyncUpdate' has beeninjected into other beans [dealerService,userService] in its raw version aspart of a circular reference, but has eventually been wrapped. This means thatsaid other beans do not use the final version of the bean. This is often theresult of over-eager type matching - consider using 'getBeanNamesOfType' withthe 'allowEagerInit' flag turned off, for example.
以上这一长串大概的意思就是说,'asyncUpdate' bean已经注入了一个版本,程序中出现了循环依赖的可能性。
就如:在创建A类时,构造器需要B类,那将去创建B,在创建B类时又发现需要C类,则又去创建C,最终在创建C时发现又需要A,从而形成一个环,没办法创建。
分析问题
首先来看看这里的AsyncUpdate类是用来做什么的:
@Component@EnableAsyncpublic classAsyncUpdate { @Autowired private DealerMemberMapper dealerMemberMapper; @Autowired private UserService userService; @Autowired private GoodsSubscribeService goodsSubscribeService; /** *异步更新 */ @Async public void updateDealerAsync(Dealer dealer, String mobilephone, Integer type) throwsException { //篇幅有限就不贴代码 }}
@Async注解:表示当前是一个异步方法。
@Component:表示把普通pojo实例化到spring容器中,相当于配置文件中的<bean id="" class=""/>。
@EnableAsync注解:表示开启异步线程。
@Servicepublic classDealerService { @Autowired private DealerMapper dealerMapper; @Autowired private UserService userService; @Autowired private AsyncUpdate asyncUpdate; //一些代码}
通过以上两段程序能够得到,问题出现的原因:简单的讲就是在启动程序的时候,Spring已经对DealerService里的asyncUpdate、userService加载完成,当准备创建AsyncUpdate类的时候发现使用了@Async注解,即spring又需将该Bean代理一次,然后Spring发现该Bean已经被其他对象注入,这里就是问题的关键所在了。
即:在创建A类时,构造器需要B类,那将去创建B,在创建B类时又发现需要C类,则又去创建C,最终在创建C时发现又需要A,从而形成一个环,没办法创建。
解决方案
大量查找解决方案后发现解决方案有三:
1.使用 @Lazy 或者 @ComponentScan(lazyInit = true) 来解决该问题,经过我实验,@Lazy必须和@Autowired联合使用才能生效,单纯在类上添加 @Lazy 并无意义。
@Autowired@Lazyprivate AsyncUpdate asyncUpdate;
2. 使用基于 Setter 的注入
@Componentpublic classDealerService { private AsyncUpdate a; public void setA(AsyncUpdate a) { this.a = a; }} @Componentpublic classAsyncUpdate { private DealerService ds; public void setDs(DealerService ds) { this.ds = ds; }}
3.使用@Autowired注解防止循环注入,如:
@Componentpublic classDealerService { @Autowired private AsyncUpdate a;} @Componentpublic classAsyncUpdate { private DealerService ds; @Autowired public void foo(DealerService ds) { this.ds= ds; }}
注:当@Autowired 对方法或构造函数进行标注时,表示如果构造函数有两个入参,分别是 bean1 和bean2,@Autowired 将分别寻找和它们类型匹配的 Bean,将它们作为 baanService (Bean1 bean1 ,Bean2 bean2) 的入参来创建baanService Bean。
ps:该文章仅是用于个人日常笔记记录,如有错误之处还望各位大佬海量汪涵,指出错误共同进步。
- Spring 使用@Async出现循环依赖Bean报错的解决方案
- Spring Bean 循环依赖解决方案
- Spring Bean 循环依赖解决方案
- 使用require出现循环依赖模块报错的问题
- spring使用手动装载依赖的bean
- Spring注入bean报错:Error creating bean with name的网上找不到的解决方案
- 从spring源码角度分析循环依赖bean的组装
- Spring-bean的循环依赖以及解决方式
- springboot的@Async循环依赖问题
- Spring源码(六)-Spring循环依赖的解决方案
- web.xml配置文件中<async-supported>true</async-supported>报错的解决方案
- web.xml配置文件中<async-supported>true</async-supported>报错的解决方案
- web.xml配置文件中<async-supported>true</async-supported>报错的解决方案
- web.xml配置文件中<async-supported>true</async-supported>报错的解决方案
- web.xml配置文件中<async-supported>true</async-supported>报错的解决方案
- web.xml配置文件中<async-supported>true</async-supported>报错的解决方案
- spring 源码探索--单例bean解决循环依赖问题
- Spring源码学习--Bean对象循环依赖问题解决(四)
- es6箭头函数
- sublime text3搭建python调试环境
- django1.10 翻译文档
- oracle数据库(表、视图、索引、事务)
- python 用本地git来保存文件或者博客,实现版本控制
- Spring 使用@Async出现循环依赖Bean报错的解决方案
- 工具类-软键盘
- 将表达式转换为逆波兰表达式-LintCode
- MATLAB函数之imfinfo
- 不错的vim python配置
- 搜索练习 2002t2字串变换
- [bzoj1103][树链剖分]大都市
- windows下vue.js的安装与配置
- 1)Java语法基础知识点(可以借鉴)