Spring5.0源碼學習系列之淺談循環依賴問題
前言介紹
附錄:Spring源碼學習專欄
在上一章的學習中,我們對Bean的創建有了一個粗略的了解,接着本文淺談Spring循環依賴問題,這是一個面試比較常見的問題
1、什麼是循環依賴?
所謂的循環依賴就是兩個以及兩個以上的類互相調用依賴,形成閉環
// 類A依賴於B
class A{
public B b;
}
// 類B依賴了C
class B{
public C c;
}
// 類C依賴了A
class C{
public A a;
}

然後?看起來是很正常的,我們隨便new一個類,循環依賴的類都是能正常調用的
A a = new A();
System.out.println(a);
為什麼?因為這種情況,A.java->A.class,我們new就能獲取到實例的對象,這個通過jvm支持的,jdk是能支持這種情況的,不過本文不詳細說明,本文要討論的Spring中的bean,循環依賴在Spring中就是一個問題了
為什麼?首先回顧一下之前的知識點,首先在Spring框架中類的創建都是給Spring IOC容器創建的,如圖:

然後?真的出現這種情況,會怎麼樣?

Spring框架檢測到這種場景會拋 BeanCurrentlyInCreationException,提前暴露對象的方法,因為Spring創建bean的過程是一個很複雜的過程,首先是xml解析為document對象,document對象再轉成BeanDefinition,然後進行bean的生命周期,才算得上是一個真正的spring bean,接着進行後置處理器加工,假如出現這種,設想一下會怎麼樣?spring容器就會一直循環調用,當然是在特定的條件,為什麼說是特定情況?請看下文
2、實驗環境準備
實驗環境:
- SpringFramework版本
- Springframework5.0.x
- 開發環境
- JAR管理:gradle 4.9/ Maven3.+
- 開發IDE:IntelliJ IDEA 2018.2.5
- JDK:jdk1.8.0_31
- Git Server:Git fro window 2.8.3
- Git Client:SmartGit18.1.5(可選)
3、循環依賴問題
我們可以通過例子進行驗證,創建類A:
package com.example.bean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
* <pre>
* A class
* </pre>
*
* <pre>
* @author mazq
* 修改記錄
* 修改後版本: 修改人: 修改日期: 2020/11/05 10:31 修改內容:
* </pre>
*/
@Component
public class A {
//@Autowired
B b;
public A() {
b = new B();
System.out.println("A class is create");
}
}
類B:
package com.example.bean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
* <pre>
* B class
* </pre>
*
* <pre>
* @author mazq
* 修改記錄
* 修改後版本: 修改人: 修改日期: 2020/11/16 14:03 修改內容:
* </pre>
*/
@Component
public class B {
//@Autowired
A a;
public B() {
a = new A();
System.out.println("B class is create");
}
}
註冊類A、B
package com.example.config;
import com.example.bean.B;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.example.bean.A;
/**
* <pre>
* AppConfiguration
* </pre>
*
* <pre>
* @author mazq
* 修改記錄
* 修改後版本: 修改人: 修改日期: 2020/11/05 10:26 修改內容:
* </pre>
*/
@Configuration
public class AppConfiguration {
@Bean
public A a(){
return new A();
}
@Bean
public B b() {
return new B();
}
}
package com.example;
import com.example.config.AppConfiguration;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.example.bean.A;
/**
* <pre>
* TestController
* </pre>
*
* <pre>
* @author mazq
* 修改記錄
* 修改後版本: 修改人: 修改日期: 2020/11/05 10:22 修改內容:
* </pre>
*/
public class TestApplication {
public static void testCircularReferences() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.register(AppConfiguration.class);
//context.setAllowCircularReferences(false);
context.refresh();
A bean = context.getBean(A.class);
System.out.println(bean);
}
public static void main(String[] args) {
// 測試Sprin循環依賴
testCircularReferences();
}
}
經過測試,一直在循環調用:

4、循環依賴解決方法
對於這種情況,Spring有處理方法?答案是有的,方法就是通過@Autowired註解,當然bean要是單例的,多例的情況不支持,原因後面分析
@Component
public class A {
@Autowired
B b;
public A() {
System.out.println("A class is create");
}
}

補充:除了
@Autowired方法,我們還可以通過set方法處理循環依賴問題,當然也是僅支持單例bean,多例的情況不支持
5、關閉Spring循環依賴
有個疑問?Spring的循環依賴支持,默認情況是開啟?是否有什麼開關控制?通過源碼學習,可以通過setAllowCircularReferences設置
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.register(AppConfiguration.class);
// 關閉Spring循環依賴支持
context.setAllowCircularReferences(false);
context.refresh();
通過測試,設置不開啟這個屬性的時候,即使加上@Autowired,代碼還是拋異常了
6、prototype(多例)循環依賴
在多例的情況,Spring能支持循環依賴?加上@Scope("prototype"),將bean變成多例的

經過測試:多例的情況會拋出異常,即使加上了@Autowired,原因請看下文
7、Spring循環依賴特徵
ok,經過前面例子的驗證,到這來,可以對Spring的循環依賴特點進行歸納:
- Spring中的循環依賴場景
- 構造器的循環依賴,通過構造函數
- Field屬性的循環依賴,通過set方法
- Spring的循環依賴是默認開啟的(setAllowCircularReferences)
- Spring對單例和多例Bean的支持
- 單例Bean(singleton) :只能通過
@Autowired和set方法支持 - 多例Bean(prototype):默認不支持,直接拋異常
BeanCurrentlyInCreationException
- 單例Bean(singleton) :只能通過
8、Spring循環依賴原理
我們通過實驗進行了驗證,也歸納出了Spring循環依賴的特點,然後具體原因是什麼?我們只能通過源碼學習得到答案
在上一章的學習中,我們對Bean的創建有了一個粗略的了解,所以,順着這條路線,跟下源碼:
在前面的學習,我們知道了{@link org.springframework.beans.factory.support.AbstractBeanFactory#doGetBean}這個方法就是Spring Bean創建的真正執行方法
protected <T> T doGetBean(
String name, @Nullable Class<T> requiredType, @Nullable Object[] args, boolean typeCheckOnly)
throws BeansException {
// 處理BeanName,前面說的FactoryBean帶『&』符號,要在這裡進行轉換
String beanName = transformedBeanName(name);
Object bean;
// Eagerly check singleton cache for manually registered singletons.
// 從map(singletonObjects)里獲取單例bean,確定是否已經存在對應實例
Object sharedInstance = getSingleton(beanName);
if (sharedInstance != null && args == null) {
if (logger.isDebugEnabled()) {
if (isSingletonCurrentlyInCreation(beanName)) {
logger.debug("Returning eagerly cached instance of singleton bean '" + beanName +
"' that is not fully initialized yet - a consequence of a circular reference");
}
else {
logger.debug("Returning cached instance of singleton bean '" + beanName + "'");
}
}
// 兩種情況:普通的bean,直接從singletonObjects返回sharedInstance
//如果是FactoryBean,返回其創建的對象實例
bean = getObjectForBeanInstance(sharedInstance, name, beanName, null);
}
else {
// Fail if we're already creating this bean instance:
// We're assumably within a circular reference.
// 校驗是否是多例(Prototype)的Bean,多例的bean是不支持循環依賴的
// 為了避免循環依賴,遇到這種情況,直接拋出異常
if (isPrototypeCurrentlyInCreation(beanName)) {
throw new BeanCurrentlyInCreationException(beanName);
}
// Check if bean definition exists in this factory.
// 檢查BeanFactory是否存在這個BeanDefinition
BeanFactory parentBeanFactory = getParentBeanFactory();
if (parentBeanFactory != null && !containsBeanDefinition(beanName)) {
// Not found -> check parent.
// 當前容器找不到BeanDefinition,去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.
// 返回parent容器的查詢結果
return (T) parentBeanFactory.getBean(nameToLookup, args);
}
else {
// No args -> delegate to standard getBean method.
return parentBeanFactory.getBean(nameToLookup, requiredType);
}
}
if (!typeCheckOnly) {
//typeCheckOnly為false的情況,將beanName放在一個alreadyCreated的集合
markBeanAsCreated(beanName);
}
try {
RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName);
checkMergedBeanDefinition(mbd, beanName, args);
// Guarantee initialization of beans that the current bean depends on.
// 校驗是否配置了 depends-on
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.
// 單例的Bean
if (mbd.isSingleton()) {
sharedInstance = getSingleton(beanName, () -> {
try {
// 創建單例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);
}
// 多例的Bean,scope = protoType
else if (mbd.isPrototype()) {
// It's a prototype -> create a new instance.
Object prototypeInstance = null;
try {
// 多例的情況,創建bean之前添加標記(用於循環依賴校驗)
beforePrototypeCreation(beanName);
// 執行多例Bean創建
prototypeInstance = createBean(beanName, mbd, args);
}
finally {
// 創建原型(多例)bean之後擦除標記
afterPrototypeCreation(beanName);
}
bean = getObjectForBeanInstance(prototypeInstance, name, beanName, mbd);
}
// 如果不是單例bean也不是多例的bean,委託給對應的實現類
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 {
// 執行bean創建
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.isDebugEnabled()) {
logger.debug("Failed to convert bean '" + name + "' to required type '" +
ClassUtils.getQualifiedName(requiredType) + "'", ex);
}
throw new BeanNotOfRequiredTypeException(name, requiredType, bean.getClass());
}
}
return (T) bean;
}
- 源碼比較複雜,所以可以帶着疑問來跟,首先以單例Bean的情況:#doGetBean.getSingleton


protected Object getSingleton(String beanName, boolean allowEarlyReference) {
// 一級緩存:singletonObjects (單例池)
Object singletonObject = this.singletonObjects.get(beanName);
if (singletonObject == null && isSingletonCurrentlyInCreation(beanName)) {
synchronized (this.singletonObjects) {
// 二級緩存:earlySingletonObjects(BeanDefinition還沒進行屬性填充)
singletonObject = this.earlySingletonObjects.get(beanName);
if (singletonObject == null && allowEarlyReference) {
// 三級緩存:singletonFactories
ObjectFactory<?> singletonFactory = this.singletonFactories.get(beanName);
if (singletonFactory != null) {
singletonObject = singletonFactory.getObject();
this.earlySingletonObjects.put(beanName, singletonObject);
this.singletonFactories.remove(beanName);
}
}
}
}
return singletonObject;
}
在某些情況,循環依賴會造成循環調用,所以需要怎麼解決?

Spring框架的方法是使用了三級緩存,其實最關鍵的是earlySingletonObjects
- 一級緩存:singletonObjects,這是Spring BeanDefinition的單例池,首先只保存單例Bean的BeanDefinition,而且這個Bean是一個真正的bean,也就是進行過屬性填充的
- 二級緩存:earlySingletonObjects,early從單詞意思來說,這個緩存是在singletonObjects之前的,也就是BeanDefinition還沒進行屬性填充等等操作,Spring引入這個緩存的目的就是為了處理單例bean的循環依賴問題
- 三級緩存:singletonFactories,緩存的是ObjectFactory,表示對象工廠,為什麼要加上這個緩存?原因比較複雜,涉及到AOP等等原因,因為我還沒理解清楚,所以本文不說明
加上了earlySingletonObjects緩存之後,Spring就能支持單例bean的循環依賴,參考語雀某大佬的筆記,畫圖表示:

- 帶着疑問來跟一下多例Bean的情況:
Spring框架是不支持多例bean的循環依賴的,原因跟下代碼:#doGetBean
// Fail if we're already creating this bean instance:
// We're assumably within a circular reference.
// 校驗是否是多例(Prototype)的Bean,多例的bean是不支持循環依賴的
// 為了避免循環依賴,遇到這種情況,直接拋出異常
if (isPrototypeCurrentlyInCreation(beanName)) {
throw new BeanCurrentlyInCreationException(beanName);
}
多例的情況:看代碼是通過prototypesCurrentlyInCreation里的數據校驗的,prototypesCurrentlyInCreation是一個ThreadLocal對象
protected boolean isPrototypeCurrentlyInCreation(String beanName) {
Object curVal = this.prototypesCurrentlyInCreation.get();
return (curVal != null &&
(curVal.equals(beanName) || (curVal instanceof Set && ((Set<?>) curVal).contains(beanName))));
}
繼續找代碼,找到beforePrototypeCreation:
protected void beforePrototypeCreation(String beanName) {
Object curVal = this.prototypesCurrentlyInCreation.get();
if (curVal == null) {
this.prototypesCurrentlyInCreation.set(beanName);
}
else if (curVal instanceof String) {
Set<String> beanNameSet = new HashSet<>(2);
beanNameSet.add((String) curVal);
beanNameSet.add(beanName);
this.prototypesCurrentlyInCreation.set(beanNameSet);
}
else {
Set<String> beanNameSet = (Set<String>) curVal;
beanNameSet.add(beanName);
}
}
Ctrl+Alt+H,查看這個方法的調用棧:其實就是在#doGetBean就調用了,也就是bean創建之前

try {
// 多例的情況,創建bean之前添加標記(用於循環依賴校驗)
beforePrototypeCreation(beanName);
// 執行多例Bean創建
prototypeInstance = createBean(beanName, mbd, args);
}
finally {
// 創建原型(多例)bean之後擦除標記
afterPrototypeCreation(beanName);
}
bean = getObjectForBeanInstance(prototypeInstance, name, beanName, mbd);
知識點歸納
- Spring中的循環依賴場景
- 構造器的循環依賴,通過構造函數
- Field屬性的循環依賴,通過set方法
- Spring的循環依賴是默認開啟的(setAllowCircularReferences)
- Spring對單例和多例Bean的支持
- 單例Bean(singleton) :只能通過
@Autowired和set方法支持 - 多例Bean(prototype):默認不支持,直接拋異常
BeanCurrentlyInCreationException
- 單例Bean(singleton) :只能通過
- Spring支持單例bean的循環依賴原因:使用了三級緩存



