Spring 使用@Async出现循环依赖Bean报错的解决方案
初现端倪
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
@EnableAsync
public 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容器中,相当于配置文件中的。
@EnableAsync注解:表示开启异步线程。
@Service
public 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
@Lazy
private AsyncUpdate asyncUpdate;
- 使用基于 Setter 的注入
@Component
public classDealerService {
private AsyncUpdate a;
public void setA(AsyncUpdate a) {
this.a = a;
}
}
@Component
public classAsyncUpdate {
private DealerService ds;
public void setDs(DealerService ds) {
this.ds = ds;
}
}
3.使用@Autowired注解防止循环注入,如:
@Component
public classDealerService {
@Autowired
private AsyncUpdate a;
}
@Component
public 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。
Spring 使用@Async出现循环依赖Bean报错的解决方案的更多相关文章
- spring: 我是如何解决循环依赖的?
1.由同事抛的一个问题开始 最近项目组的一个同事遇到了一个问题,问我的意见,一下子引起的我的兴趣,因为这个问题我也是第一次遇到.平时自认为对spring循环依赖问题还是比较了解的,直到遇到这个和后面的 ...
- Spring 是如何解决循环依赖的?
前言 相信很多小伙伴在工作中都会遇到循环依赖,不过大多数它是这样显示的: 还会提示这么一句: Requested bean is currently in creation: Is there an ...
- [跟我学spring学习笔记][DI循环依赖]
循环依赖 什么是循环依赖? 循环依赖就是循环引用,就是两个或多个Bean相互之间的持有对方. Spring容器循环依赖包括构造器循环依赖和setter循环依赖,那Spring容器如何解决循环依赖呢? ...
- Spring.getBean()流程和循环依赖的解决
getBean流程介绍(以单例的Bean流程为准) getBean(beanName) 从BeanFactory中获取Bean的实例对象,真正获取的逻辑由doGetBean实现. doGetBean( ...
- Spring是如何解决循环依赖的
前言 在面试的时候这两年有一个非常高频的关于spring的问题,那就是spring是如何解决循环依赖的.这个问题听着就是轻描淡写的一句话,其实考察的内容还是非常多的,主要还是考察的应聘者有没有研究过s ...
- 听说你还不知道Spring是如何解决循环依赖问题的?
Spring如何解决的循环依赖,是近两年流行起来的一道Java面试题. 其实笔者本人对这类框架源码题还是持一定的怀疑态度的. 如果笔者作为面试官,可能会问一些诸如"如果注入的属性为null, ...
- Spring 是怎么处理循环依赖的?
Java语法中的循环依赖 首先看一个使用构造函数的循环依赖,如下: public class ObjectA { private ObjectB b; public ObjectA(ObjectB b ...
- SpringBoot导入jsp依赖始终报错
先粘出我自己的pom代码: <?xml version="1.0" encoding="UTF-8"?> <project xmlns=&qu ...
- 目标平台、活动平台 配置,出现未能加载文件或程序集“xxx”或它的某一个依赖项报错
今天在做动态加载程序集的时候,发现明明程序集存在的情况下,还是依然报“未能加载文件或程序集“xxx”或它的某一个依赖项报错”的错误,排除了程序和配置的错误后,怀疑是否是环境的问题,于是百度加msdn后 ...
随机推荐
- html5 新增元素以及css3新特性
HTML5 1.HTML5 新元素 HTML5提供了新的元素来创建更好的页面结构: 标签 描述 <article> 定义页面独立的内容区域. <aside> 定义页面的侧边栏内 ...
- 开发环境绑定host vue 返回 invalid host header
事情:使用域名绑定host为本机电脑ip,vue返回 invalid host header 原因:新版的webpack-dev-server出于安全考虑,默认检查hostname,如果hostnam ...
- 多线性方程组迭代算法——Jacobi迭代算法的Python实现
多线性方程(张量)组迭代算法的原理请看这里:若想看原理部分请留言,不方便公开分享 Gauss-Seidel迭代算法:多线性方程组迭代算法——Gauss-Seidel迭代算法的Python实现 impo ...
- Java面试之String、StringBuffer和StringBuilder的区别和原理
首先我们先来谈谈String: String 对象一旦创建,其值是不能修改的,如果要修改,会重新开辟内存空间来存储修改之后的对象,即修改了 String 的引用. 因为 String 的底层是用数组来 ...
- 关于UITableViewAutomaticDimension的产生的bug
一.下面这句代码要想有作用 在iOS11之前需要适配,两个代理都需要实现 - (CGFloat)tableView:(UITableView *)tableView heightForHeader ...
- [web设计]带有方向感应的hover effect
See the Pen bdxLQa by jeremylee (@lijie33402) on CodePen. codepen不知道怎么嵌入到cnblogs..待编辑 参考资料 参考博客
- EventBus总结(原)
1.EventBus的作用 EventBus is a publish/subscribe event bus for Android and Java. EventBus可以被用来在各种自定义的监听 ...
- C语言结构体实例-创建兔子
参考裸编程思想. #include <stdio.h> //#include "ycjobject.h" // 颜色定义 #define CL_BLACK 0 #def ...
- Java8使用实现Runnable接口方式创建新线程的方法
环境介绍 JDK版本:1.8 开发架构:spring boot 2.x 日志:slf4j 实现步骤 Runnable接口中只有一个run()方法,它是非Thread类子类的类提供的一种激活方式.一个类 ...
- SpringDataJpa全部依赖
<properties> <spring.version>4.2.4.RELEASE</spring.version> <hibernate.version& ...