Spring怎么解决循环依赖问题?这是spring一个非常典型的面试题,堪比spring生命周期一样常问
知识点:循环引用指单例之间的引用
下面通过一个简单的互相注入的案例,引入循环依赖问题。首先我们准备四个类,配置类,IndexService,UserService,测试类,然后使用IndexService注入UserService,UserService注入IndexService,这种使用场景,在项目中是一定会使用的,面试官通常会问Spring怎么解决循环依赖问题?下面我们就一步步分析解决过程,相关测试类没有什么代码,就是互相注入,主要是分析spring源码
配置类
package cn.tellsea.loop;
import org.springframework.context.annotation.ComponentScan;
/**
* @author Tellsea
* @date 2020-9-2
*/
@ComponentScan("cn.tellsea.loop")
public class AppConfig {
}
IndexService,其中注入UserService
package cn.tellsea.loop;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
* @author Tellsea
* @date 2020-9-2
*/
@Component
public class IndexService {
@Autowired
private UserService userService;
public IndexService() {
System.out.println("IndexService Constructor ...");
}
}
UserService,注入IndexService,两个反过来
package cn.tellsea.loop;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
* @author Tellsea
* @date 2020-9-2
*/
@Component
public class UserService {
@Autowired
private IndexService indexService;
}
测试类,实际上没有什么代码,主要是看spring源码的流程
package cn.tellsea.loop;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
* @author Tellsea
* @date 2020-9-2
*/
public class AppTest {
public static void main(String[] args) {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(AppConfig.class);
}
}
测试结果:IndexService默认在UserService上面加载,后面我们就先分析IndexService的加载
IndexService Constructor ...
org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
appConfig
indexService
userService
分析spring循环依赖问题之前, 必须要知道【spring bean的生命周期】,因为循环依赖解决的过程,就是伴随着生命周期完成的
我们知道bean的生命周期过程中, 都是一步步按照顺序执行的。上面我们说过,有IndexService和UserService两个组件,当我们程序执行到IndexService时,发现需要注入UserService属性,但是spring中才刚执行到IndexService的生命周期时,根本就没有UserService的bean对象,也就是说,当前的IndexService是正在创建中,而且UserService还是一个普通的java对象,不是spring bean对象,这里就是循环依赖的核心问题,下面逐级分析
这里可以通过运行测试方法,打印出默认执行的bean的顺序,IndexService在UserService前面,所以我们先走IndexService的生命周期。首先通过 AnnotationConfigApplicationContext 创建IOC容器
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(AppConfig.class);
然后在AnnotationConfigApplicationContext的构造方法中找到 refresh()
方法
/**
* Create a new AnnotationConfigApplicationContext, deriving bean definitions
* from the given component classes and automatically refreshing the context.
* @param componentClasses one or more component classes — for example,
* {@link Configuration @Configuration} classes
* 创建一个新的AnnotationConfigApplicationContext,并自动刷新上下文
*/
public AnnotationConfigApplicationContext(Class<?>... componentClasses) {
this();
register(componentClasses);
// 9个大步骤
refresh();
}
AbstractApplicationContext类中的核心方法是refresh()
,所有过程都是在这里执行的
@Override
public void refresh() throws BeansException, IllegalStateException {
synchronized (this.startupShutdownMonitor) {
// 准备此上下文以进行刷新。
prepareRefresh();
// 告诉子类刷新内部bean工厂。
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
// 准备在这种情况下使用的bean工厂。
prepareBeanFactory(beanFactory);
try {
// 1、允许在上下文子类中对beanFactory进行后处理。
postProcessBeanFactory(beanFactory);
// 2、调用在上下文中注册为bean的工厂处理器。
invokeBeanFactoryPostProcessors(beanFactory);
// 3、注册拦截Bean创建的Bean处理器。
registerBeanPostProcessors(beanFactory);
// 4、为此上下文初始化消息源。
initMessageSource();
// 5、为此上下文初始化事件多播器。
initApplicationEventMulticaster();
// 6、在特定上下文子类中初始化其他特殊bean。
onRefresh();
// 7、检查侦听器bean并注册它们。
registerListeners();
// 8、实例化所有剩余的(非延迟初始化)单例。
finishBeanFactoryInitialization(beanFactory);
// 9、最后一步:发布相应的事件。
finishRefresh();
}
catch (BeansException ex) {
if (logger.isWarnEnabled()) {
logger.warn("Exception encountered during context initialization - " +
"cancelling refresh attempt: " + ex);
}
// 销毁已创建的单例以避免资源悬空。
destroyBeans();
// 重置“活动”标志。
cancelRefresh(ex);
// 将异常传播给呼叫者。
throw ex;
}
finally {
//在Spring的核心中重置常见的自省缓存,因为我们
//可能不再需要单例bean的元数据...
resetCommonCaches();
}
}
}
进入第8点, 实例化所有剩余的(非延迟初始化)单例,实际上到这里,IndexService的构造方法都还没有调用,断点进入方法finishBeanFactoryInitialization(beanFactory);
,这个方法断点一路执行会发现,只有走到最后一步的时候,IndexService才会打印调用了构造方法, 我们继续断点进入方法beanFactory.preInstantiateSingletons();
,相关源码
/**
* Finish the initialization of this context's bean factory,
* initializing all remaining singleton beans.
* 完成这个上下文的bean工厂的初始化,
* 初始化所有剩余的单例bean。
*/
protected void finishBeanFactoryInitialization(ConfigurableListableBeanFactory beanFactory) {
// Initialize conversion service for this context.
if (beanFactory.containsBean(CONVERSION_SERVICE_BEAN_NAME) &&
beanFactory.isTypeMatch(CONVERSION_SERVICE_BEAN_NAME, ConversionService.class)) {
beanFactory.setConversionService(
beanFactory.getBean(CONVERSION_SERVICE_BEAN_NAME, ConversionService.class));
}
// Register a default embedded value resolver if no bean post-processor
// (such as a PropertyPlaceholderConfigurer bean) registered any before:
// at this point, primarily for resolution in annotation attribute values.
if (!beanFactory.hasEmbeddedValueResolver()) {
beanFactory.addEmbeddedValueResolver(strVal -> getEnvironment().resolvePlaceholders(strVal));
}
// Initialize LoadTimeWeaverAware beans early to allow for registering their transformers early.
String[] weaverAwareNames = beanFactory.getBeanNamesForType(LoadTimeWeaverAware.class, false, false);
for (String weaverAwareName : weaverAwareNames) {
getBean(weaverAwareName);
}
// Stop using the temporary ClassLoader for type matching.
beanFactory.setTempClassLoader(null);
// Allow for caching all bean definition metadata, not expecting further changes.
beanFactory.freezeConfiguration();
// Instantiate all remaining (non-lazy-init) singletons.
// 实例化所有单例,非lazy加载
beanFactory.preInstantiateSingletons();
}
进入之后,发现preInstantiateSingletons()
是ConfigurableListableBeanFactory接口的方法,idea点击Ctrl + alt + B进入该方法的实现DefaultListableBeanFactory.preInstantiateSingletons()
,打个断点继续进入实现类
我们发现会遍历所有BeanDefinition的name值,并验证名字合法性,这里我们使用idea的debug的小技巧,增加条件,beanName.equals(“indexService”),因为spring默认还有好几个类需要加载,否则要等断点,然后点击Done,就直接到beanName = "indexService"的循环了
当IndexService执行到下面的判断时,进入了else语句,
if (isFactoryBean(beanName)) {
...
}
else {
getBean(beanName);
}
进入getBean(String name)方法,在AbstractBeanFactory类中
@Override
public Object getBean(String name) throws BeansException {
return doGetBean(name, null, null, false);
}
然后进入doGetBean()方法,do开头的方法才是做事的方法,doGetBean()方法源码比较长,我们挑重点分析,进入方法之后,设置断点,并增加条件name.equals(“indexService”),看一下日志,发现执行到这里IndexService的构造方法依旧没有执行
/**
* Return an instance, which may be shared or independent, of the specified bean.
* @param name the name of the bean to retrieve
* @param requiredType the required type of the bean to retrieve
* @param args arguments to use when creating a bean instance using explicit arguments
* (only applied when creating a new instance as opposed to retrieving an existing one)
* @param typeCheckOnly whether the instance is obtained for a type check,
* not for actual use
* @return an instance of the bean
* @throws BeansException if the bean could not be created
*/
@SuppressWarnings("unchecked")
protected <T> T doGetBean(
String name, @Nullable Class<T> requiredType, @Nullable Object[] args, boolean typeCheckOnly)
throws BeansException {
String beanName = transformedBeanName(name);
Object bean;
// Eagerly check singleton cache for manually registered singletons.
// 单例池中拿
Object sharedInstance = getSingleton(beanName);
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 {
// Fail if we're already creating this bean instance:
// We're assumably within a circular reference.
//如果我们已经创建了这个bean实例,则失败:
//我们可以假定是在循环引用中。
if (isPrototypeCurrentlyInCreation(beanName)) {
throw new BeanCurrentlyInCreationException(beanName);
}
// 检查此工厂中是否存在bean定义
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);
}
}
if (!typeCheckOnly) {
markBeanAsCreated(beanName);
}
try {
RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName);
checkMergedBeanDefinition(mbd, beanName, args);
// 保证初始化当前bean所依赖的bean。
String[] dependsOn = mbd.getDependsOn();
if (dependsOn != null) {
for (String dep : dependsOn) {
if (isDependent(beanName, dep)) {
throw new BeanCreationException(mbd.getResourceDescription(), beanName,
"Circular depends-on relationship between '" + beanName + "' and '" + dep + "'");
}
registerDependentBean(dep, beanName);
try {
getBean(dep);
}
catch (NoSuchBeanDefinitionException ex) {
throw new BeanCreationException(mbd.getResourceDescription(), beanName,
"'" + beanName + "' depends on missing bean '" + dep + "'", ex);
}
}
}
// Create bean instance.
if (mbd.isSingleton()) {
sharedInstance = getSingleton(beanName, () -> {
try {
// 再走一遍创建bean的流程
// 循环依赖中,依赖第二个bean的时候,找不到,然后创建一个
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 = getObjectForBeanInstance(sharedInstance, name, beanName, mbd);
}
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);
}
else {
String scopeName = mbd.getScope();
if (!StringUtils.hasLength(scopeName)) {
throw new IllegalStateException("No scope name defined for bean ´" + beanName + "'");
}
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());
}
}
return (T) bean;
}
首先,发现调用了getSingleton(beanName)方法
Object sharedInstance = getSingleton(beanName);
我们继续进入getSingleton(beanName)
方法,发现是空方法, 然后直接调用了getSingleton(beanName, true)
方法,具体源码如下,在DefaultSingletonBeanRegistry类中
/**
* 返回在给定名称下注册的(raw)单例对象。
* 检查已经实例化的单例,并且允许早期
* 对当前创建的单例对象的引用(解决循环引用)。
* Return the (raw) singleton object registered under the given name.
* Checks already instantiated singletons and also allows for an early
* reference to a currently created singleton (resolving a circular reference).
* @param beanName the name of the bean to look for
* @param allowEarlyReference whether early references should be created or not
* @return the registered singleton object, or {@code null} if none found
*/
@Nullable
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对象工厂并得到返回对象(三级缓存)
ObjectFactory<?> singletonFactory = this.singletonFactories.get(beanName);
if (singletonFactory != null) {
singletonObject = singletonFactory.getObject();
// 放到早期单例对象的缓存
this.earlySingletonObjects.put(beanName, singletonObject);
// 清除单例工厂的缓存
this.singletonFactories.remove(beanName);
}
}
}
}
return singletonObject;
}
这里重点分析一下getSingleton(beanName, true)
方法,因为循环依赖解决的核心就在这里处理的。我到这里取消了其他的所有断点,只打了一个断点在getSingleton()方法中,并设置了debug条件beanName.equals(“indexService”),然后直接debug重启项目,走到这里,就是当前的IndexService执行了
断点到这里,你会发现,debug直接走不了第一个判断,我们发现,根据beanName从单例池中获取的获取的单例对象为空,然后直接返回了
Object singletonObject = this.singletonObjects.get(beanName);
到这里需要注意,因为有很多bean,断点非常容易乱,我到这里是整理了断点,并将断点设置在了doGetBean()方法的的下一句判断
当前位置:AbstractBeanFactory.doGetBean()
我们这里从单例池拿到的IndexService实例为空,继续执行下面的代码,这里对于循环依赖不是很核心,以下代码就简单说明一下作用,直接跳过了:
if (isPrototypeCurrentlyInCreation(beanName)) {
throw new BeanCurrentlyInCreationException(beanName);
}
// 检查此工厂中是否存在bean定义
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);
}
}
if (!typeCheckOnly) {
markBeanAsCreated(beanName);
}
try {
RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName);
// 检查给定的合并bean定义
checkMergedBeanDefinition(mbd, beanName, args);
// 保证初始化当前bean所依赖的bean。
String[] dependsOn = mbd.getDependsOn();
if (dependsOn != null) {
for (String dep : dependsOn) {
if (isDependent(beanName, dep)) {
throw new BeanCreationException(mbd.getResourceDescription(), beanName,
"Circular depends-on relationship between '" + beanName + "' and '" + dep + "'");
}
registerDependentBean(dep, beanName);
try {
getBean(dep);
}
catch (NoSuchBeanDefinitionException ex) {
throw new BeanCreationException(mbd.getResourceDescription(), beanName,
"'" + beanName + "' depends on missing bean '" + dep + "'", ex);
}
}
}
/**
* 返回在给定名称下注册的(raw)单例对象。
* 检查已经实例化的单例,并且允许早期
* 对当前创建的单例对象的引用(解决循环引用)。
*/
protected Object getSingleton(String beanName, boolean allowEarlyReference) {
}
/**
* 返回注册在给定名称下的(raw)单例对象,
* 如果还没有注册,创建并注册一个新的。
*/
public Object getSingleton(String beanName, ObjectFactory<?> singletonFactory) {
}
以下,是doGetBean()方法调用的第五段核心代码
// 创建bean实例
if (mbd.isSingleton()) {
sharedInstance = getSingleton(beanName, () -> {
try {
// 再走一遍创建bean的流程
// 循环依赖中,依赖第二个bean的时候,找不到,然后创建一个
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 = getObjectForBeanInstance(sharedInstance, name, beanName, mbd);
}
然后断点进入DefaultSingletonBeanRegistry.getSingleton()方法
/**
* 返回注册在给定名称下的(raw)单例对象,
* 如果还没有注册,创建并注册一个新的。
* @param beanName the name of the bean
* @param singletonFactory the ObjectFactory to lazily create the singleton
* with, if necessary
* @return the registered singleton object
*/
public Object getSingleton(String beanName, ObjectFactory<?> singletonFactory) {
Assert.notNull(beanName, "Bean name must not be null");
synchronized (this.singletonObjects) {
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 + "'");
}
// 将当前对象设置为创建中
beforeSingletonCreation(beanName);
boolean newSingleton = false;
boolean recordSuppressedExceptions = (this.suppressedExceptions == null);
if (recordSuppressedExceptions) {
this.suppressedExceptions = new LinkedHashSet<>();
}
try {
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;
}
afterSingletonCreation(beanName);
}
if (newSingleton) {
addSingleton(beanName, singletonObject);
}
}
return singletonObject;
}
}
一样的,首先,单例池中拿,当然,我们还是拿不到IndexService
Object singletonObject = this.singletonObjects.get(beanName);
然后,通过beforeSingletonCreation(beanName);
将当前对象设置为创建中,还调用了单例创建后的回调,afterSingletonCreation(beanName);
,后面的代码就调用了这两个方法,debug就返回到了doGetBean()中
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 + "'");
}
// 将当前对象设置为创建中
beforeSingletonCreation(beanName);
boolean newSingleton = false;
boolean recordSuppressedExceptions = (this.suppressedExceptions == null);
if (recordSuppressedExceptions) {
this.suppressedExceptions = new LinkedHashSet<>();
}
try {
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;
}
// 单例创建后的回调
afterSingletonCreation(beanName);
}
if (newSingleton) {
addSingleton(beanName, singletonObject);
}
}
return singletonObject;
到此为止,第五段中调用的getSingleton(String beanName, ObjectFactory> singletonFactory)
方法已经完成,函数式方法中有一个回调方法,createBean(beanName, mbd, args)
方法,直接断点到回调,继续看内部执行的方法, 因为我们的属性注入都还没有操作,也就是没有解决循环依赖。在AbstractAutowireCapableBeanFactory类中
/**
*此类的中央方法:创建一个bean实例,
*填充bean实例,应用后处理器等。
* @see #doCreateBean
*/
@Override
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;
//确保此时确实解析了bean类,并且
//在动态解析的类的情况下克隆bean定义
//无法存储在共享的合并bean定义中。
Class<?> resolvedClass = resolveBeanClass(mbd, beanName);
if (resolvedClass != null && !mbd.hasBeanClass() && mbd.getBeanClassName() != null) {
mbdToUse = new RootBeanDefinition(mbd);
mbdToUse.setBeanClass(resolvedClass);
}
// 准备方法替代。
try {
mbdToUse.prepareMethodOverrides();
}
catch (BeanDefinitionValidationException ex) {
throw new BeanDefinitionStoreException(mbdToUse.getResourceDescription(),
beanName, "Validation of method overrides failed", ex);
}
try {
// 给BeanPostProcessors一个返回代理而不是目标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 {
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);
}
}
这个方法除了做了简单的操作外,核心还是调用doCreateBean(beanName, mbdToUse, args)
方法创建bean,并返回bean对象
try {
Object beanInstance = doCreateBean(beanName, mbdToUse, args);
if (logger.isTraceEnabled()) {
logger.trace("Finished creating instance of bean '" + beanName + "'");
}
return beanInstance;
}
然后我们进入方法doCreateBean(beanName, mbdToUse, args)
方法
/**
* 实际创建指定的bean。预创建处理已经发生
* 此时,例如检查{@code postProcessBeforeInstantiation}回调。
* 区分默认的bean实例化和使用
* 工厂方法,并自动装配构造函数。
*
* @param beanName bean的名称
* @param mbd 合并bean定义
* @param args 用于构造函数或工厂方法调用的显式参数
* @return Bean的新实例
* @throws BeanCreationException 如果无法创建该bean
* @see #instantiateBean
* @see #instantiateUsingFactoryMethod
* @see #autowireConstructor
*/
protected Object doCreateBean(String beanName, RootBeanDefinition mbd, @Nullable Object[] args)
throws BeanCreationException {
// 实例化bean。
BeanWrapper instanceWrapper = null;
if (mbd.isSingleton()) {
instanceWrapper = this.factoryBeanInstanceCache.remove(beanName);
}
if (instanceWrapper == null) {
// 实例化对象
// 第二次调用后置处理器
instanceWrapper = createBeanInstance(beanName, mbd, args);
}
// 循环依赖第二次这里这里拿到的是对象(半成品)
Object bean = instanceWrapper.getWrappedInstance();
Class<?> beanType = instanceWrapper.getWrappedClass();
if (beanType != NullBean.class) {
mbd.resolvedTargetType = beanType;
}
// 允许后处理器修改合并的bean定义。
synchronized (mbd.postProcessingLock) {
if (!mbd.postProcessed) {
try {
// 应用合并BeanDefinition后置处理器
// 第三次调用后置处理器
applyMergedBeanDefinitionPostProcessors(mbd, beanType, beanName);
}
catch (Throwable ex) {
throw new BeanCreationException(mbd.getResourceDescription(), beanName,
"Post-processing of merged bean definition failed", ex);
}
mbd.postProcessed = true;
}
}
//判断是否允许循环依赖
// isSingletonCurrentlyInCreation(beanName) 放入缓存中
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");
}
// 第四次调用后置处理器
// 判断是否需要aop
// 放入缓存中,然后继续执行生命周期
addSingletonFactory(beanName, () -> getEarlyBeanReference(beanName, mbd, bean));
}
// 初始化bean实例。
Object exposedObject = bean;
try {
//属性赋值
populateBean(beanName, mbd, instanceWrapper);
// 又会调用getBean方法,
//初始化bean
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) {
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 " +
"'getBeanNamesForType' with the 'allowEagerInit' flag turned off, for example.");
}
}
}
}
// 必要时注册一次性Bean
try {
registerDisposableBeanIfNecessary(beanName, bean, mbd);
}
catch (BeanDefinitionValidationException ex) {
throw new BeanCreationException(
mbd.getResourceDescription(), beanName, "Invalid destruction signature", ex);
}
return exposedObject;
}
我们分析一下当前的doCreateBean()中做了些什么事,首先通过createBeanInstance()方法创建了IndexService的实例对象,这里可以看见控制台输出了
IndexService Constructor ...
然后通过applyMergedBeanDefinitionPostProcessors()调用相关后置处理器,继续往下走,有一个判断,判断是否允许循环依赖,如果允许循环依赖(spring默认是允许的),则调用addSingletonFactory()方法,将当前的IndexService放入singletonObjects单例池(一级缓存),然后从earlySingletonObjects早期单例对象缓存(二级缓存)中移出,相关源码在DefaultSingletonBeanRegistry类中
/**
*添加给定的单例工厂以构建指定的单例
*如有必要。
* To be called for eager registration of singletons, e.g. to be able to
* resolve circular references.
* @param beanName the name of the bean
* @param singletonFactory the factory for the singleton object
*/
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);
}
}
}
继续debug回到doCreateBean()中, 往下走,就是熟悉的代码了,调用了populateBean()方法,源码我就不粘贴了,在AbstractAutowireCapableBeanFactory类中
populateBean(beanName, mbd, instanceWrapper);
这不就是spring bean生命周期的第二阶段嘛,属性赋值,因为我们马上要给IndexService中注入UserService了。populateBean()方法中,先调用相关后置处理器一顿操作结束后,注意,我这里是用debug条件beanName.equals(“indexService”)进入的,源中通过getBeanPostProcessors()拿到所有BeanPostProcessors处理器,然后遍历调用,我们断点找到AutowiredAnnotationBeanPostProcessor处理器的循环,spring中处理@Autowired注入的是AutowiredAnnotationBeanPostProcessor处理器
spring底层通过AutowiredAnnotationBeanPostProcessor处理器发现IndexService中,需要注入UserService,然后会调用AbstractBeanFactory.doGetBean()方法, 我提前在doGetBean()中打了断点,发现确实调用了getBean(userService)
然后就是常规操作了,接着走IndexService走过的路线,下面我们继续走UserService
第五段最后调用
bean = getObjectForBeanInstance(sharedInstance, name, beanName, mbd);
最终拿到IndexService的Bean,到此,实际上分析的是bean的创建过程,但是循环依赖就是在bean创建过程中解决的
炸了
若果判断从单例池中获取的实例不为空,则将当前的IndexService设置为正在创建中,调用的相关方法为isSingletonCurrentlyInCreation(beanName)
,这里就不详细分析这个方法了,就是将beanName放入DefaultSingletonBeanRegistry的Set
中,
创建完成后,发现需要UserService,又会调用getBean(UserService)方法,这里我就不粘贴代码了,和第一次IndexService实例化一样的,首先调用 getSingleton(beanName)方法,拿不到,然后检查此工厂中是否存在UserService对象的定义,又将指定的UserService对象标记为已经创建(或即将创建),到这里,实际上UserService还是一个普通的对象,不是bean,代码继续往下走,又到了getSingleton()方法, getSingleton()算是解决循环依赖的核心方法了
继续分析UserService执行到getSingleton()方法,先从单例池拿,拿不到,然后将UserService对象设置为创建中,这个时候,当前正在创建的bean的名称有两个,indexService和userService
DefaultSingletonBeanRegistry类中
/** 当前正在创建的bean的名称。 */
private final Set<String> singletonsCurrentlyInCreation =
Collections.newSetFromMap(new ConcurrentHashMap<>(16));
调用完getSingleton()之后,继续调用createBean,我们这里创建的是UserService,然后和IndexService走一样的流程,我们继续走UserService的生命周期,走到populateBean()时,调用到AutowiredAnnotationBeanPostProcessor处理器时,发现UserService需要注入IndexService,然后spring底层又调用getBean(indexService),到这了有些人懵逼了,逻辑图走一下
总的大纲,我们现在是在处理IndexService的生命周期,发现需要UserService的bean,spring中又没有UserService的bean,然后开始创建UserService对象,然后走UserService的生命周期,又发现需要注入IndexService的bean
debug在doGetBean()中打一个断点,F8直接到断点,看截图,没骗你吧,UserService又来获取IndexService了
接着往下走,进入getSingleton()方法,现在是UserService需要获取IndexService的Bean并进行注入,getSingleton()方法核心代码
/**
* 返回在给定名称下注册的(raw)单例对象。
* 检查已经实例化的单例,并且允许早期
* 对当前创建的单例对象的引用(解决循环引用)。
* Return the (raw) singleton object registered under the given name.
* Checks already instantiated singletons and also allows for an early
* reference to a currently created singleton (resolving a circular reference).
* @param beanName the name of the bean to look for
* @param allowEarlyReference whether early references should be created or not
* @return the registered singleton object, or {@code null} if none found
*/
@Nullable
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对象工厂并得到返回对象(三级缓存)
ObjectFactory<?> singletonFactory = this.singletonFactories.get(beanName);
if (singletonFactory != null) {
singletonObject = singletonFactory.getObject();
// 放到早期单例对象的缓存
this.earlySingletonObjects.put(beanName, singletonObject);
// 清除单例工厂的缓存
this.singletonFactories.remove(beanName);
}
}
}
}
return singletonObject;
}
到这里,UserService就拿到了IndexService的半成品,然后走完了UserService的生命周期,并且调用的方法一路返回,返回,… 之后,IndexService就拿到了UserService,所以IndexService也是一个成品的Bean,至此,spring循环依赖已经解决完成
最终,UserService填充好了IndexService,然后调用方法返回后,IndexService也填充好了UserService