Spring5.0原始碼學習系列之淺談迴圈依賴問題
前言介紹
在上一章的學習中,我們對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的迴圈依賴原因:使用了三級快取