Spring Boot起步依賴原始碼分析(二)

2021-07-25 13:31:18 字數 1583 閱讀 2966

spring boot起步依賴原始碼分析(一)

上次說到enableautoconfigurationimportselector.getcandidateconfigurations方法匯入了meta-inf/spring.factories配置檔案,把配置資訊封裝到了乙個list集合中,接下來看下這個方法在什麼地方唄呼叫,這裡交大家乙個eclipse看原始碼的小技巧,在方法名上右鍵然後選擇open call hierarchy就可以快速找到什麼地方呼叫了這個方法。

@override

public string selectimports(annotationmetadata metadata)

try

catch (ioexception ex)

}

這段**需要跟大家分析分析了

1、getattributes(metadata);獲取到了@enableautoconfiguration(exclude={},excludename={})註解上的exclude和excludename

2、

set

exclusions = getexclusions(metadata, attributes);

configurations.removeall(exclusions);

@configuration

@conditionalonclass()

@conditionalo****singbean(webmvcconfigurationsupport.class)

@autoconfigureorder(ordered.highest_precedence + 10)

@autoconfigureafter(dispatcherservletautoconfiguration.class)

public class

webmvcautoconfiguration

看到這,如果你熟悉spring4.x的條件註解的話,上面的**應該就很情切了,這裡還是簡單提一下@conditional根據滿足某乙個條件建立乙個特定的bean。比如說當乙個jar包在乙個類路徑下時,自動配置乙個或多個bean,或者只有某個bean被建立時才會建立另乙個bean。spring boot中將大量使用到進行一些自動配置。舉個例子比如:

@conditionalonclass()
當我們的jar依賴也就是有我們的環境中存在servlet.class, dispatcherservlet.class,webmvcconfigureradapter.class這幾個類檔案,即條件滿足。

到這裡,你應該就明白了,spring boot 高大上的自動配置起步依賴是怎麼玩起來的,我們再來總結一下

1、spring boot啟動時回去匯入spring-boot-autoconfigure包下meta-inf/spring.factories配置檔案

2、根據***xautoconfiguration配置類的條件註解@conditional判斷當前環境中是否滿足***xautoconfiguration的註解條件,如果滿足,則自動向spring ioc 容器中注入bean

springboot的起步依賴

springboot讀取配置檔案的方式 1 讀取核心配置檔案 方式一 使用 value方式 常用 在 value的 中包含的是核心配置檔案中的鍵名 方式二 使用environment方式 這種方式是依賴注入evnironment來完成,在建立的成員變數private environment env上...

Spring boot相關依賴

org.springframework.boot spring boot starter parent 2.1.4.release 1 概述 2 作用 控制了所有依賴的版本號 3 好處 以後我們匯入依賴預設是不需要寫版本 org.springframework.boot spring boot st...

Springboot迴圈依賴

如何解決迴圈依賴 最好的方法是重構 進行解耦 從網上搜尋了簡單的方法如下 第一種 專案解決用了這種方式 autowired lazy true private classa classa autowired lazy true private classb classb 在你注入bean時,在互相依...