这篇文章将主要介绍循环依赖的解决细节,以便于解决一些常见的面试题的细节部分。能够获取到的知识点包括以下:
1,spring循环依赖是如何解决的?
2,为什么是三级缓存,只有二级行不行?
3,描述其中一些细节,bean的生命周期。
便于读者去能够debug源码,会把重点的debug细节点,贴出来,感兴趣的读者可以试着去本地构建循环依赖,一步步分析每个部分做了什么事情, 下面主要阐述一些结论。下面的代码截取来自SpringBoot 5.1.8版本
建议使用条件断点,如图下面Conditions的内容,我们只需要关注A,B两个bean的执行流即可。
开篇前对这个部分涉及到的主要容器进行前置基础知识介绍,在了解相关的概念之后,能够更好的去读这一部分源码;
// 一级缓存,存放完整的bean
Map<String, Object> singletonObjects = new ConcurrentHashMap<>(256);
// 二级缓存,存放半成品的bean和经过增强、代理之后的bean,这里对于AOP的代理逻辑很有必要
Map<String, Object> earlySingletonObjects = new HashMap<>(16);
// 三级缓存,存放bean对应的工厂方法,即可以用工厂方法调用去生成指定名字的bean
Map<String, ObjectFactory<?>> singletonFactories = new HashMap<>(16);
// 保留注册bean 名字的注册顺序的集合容器
Set<String> registeredSingletons = new LinkedHashSet<>(256);
// 保留正在创建中的bean的名字集合
Set<String> singletonsCurrentlyInCreation = Collections.newSetFromMap(new ConcurrentHashMap<>(16));
我们的分析代码的开始部分从getBean(String name)开始,这个方法直接调用doCreateBean(),所以从这个方法开始,下面循环依赖的部分是本地构建的A -> B的情况,下面将以A,B作为两个bean的名字来进行阐述:
由于源码存在比较多方法重载的情况,所以将方法的参数类型也写出来,供读者能够定位到指定代码进行分析。
代码段1: doGetBean(final String name, @Nullable final Class requiredType,
@Nullable final Object[] args, boolean typeCheckOnly)
protected <T> T doGetBean(final String name, @Nullable final Class<T> requiredType,
@Nullable final Object[] args, boolean typeCheckOnly) throws BeansException {
// 转换bean的名字,以区分工厂bean和普通bean的名字转换
final String beanName = transformedBeanName(name);
Object bean;
// 从缓存中获取A,显然现在不可能有,第一次进来
Object sharedInstance = getSingleton(beanName);
// 第一次过来sharedInstancew为空,直接过
if (sharedInstance != null && args == null) {
if (logger.isTraceEnabled()) {
if (isSingletonCurrentlyInCreation(beanName)) {
logger.trace("Returning eagerly cached instance of singleton bean '" + beanName +
"' that is not fully initialized yet - a consequence of a circular reference");
}
else {
logger.trace("Returning cached instance of singleton bean '" + beanName + "'");
}
}
bean = getObjectForBeanInstance(sharedInstance, name, beanName, null);
}
else {
// 还没创建过,也过掉
if (isPrototypeCurrentlyInCreation(beanName)) {
throw new BeanCurrentlyInCreationException(beanName);
}
// 当前已经是顶级工厂,不存在父工厂,不进入if
BeanFactory parentBeanFactory = getParentBeanFactory();
if (parentBeanFactory != null && !containsBeanDefinition(beanName)) {
// Not found -> check parent.
String nameToLookup = originalBeanName(name);
if (parentBeanFactory instanceof AbstractBeanFactory) {
return ((AbstractBeanFactory) parentBeanFactory).doGetBean(
nameToLookup, requiredType, args, typeCheckOnly);
}
else if (args != null) {
// Delegation to parent with explicit args.
return (T) parentBeanFactory.getBean(nameToLookup, args);
}
else if (requiredType != null) {
// No args -> delegate to standard getBean method.
return parentBeanFactory.getBean(nameToLookup, requiredType);
}
else {
return (T) parentBeanFactory.getBean(nameToLookup);
}
}
// 类型检查,是否已经创建过,这是一个bean优化检查的方式
if (!typeCheckOnly) {
markBeanAsCreated(beanName);
}
try {
final RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName);
checkMergedBeanDefinition(mbd, beanName, args);
// Guarantee initialization of beans that the current bean depends on.
// 解析bean的先后加载顺序,如有则先解析当前bean前面需要先加载的bean
String[] dependsOn = mbd.getDependsOn();
if (dependsOn != null) {
for (String dep : dependsOn) {
// 判断bean名称和dep是否相互依赖
if (isDependent(beanName, dep)) {
throw new BeanCreationException(mbd.getResourceDescription(), beanName,
"Circular depends-on relationship between '" + beanName + "' and '" + dep + "'");
}
// 把当前的dep作为beanName对应的依赖集合里
registerDependentBean(dep, beanName);
try {
getBean(dep);
}
catch (NoSuchBeanDefinitionException ex) {
throw new BeanCreationException(mbd.getResourceDescription(), beanName,
"'" + beanName + "' depends on missing bean '" + dep + "'", ex);
}
}
}
// 直到这里开始做事,判断是否单例,此处A为单例,即默认
if (mbd.isSingleton()) {
// getSingleton传入了第二个参数是创建bean的工厂方法,objectFactory后续会用到
// 接下来将继续重点分析这个方法,从缓存中拿拿不到就会用传入的工厂函数去创建
sharedInstance = getSingleton(beanName, () -> {
try {
// 创建bean的流程集中在这个createBean方法里
return createBean(beanName, mbd, args);
}
catch (BeansException ex) {
// Explicitly remove instance from singleton cache: It might have been put there
// eagerly by the creation process, to allow for circular reference resolution.
// Also remove any beans that received a temporary reference to the bean.
destroySingleton(beanName);
throw ex;
}
});
// 对于工厂bean需要调用getObject方法返回真正的bean
bean = getObjectForBeanInstance(sharedInstance, name, beanName, mbd);
}
// 处理原型bean的逻辑
else if (mbd.isPrototype()) {
// It's a prototype -> create a new instance.
Object prototypeInstance = null;
try {
beforePrototypeCreation(beanName);
prototypeInstance = createBean(beanName, mbd, args);
}
finally {
afterPrototypeCreation(beanName);
}
bean = getObjectForBeanInstance(prototypeInstance, name, beanName, mbd);
}
// 处理自定义scope的bean,如果是动态加载参数配置这里发生作用@RefreshScope作用原理
else {
String scopeName = mbd.getScope();
final Scope scope = this.scopes.get(scopeName);
if (scope == null) {
throw new IllegalStateException("No Scope registered for scope name '" + scopeName + "'");
}
try {
Object scopedInstance = scope.get(beanName, () -> {
beforePrototypeCreation(beanName);
try {
return createBean(beanName, mbd, args);
}
finally {
afterPrototypeCreation(beanName);
}
});
bean = getObjectForBeanInstance(scopedInstance, name, beanName, mbd);
}
catch (IllegalStateException ex) {
throw new BeanCreationException(beanName,
"Scope '" + scopeName + "' is not active for the current thread; consider " +
"defining a scoped proxy for this bean if you intend to refer to it from a singleton",
ex);
}
}
}
catch (BeansException ex) {
cleanupAfterBeanCreationFailure(beanName);
throw ex;
}
}
// Check if required type matches the type of the actual bean instance.
if (requiredType != null && !requiredType.isInstance(bean)) {
try {
T convertedBean = getTypeConverter().convertIfNecessary(bean, requiredType);
if (convertedBean == null) {
throw new BeanNotOfRequiredTypeException(name, requiredType, bean.getClass());
}
return convertedBean;
}
catch (TypeMismatchException ex) {
if (logger.isTraceEnabled()) {
logger.trace("Failed to convert bean '" + name + "' to required type '" +
ClassUtils.getQualifiedName(requiredType) + "'", ex);
}
throw new BeanNotOfRequiredTypeException(name, requiredType, bean.getClass());
}
}
// 最后返回bean对象
return (T) bean;
}
代码段2: getSingleton(String beanName, ObjectFactory<?> singletonFactory)
// 获取单例bean
public Object getSingleton(String beanName, ObjectFactory<?> singletonFactory) {
Assert.notNull(beanName, "Bean name must not be null");
// 多线程环境,访问bean的时候队一级缓存容器加同步锁访问
synchronized (this.singletonObjects) {
// A还没创建好,从一级缓存容器拿不到,此时为null
Object singletonObject = this.singletonObjects.get(beanName);
if (singletonObject == null) {
if (this.singletonsCurrentlyInDestruction) {
throw new BeanCreationNotAllowedException(beanName,
"Singleton bean creation not allowed while singletons of this factory are in destruction " +
"(Do not request a bean from a BeanFactory in a destroy method implementation!)");
}
if (logger.isDebugEnabled()) {
logger.debug("Creating shared instance of singleton bean '" + beanName + "'");
}
// 创建之前,先去判断是否在正在创建的bean set集合里,没有则加入进去,表示A正在创建
beforeSingletonCreation(beanName);
boolean newSingleton = false;
boolean recordSuppressedExceptions = (this.suppressedExceptions == null);
if (recordSuppressedExceptions) {
this.suppressedExceptions = new LinkedHashSet<>();
}
try {
// 这里开始调用singletonFactory工厂bean的getObject方法,就是上面第二个参数的逻辑,
// 下面将重点分析这个getObject()方法对应的createBean()方法
singletonObject = singletonFactory.getObject();
// 修改标志
newSingleton = true;
}
catch (IllegalStateException ex) {
// Has the singleton object implicitly appeared in the meantime ->
// if yes, proceed with it since the exception indicates that state.
singletonObject = this.singletonObjects.get(beanName);
if (singletonObject == null) {
throw ex;
}
}
catch (BeanCreationException ex) {
if (recordSuppressedExceptions) {
for (Exception suppressedException : this.suppressedExceptions) {
ex.addRelatedCause(suppressedException);
}
}
throw ex;
}
finally {
if (recordSuppressedExceptions) {
this.suppressedExceptions = null;
}
// 创建完之后从正在创建的bean set集合里剔除
afterSingletonCreation(beanName);
}
// 如果是新的单例,加入到缓存
if (newSingleton) {
// 三个级别缓存之间,进行缓存提升,添加到一级,移除二、三级缓存
addSingleton(beanName, singletonObject);
}
}
return singletonObject;
}
}
代码段3: createBean(String beanName, RootBeanDefinition mbd, @Nullable Object[] args)
// 真正创建bean的逻辑
protected Object createBean(String beanName, RootBeanDefinition mbd, @Nullable Object[] args)
throws BeanCreationException {
if (logger.isTraceEnabled()) {
logger.trace("Creating instance of bean '" + beanName + "'");
}
RootBeanDefinition mbdToUse = mbd;
// Make sure bean class is actually resolved at this point, and
// clone the bean definition in case of a dynamically resolved Class
// which cannot be stored in the shared merged bean definition.
Class<?> resolvedClass = resolveBeanClass(mbd, beanName);
if (resolvedClass != null && !mbd.hasBeanClass() && mbd.getBeanClassName() != null) {
mbdToUse = new RootBeanDefinition(mbd);
mbdToUse.setBeanClass(resolvedClass);
}
// Prepare method overrides.
try {
// bean配置的时候,可以配置覆盖方法,如果有的话,这里可以执行覆盖,此处不分析
mbdToUse.prepareMethodOverrides();
}
catch (BeanDefinitionValidationException ex) {
throw new BeanDefinitionStoreException(mbdToUse.getResourceDescription(),
beanName, "Validation of method overrides failed", ex);
}
try {
// Give BeanPostProcessors a chance to return a proxy instead of the target bean instance.
// 这里如果有beanPostProcessors接口返回了自定义的bean,那么将直接返回当前bean,流程不再继续向下执行
Object bean = resolveBeforeInstantiation(beanName, mbdToUse);
if (bean != null) {
return bean;
}
}
catch (Throwable ex) {
throw new BeanCreationException(mbdToUse.getResourceDescription(), beanName,
"BeanPostProcessor before instantiation of bean failed", ex);
}
try {
// 正常场景里,走到这里开始执行doCreateBean创建bean的逻辑,下面代码片段4分析这个方法
Object beanInstance = doCreateBean(beanName, mbdToUse, args);
if (logger.isTraceEnabled()) {
logger.trace("Finished creating instance of bean '" + beanName + "'");
}
return beanInstance;
}
catch (BeanCreationException | ImplicitlyAppearedSingletonException ex) {
// A previously detected exception with proper bean creation context already,
// or illegal singleton state to be communicated up to DefaultSingletonBeanRegistry.
throw ex;
}
catch (Throwable ex) {
throw new BeanCreationException(
mbdToUse.getResourceDescription(), beanName, "Unexpected exception during bean creation", ex);
}
}
代码段4:doCreateBean(final String beanName, final RootBeanDefinition mbd, final @Nullable Object[] args)
// 关于bean创建的生命周期,执行流程最复杂的部分,我愿称之为最需要掌握的部分
protected Object doCreateBean(final String beanName, final RootBeanDefinition mbd, final @Nullable Object[] args)
throws BeanCreationException {
// Instantiate the bean.
BeanWrapper instanceWrapper = null;
if (mbd.isSingleton()) {
// 移除已经创建了的缓存
instanceWrapper = this.factoryBeanInstanceCache.remove(beanName);
}
if (instanceWrapper == null) {
// 这里createBeanInstance主要是用对应的构造函数,完成bean的实例化,并且封装成一个instanceWrapper
instanceWrapper = createBeanInstance(beanName, mbd, args);
}
// 从wrapper里拿出来这个bean的实例,才经过实例化,显然是个半成品bean
final Object bean = instanceWrapper.getWrappedInstance();
Class<?> beanType = instanceWrapper.getWrappedClass();
if (beanType != NullBean.class) {
mbd.resolvedTargetType = beanType;
}
// Allow post-processors to modify the merged bean definition.
synchronized (mbd.postProcessingLock) {
if (!mbd.postProcessed) {
try {
// 这里处理到这个bean所依赖的属性@Autowired@Value注解信息,并且将检查到的信息
// 放入到set集合里,需要注入的属性集合Set<InjectedElement> checkedElements;
applyMergedBeanDefinitionPostProcessors(mbd, beanType, beanName);
}
catch (Throwable ex) {
throw new BeanCreationException(mbd.getResourceDescription(), beanName,
"Post-processing of merged bean definition failed", ex);
}
mbd.postProcessed = true;
}
}
// Eagerly cache singletons to be able to resolve circular references
// even when triggered by lifecycle interfaces like BeanFactoryAware.
// 这里判断A是否能早期暴露,依次判断,是否单例,是否循环引用,是否正在创建中,显然已经是创建中
// 在代码片段2里其实已经加入进了正在创建的set容器里了
boolean earlySingletonExposure = (mbd.isSingleton() && this.allowCircularReferences &&
isSingletonCurrentlyInCreation(beanName));
if (earlySingletonExposure) {
if (logger.isTraceEnabled()) {
logger.trace("Eagerly caching bean '" + beanName +
"' to allow for resolving potential circular references");
}
// 允许早期暴露,来到这个方法,首先将bean名字对应的工厂函数加入到三级缓存里;这个工厂函数
// getEarlyBeanReference()后面还将继续分析。此时可以直到bean A已经暴露在了三级缓存里了
// 并且当前bean的名字也将添加到已创建单例bean的容器registeredSingletons集合里;
// 这个方法同时还会提供了给beanPostProcessor方法去对半成的bean进行代理,将代理的bean
// AOP的代理逻辑,都将在这里发生作用
addSingletonFactory(beanName, () -> getEarlyBeanReference(beanName, mbd, bean));
}
// Initialize the bean instance.开始执行bean的初始化流程,一个bean要经历实例化,初始化才算完整
Object exposedObject = bean;
try {
// bean的属性在这里进行填充,并且循环依赖的触发,导致A->B,将开始创建bean B,同样地B的流程会
// 类似A重新走一遍流程,知道开始B解析自己的属性依赖,开始尝试获取A的时候,这时候打开了破局,
// 此时A,已经加入了三级缓存,那么在获取A的过程里,可以通过三级缓存对应的ObjctFactory获取A的早期
// 引用,尽管此时的A还没彻底变成一个完整的bean,但是B已经完成了初始化,然后b加入到一级缓存里;
// A继续开始执行自己依赖部分往下走,获取B,然后开始扩展接口完成整个创建过程;
populateBean(beanName, mbd, instanceWrapper);
// 初始化接口调用,这里也是着重分析的方法,后续将进入该方法具体分析
exposedObject = initializeBean(beanName, exposedObject, mbd);
}
catch (Throwable ex) {
if (ex instanceof BeanCreationException && beanName.equals(((BeanCreationException) ex).getBeanName())) {
throw (BeanCreationException) ex;
}
else {
throw new BeanCreationException(
mbd.getResourceDescription(), beanName, "Initialization of bean failed", ex);
}
}
if (earlySingletonExposure) {
// 获取单例bean.此时传入的第二个参数为false,这一次获取不允许早期引用,参考代码片段6
Object earlySingletonReference = getSingleton(beanName, false);
if (earlySingletonReference != null) {
if (exposedObject == bean) {
exposedObject = earlySingletonReference;
}
else if (!this.allowRawInjectionDespiteWrapping && hasDependentBean(beanName)) {
String[] dependentBeans = getDependentBeans(beanName);
Set<String> actualDependentBeans = new LinkedHashSet<>(dependentBeans.length);
for (String dependentBean : dependentBeans) {
if (!removeSingletonIfCreatedForTypeCheckOnly(dependentBean)) {
actualDependentBeans.add(dependentBean);
}
}
if (!actualDependentBeans.isEmpty()) {
throw new BeanCurrentlyInCreationException(beanName,
"Bean with name '" + beanName + "' has been injected into other beans [" +
StringUtils.collectionToCommaDelimitedString(actualDependentBeans) +
"] in its raw version as part of a circular reference, but has eventually been " +
"wrapped. This means that said other beans do not use the final version of the " +
"bean. This is often the result of over-eager type matching - consider using " +
"'getBeanNamesOfType' with the 'allowEagerInit' flag turned off, for example.");
}
}
}
}
// Register bean as disposable.
try {
registerDisposableBeanIfNecessary(beanName, bean, mbd);
}
catch (BeanDefinitionValidationException ex) {
throw new BeanCreationException(
mbd.getResourceDescription(), beanName, "Invalid destruction signature", ex);
}
return exposedObject;
}
代码段5:initializeBean(final String beanName, final Object bean, @Nullable RootBeanDefinition mbd)方法
// 这里是bean的生命周期着重部分
protected Object initializeBean(final String beanName, final Object bean, @Nullable RootBeanDefinition mbd) {
// 依据安全策略,执行invokeAwareMethods方法,这里方法里执行了一系列aware接口的扩展,
// 依次是BeanNameAware、BeanClassLoaderAware、BeanFactoryAware对应的setXXX方法
if (System.getSecurityManager() != null) {
AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
invokeAwareMethods(beanName, bean);
return null;
}, getAccessControlContext());
}
else {
invokeAwareMethods(beanName, bean);
}
Object wrappedBean = bean;
if (mbd == null || !mbd.isSynthetic()) {
// 开始加载beforeInitialization的扩展点的before动作
wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);
}
try {
// 执行InitializingBean的afterPropertiesSet方法,再执行指定的init-method方法
invokeInitMethods(beanName, wrappedBean, mbd);
}
catch (Throwable ex) {
throw new BeanCreationException(
(mbd != null ? mbd.getResourceDescription() : null),
beanName, "Invocation of init method failed", ex);
}
if (mbd == null || !mbd.isSynthetic()) {
// 最后再执行下AfterInitialization扩展的after动作
wrappedBean = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);
}
// 以上一系列可扩展的接口,对bean的处理都视为对bean的功能增强,执行完所有返回增强后的bean
return wrappedBean;
}
代码段6: getSingleton(String beanName, boolean allowEarlyReference)
// 此处和上述getSingleton(String beanName, ObjectFactory<?> singletonFactory) 参数不一致,属于重载
protected Object getSingleton(String beanName, boolean allowEarlyReference) {
// 从一级缓存获取,此时A为空
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();
/** 放入二级缓存,注意这里,A->B 创建A的过程中,解析依赖B,然后A的状态还只是加入了一个三级缓存,
这时候B开始创建,同样B也会去解析依赖属性A,这时候再去尝试getBean("A"),
会去创建A,就会来到这里,将调用A提前放入到三级缓存的工厂函数,获取到早期引用bean,
这里会把A放入到二级缓存里,并且从三级缓存里移除*/
this.earlySingletonObjects.put(beanName, singletonObject);
this.singletonFactories.remove(beanName);
}
}
}
}
return singletonObject;
}
小结:
这里用文字阐述下,解决的细节A->B 这两个bean的耦合之处在于在处理依赖注入属性发生的,所以A的创建过程中,直到解析依赖属性B的时候,才开始创建B,然后会把B也会进行实例化,也会放入三级缓存里一个早期引用,继续填充属性A的时候,会去尝试获取A的单例,这时候,刚开始A放入到三级缓存里的早期引用开始作用了,B可以顺利拿到A,完成自身的属性填充,然后B将自己提升到一级缓存里;那么A解析依赖属性B的时候,到现在结束了,A顺利也拿到了B的依赖。所以A将完成自身的属性填充,然后经过扩展接口的增强,然后缓存提升完成初始化。整个bean的创建过程结束;也就是A->B,实际上是B先完成bean的创建,再是A的创建;
总结
回答本文开头提出的问题:
- Spring循环依赖是通过三级缓存依赖来解决的,并且只能是解决单例的,属性循环依赖,不能是构造依赖,通过上面可得知,bean的依赖是属性注入那一部分,在此之前bean已经完成了自身的实例化;
- 必须使用三级缓存,尽管解决循环依赖可以只用两级,例如只要一级和三级,依然可以通过三级暴露早期引用解决循环依赖,但是解决AOP等代理的场景,需要将增强后的bean放入到二级缓存里,保证每次从二级缓存里取出来的是同一个单例bean;
- 根据上述分析,bean的生命周期,先经由自身的对应构造器进行实例化,然后进行属性注入,在开始执行一系列扩展点,进入initializeBean方法,先执行一系列实现了aware接口的方法,依次beanNameAware,beanClassLoaderAware,beanFactoryAware的set方法,然后再执行实现initializeBean接口的afterPropertiesSet()方法,在执行init-method指定的方法,最后容器销毁的时候执行destroyed方法,bean的全生命周期流程。