目的:源码调试构造器注入,看看是怎么报错的。
环境: spring:5.2.3 jdk:1.8
(小弟第一次写,知道有待改进,路过的各位大哥,在评论里给点意见,我会逐步完善的,谢谢了~)
一、准备
首先准备两个循环依赖的类:userService和roleService
<bean id="userService" class="com.chris.spring.service.UserServiceImpl">
<constructor-arg ref="roleService"/>
</bean>
<bean id="roleService" class="com.chris.spring.service.RoleService">
<constructor-arg ref="userService"/>
</bean>
二、开始调试
依赖注入的触发点:是容器实例化所有非懒加载Bean时候。准确的说,是实例化bean之前,检查缓存中是否存在一样的bean,如果存在说明发生依赖注入,不存在就插入缓存中。
所以可以从refresh()方法中的finishBeanFactoryInitialization(beanFactory)方法看起。
1 /**
2 * Finish the initialization of this context's bean factory,
3 * initializing all remaining singleton beans.
4 */
5 protected void finishBeanFactoryInitialization(ConfigurableListableBeanFactory beanFactory) {
......
//初始化所有非懒加载的单例bean,
32 // Instantiate all remaining (non-lazy-init) singletons.
33 beanFactory.preInstantiateSingletons();
34 }
1. org.springframework.beans.factory.support.DefaultListableBeanFactory#preInstantiateSingletons()
![](https://img-blog.csdnimg.cn/img_convert/122343c326089b8e3f93a1eb0b06199f.png)
- 获取BeanDefinition集合,可以看到userService和roleServic已经在容器中
![](https://img-blog.csdnimg.cn/img_convert/6268c127072ae4c6e909917f548ae09a.png)
- 循环遍历每个BeanDefinition,判读是不是非抽象的&&单例的&&懒加载的 bean。
- 如果是工厂bean,走处理工厂的方法;如果普通bean,调用getBean方法。
2. org.springframework.beans.factory.support.AbstractBeanFactory#doGetBean()
![](https://img-blog.csdnimg.cn/img_convert/bdfd2ba45bfdad0cccde42f6be40a173.png)
![](https://img-blog.csdnimg.cn/img_convert/1aa2b3f8c0febebb82f248901bc183eb.png)
- 在doGetBean方法中,直接找到创建单例对象的这部分代码,如下图:
![](https://img-blog.csdnimg.cn/img_convert/df34f5313892d753fb12b2e3cb983f4a.png)
3. org.springframework.beans.factory.support.DefaultSingletonBeanRegistry#getSingleton()
![](https://img-blog.csdnimg.cn/img_convert/873ff9abefd037563e190e767a1b0f83.png)
- 首先看一下注释:返回给定名字的单例对象,如果没有注册过,那么就创建并注册。
- ObjectFactory<?>是什么?它就是一个简单工厂,可以返回对象实例,怎么用,下面说。
![](https://img-blog.csdnimg.cn/img_convert/780dd72051be07bd6028740a3f0bb76d.png)
- this.singletonObjects,缓存已经实例化的单例对象的集合。userService还没有实例化。
- 在创建单例bean之前要做些事情:检查这个bean是否可以被创建,调用beforeSingletonCreation(beanName)逻辑如下:
![](https://img-blog.csdnimg.cn/img_convert/91f0a7b720e1ce192d750cdec595da47.png)
- inCreationCheckExclusions:要排除的bean的集合。userService不是要排除的bean,这个判断是true。
- singletonsCurrentlyInCreation:正在创建的单例集合。把userService放在要被创建的集合,插入成功是true,前面还有一个‘!’,所以这个判断是false。不抛出异常。
- 看到这里基本可以明白了,循环依赖就是这里抛出的异常:第二次检查userService时,userService在singletonsCurrentlyInCreation集合中,说明userService正准备实例化,发生已经循环依赖了。
![](https://img-blog.csdnimg.cn/img_convert/58c1bbc07d591fe9e292b66299bf1dcd.png)
- 通过上面的检查,userService可以被创建。调用singletonFactory.getObject();获取对象,即从ObjectFactory<?>这个对象工厂中获取userService。
- 问题来了,这个工厂(singletonFactory)哪来的?怎么工作的?看函数声明:
![](https://img-blog.csdnimg.cn/img_convert/2e0112bb6e3facd5cb54e6d59b93fda2.png)
- 它是传进来的参数,怎么传进来的,再看看调用:
![](https://img-blog.csdnimg.cn/img_convert/3f140ee5c53c0d5d12ebb8d47a6c90ae.png)
- 原来是createBean(beanName, mbd, args)的返回值。那么就是说明createBean会返回一个工厂对象,这个工厂对象只生产userService这一个对象。
4.org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#createBean()
![](https://img-blog.csdnimg.cn/img_convert/d9797b07450e4d0a7b8b314beab767bb.png)
- 在createBean方法中,找到doCreateBean方法:
5.org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#doCreateBean()
![](https://img-blog.csdnimg.cn/img_convert/e718d626f7ac275004fddaf21915162e.png)
- 调用createBeanInstance(beanName, mbd, args)实例化bean
6.org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#createBeanInstance()
![](https://img-blog.csdnimg.cn/img_convert/68f8b6f055c97e4c1a97896dc8e7b128.png)
- 这里会判断是不是构造方法注入,点进去:
![](https://img-blog.csdnimg.cn/img_convert/3ba931c7515f2d5923f161f7495e8f9d.png)
- 这里有两个方法,创建一个构造方法解析器,然后解析构造方法
7.org.springframework.beans.factory.support.ConstructorResolver#autowireConstructor()
![](https://img-blog.csdnimg.cn/img_convert/b1a86ad183d9440dfa8444d2f0f5c522.png)
- 在这里解析到userService的构造方法中,有个参数是RoleService类型的,继续向下走:
![](https://img-blog.csdnimg.cn/img_convert/0b004d9e585510536135c2d5ca5578d7.png)
- 这里会解析构造方法的参数,点进去:
![](https://img-blog.csdnimg.cn/img_convert/567aa850671536de3ed274b428b0c542.png)
- 解析构造方法参数的值,即参数名称。解析出来是roleService,解析过程如下:
![](https://img-blog.csdnimg.cn/img_convert/29c95ab21a378dc8bcf970716d53c70a.png)
- 调用resolveReference(argName, ref)方法,获取roleService的实例,然后和userService关联起来。获取roleService代码如下:
![](https://img-blog.csdnimg.cn/img_convert/532f624e65718931472379f13019de79.png)
- 可以看到,容器调用getBean、doGetBean等方法,获取roleService。
- 像userService一样,分析roleSerice的结果是:roleSerice依赖userService。容器又去获取userService,跳过roleService的过程,直接到达第二次获取userService:
![](https://img-blog.csdnimg.cn/img_convert/b0f1c93a816c2b5cb2c65721b46f305f.png)
- 直接跳转到出现问题的地方,检查userService是否可以实例化:
![](https://img-blog.csdnimg.cn/img_convert/ab64c32fcbc99530b5530d1aeb3f3e5a.png)
- 这时候发现,准备实例化的集合里面有userService,再次插入userService失败,说明发生了循环依赖,容器就报错了。
三、结论:
singletonsCurrentlyInCreation的作用:记录准备好实例化的单例bean。如果里面出现了重复的bean,说明有循环依赖。