spring循环注入详解

这篇博客详细探讨了Spring框架处理循环依赖的过程,从源码角度分析了bean的生命周期,包括构造、属性赋值、初始化和后置处理器的执行。在循环注入中,Spring如何通过三级缓存管理bean并确保AOP功能的正常应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本人仅仅是怕自己遗忘,将来复习使用的资料!
有两个bean,indexService 和userService,两者内部互相注入了对方,那么就造成循环依赖了,spring底层是如何操作的呢?如下:
1、版本:
在这里插入图片描述

2、入口:

	@Test
	public void test01(){
		AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfigOfAOP.class);
	}

3、spring源码入口,重点看refresh()方法

	public AnnotationConfigApplicationContext(Class<?>... annotatedClasses) {
		this();
		register(annotatedClasses);
		refresh();
	}

4、重点看其中的finishBeanFactoryInitialization方法,因为spring容器里面的bean都是有生命周期的,有周期意味着每个bean在创建==》属性设值==》初始化(光初始化spring就有三种方式,而且每种初始化执行的顺序还不一致)》各种后置处理器(共9种)》实例化==》销毁等等步骤

	@Override
	public void refresh() throws BeansException, IllegalStateException {
		synchronized (this.startupShutdownMonitor) {
			// Prepare this context for refreshing.
			prepareRefresh();

			// Tell the subclass to refresh the internal bean factory.
			ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();

			// Prepare the bean factory for use in this context.
			prepareBeanFactory(beanFactory);

			try {
				// Allows post-processing of the bean factory in context subclasses.
				postProcessBeanFactory(beanFactory);

				// Invoke factory processors registered as beans in the context.
				invokeBeanFactoryPostProcessors(beanFactory);

				// Register bean processors that intercept bean creation.
				registerBeanPostProcessors(beanFactory);

				// Initialize message source for this context.
				initMessageSource();

				// Initialize event multicaster for this context.
				initApplicationEventMulticaster();

				// Initialize other special beans in specific context subclasses.
				onRefresh();

				// Check for listener beans and register them.
				registerListeners();

				// Instantiate all remaining (non-lazy-init) singletons.
				finishBeanFactoryInitialization(beanFactory);

				// Last step: publish corresponding event.
				finishRefresh();
			}

			catch (BeansException ex) {
				if (logger.isWarnEnabled()) {
					logger.warn("Exception encountered during context initialization - " +
							"cancelling refresh attempt: " + ex);
				}

				// Destroy already created singletons to avoid dangling resources.
				destroyBeans();

				// Reset 'active' flag.
				cancelRefresh(ex);

				// Propagate exception to caller.
				throw ex;
			}

			finally {
				// Reset common introspection caches in Spring's core, since we
				// might not ever need metadata for singleton beans anymore...
				resetCommonCaches();
			}
		}
	}

5、跟着debug走,掉这个方法:

 beanFactory.preInstantiateSingletons();

在这里插入图片描述

6、默认bean都是单例的,所以接着走下面这个方法:
在这里插入图片描述
7、接着走:
在这里插入图片描述
8、接着走下面的方法,注意其中这个方法:
Object sharedInstance = getSingleton(beanName);
当前程序是在做创建indexService的步骤
下面这个代码的意思是首先容器会第一次从容器中去拿indexService,如果拿到就直接返回,拿不到会去判断是否为空,且当前的bea是否正在创建,其中的isSingletonCurrentlyInCreation 很重要,因为它就是判断singletonsCurrentlyInCreation是否包含当前的bean(因为后面会将当前的bean放到这个名字为正在创建的set中),如果条件都满足,会先去earlySingletonObjects三级缓存中取,取不到,继续条件判断,判空当前对象和允许开启循环注入(可以进行认为的设置默认=true),此时的三级缓存(earlySingletonObjects)和二级缓存(singletonFactories)都是空,所以此时是不可能获取到对象的!

protected Object getSingleton(String beanName, boolean allowEarlyReference) {
		Object singletonObject = this.singletonObjects.get(beanName);
		if (singletonObject == null && isSingletonCurrentlyInCreation(beanName)) {
			synchronized (this.singletonObjects) {
				singletonObject = this.earlySingletonObjects.get(beanName);
				if (singletonObject == null && allowEarlyReference) {
					ObjectFactory<?> singletonFactory = this.singletonFactories.get(beanName);
					if (singletonFactory != null) {
						singletonObject = singletonFactory.getObject();
						this.earlySingletonObjects.put(beanName, singletonObject);
						this.singletonFactories.remove(beanName);
					}
				}
			}
		}
		return (singletonObject != NULL_OBJECT ? singletonObject : null);
	}

在这里插入图片描述
9、接着走创建bean的流程,注意传进去个ObjectFactory
在这里插入图片描述
10、将当前的indexservice 放进正在创建的缓存中singletonsCurrentlyInCreation,singletonsCurrentlyInCreation有这个bean,就代表当前这给bean正在创建中
在这里插入图片描述

11、接着用getObject的 createBean,工程方法去创建对象:
在这里插入图片描述
12、接着走:
在这里插入图片描述
13、第一次调用构造方法,利用反射创建对象,这个时候是已经调用了构造方法,但是属性设值和初始化,都没有操作,只是 一个对象而已!
在这里插入图片描述
14、这三个条件,目前都似乎true
在这里插入图片描述
15、接着走下面的方法,表示,往二级缓存里面放beanname 和singletonFactory
注意:此时二级缓存是有bean名字和一个bean工厂(注意:为什么是个工厂,而不是放个对象进去呢?)

	protected void addSingletonFactory(String beanName, ObjectFactory<?> singletonFactory) {
		Assert.notNull(singletonFactory, "Singleton factory must not be null");
		synchronized (this.singletonObjects) {
			if (!this.singletonObjects.containsKey(beanName)) {
				this.singletonFactories.put(beanName, singletonFactory);
				this.earlySingletonObjects.remove(beanName);
				this.registeredSingletons.add(beanName);
			}
		}
	}

在这里插入图片描述
16、进行的bean的属性赋值:
在这里插入图片描述
17、调用对应的后置处理器,找到对应的属性注解,比如
@Autowired ==AutowiredAnnotationBeanPostProcessor
@Resource ==CommonAnnotationBeanPostProcessor

、后置处理器会找到对应的属性,然后去从容器中去找有没有这个对象,如果没有就会再次去创建这给userService对象,从此又开启了上面跟indexservice一摸一样的流程

当userService去完成它自己的属性注入的时候,又会去造次的调用createBean的方法,去第二次创建indexService ,但是我们这个时候要清楚,已经跟第一次不一样了,首先,容器中已经将userservice 和indexservice都标记成正在创建的bean了,其次我们的第二缓存中 此时是存在bean的,且v对用的是一个bean工厂

所以,第二次去容器中去拿对象是可以拿到的!第三缓存存在的意义是,不可能每次都去调用工厂方法去获取对象,因为在工程方法执行前,会去调用很多的后置处理器,如果每次都调用的话,会很耗时,所以就将它放第三缓存中!

但是,有个问题,不知道各位又没有去思考过,到上面的步骤,spring底层的循环注入,已经完成了,但是你没发现,它注入的其实是个java对象吗?它并不是个bean吗?假设你用aop去拦截它,为什么也能成功的拦截到呢(不信你去试下)spring的bean的生命周期:aop代理不是应在在循环注入的后面吗?就是说,你只有走了aop的那个后置处理器,你才会有aop的功能啊,为什么aop能生效呢?

原因很简单,二级缓存村的是给工厂,利用工厂的getObject方法,里面会有很多后置处理器,提前将aop的后置处理器执行了,所以在循环注入的场景下,即使你注入的是个半成品bean,并不是完善的bean,但它依然就具有aop的功能!
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值