Spring Boot Starter 自動(dòng)裝配原理全解析
Spring Boot Starter 的核心設(shè)計(jì)理念是 約定優(yōu)于配置,其核心實(shí)現(xiàn)基于 自動(dòng)配置(Auto-Configuration) 和 條件化注冊(cè)(Conditional Registration)。以下是其生效原理:
約定大于配置
通過預(yù)定義合理的默認(rèn)行為和規(guī)范,減少開發(fā)者需要手動(dòng)配置的步驟。比較顯著的變化就是減少XML配置。還有一些實(shí)際體現(xiàn)如下所示:
- 項(xiàng)目結(jié)構(gòu)約定
- 默認(rèn)目錄結(jié)構(gòu):如
src/main/java
存放代碼,src/main/resources
存放配置文件。 - 配置文件命名:
application.properties
或application.yml
自動(dòng)被加載,無需顯式指定路徑。
- 默認(rèn)目錄結(jié)構(gòu):如
- 自動(dòng)配置(Auto-Configuration)
- 條件化 Bean 注冊(cè):根據(jù)類路徑依賴(如存在
DataSource
類)自動(dòng)配置數(shù)據(jù)庫連接池。 - 默認(rèn)參數(shù)值:如嵌入式 Tomcat 默認(rèn)端口為
8080
,無需手動(dòng)指定。
- 條件化 Bean 注冊(cè):根據(jù)類路徑依賴(如存在
- Starter 依賴
- 依賴聚合:引入
spring-boot-starter-web
即自動(dòng)包含 Web 開發(fā)所需的所有依賴(如 Tomcat、Jackson、Spring MVC)。 - 開箱即用:無需手動(dòng)管理版本兼容性。
- 依賴聚合:引入
- RESTful 路由映射
- 注解驅(qū)動(dòng):通過
@GetMapping("/path")
即可定義接口,無需在 XML 中配置路由規(guī)則。
- 注解驅(qū)動(dòng):通過
自動(dòng)配置機(jī)制
觸發(fā)階段:@EnableAutoConfiguration
- 應(yīng)用啟動(dòng)時(shí),
@SpringBootApplication
組合了@EnableAutoConfiguration
,觸發(fā)自動(dòng)配置流程。 AutoConfigurationImportSelector
被調(diào)用,負(fù)責(zé)加載所有候選的自動(dòng)配置類。
public String[] selectImports(AnnotationMetadata metadata) { // 1. 加載所有候選自動(dòng)配置類 List<String> configurations = getCandidateConfigurations(); // 2. 去重、過濾、排序 configurations = removeDuplicates(configurations); configurations = filter(configurations, autoConfigurationMetadata); return configurations.toArray(new String[0]); }
加載與篩選:spring.factories
- 加載所有候選配置類
從所有 META-INF/spring.factories
文件中讀取 EnableAutoConfiguration
對(duì)應(yīng)的配置類。在 Spring Boot 3.x 中,自動(dòng)配置類的加載方式從 spring.factories 過渡到 AutoConfiguration.imports,并引入了 ImportCandidates 類來處理這一變化。
- 去重與過濾
移除重復(fù)的配置類,并通過條件注解(如 @ConditionalOnClass
,@ConditionalOnMissingBean
) 有選擇的保留當(dāng)前環(huán)境的配置類。
- @ConditionalOnClass:類路徑存在指定類時(shí)生效
- @ConditionalOnMissingBean:容器中不存在指定 Bean 時(shí)生效
- @ConditionalOnProperty:配置屬性匹配時(shí)生效
排序
根據(jù) @AutoConfigureOrder
或 @AutoConfigureAfter
調(diào)整配置類的加載順序。
Bean 注冊(cè)
- 篩選后的自動(dòng)配置類被解析為標(biāo)準(zhǔn)的
@Configuration
類。 - 每個(gè)配置類中的
@Bean
方法根據(jù)條件注解動(dòng)態(tài)注冊(cè) Bean 到 Spring 容器。
編寫自定義Spring Boot Starter
項(xiàng)目結(jié)構(gòu)規(guī)劃
建議分為兩個(gè)模塊:
- 自動(dòng)配置模塊:包含核心邏輯和自動(dòng)配置類(如
hello-spring-boot-autoconfigure
)。 - Starter模塊:空項(xiàng)目,僅作為依賴聚合(如
hello-spring-boot-starter
)。
hello-spring-boot-starter-parent(父POM) ├── hello-spring-boot-autoconfigure(自動(dòng)配置模塊) └── hello-spring-boot-starter(Starter模塊)
hello-spring-boot-starter/ ├── hello-spring-boot-autoconfigure/ │ ├── src/ │ │ ├── main/ │ │ │ ├── java/com/example/autoconfigure/ │ │ │ │ ├── HelloAutoConfiguration.java │ │ │ │ ├── HelloProperties.java │ │ │ │ └── HelloService.java │ │ │ └── resources/ │ │ │ └── META-INF/ │ │ │ └── spring.factories │ │ └── test/ │ └── pom.xml ├── hello-spring-boot-starter/ │ └── pom.xml └── pom.xml
創(chuàng)建自動(dòng)配置模塊(hello-spring-boot-autoconfigure)
添加Maven依賴
<!-- pom.xml --> <dependencies> <!-- Spring Boot 自動(dòng)配置基礎(chǔ)依賴 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-autoconfigure</artifactId> <version>3.1.5</version> </dependency> <!-- 可選:配置屬性處理 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <version>3.1.5</version> <optional>true</optional> </dependency> </dependencies>
定義核心服務(wù)類
public class HelloService { private String message = "Hello, World!"; // 默認(rèn)消息 public String sayHello() { return message; } // Getter和Setter用于通過配置修改message public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } }
定義配置屬性類(可選)
@ConfigurationProperties(prefix = "hello") public class HelloProperties { private String message = "Hello, World!"; // Getter和Setter public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } }
編寫自動(dòng)配置類
@Configuration @EnableConfigurationProperties(HelloProperties.class) // 啟用配置屬性 @ConditionalOnClass(HelloService.class) // 當(dāng)HelloService在類路徑時(shí)生效 public class HelloAutoConfiguration { @Bean @ConditionalOnMissingBean // 當(dāng)用戶未自定義HelloService時(shí)生效 public HelloService helloService(HelloProperties properties) { HelloService service = new HelloService(); service.setMessage(properties.getMessage()); return service; } }
注冊(cè)自動(dòng)配置
在 resources/META-INF/
下創(chuàng)建 spring.factories
文件:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.example.autoconfigure.HelloAutoConfiguration
創(chuàng)建Starter模塊(hello-spring-boot-starter)
添加Maven依賴
<!-- pom.xml --> <dependencies> <!-- 引入自動(dòng)配置模塊 --> <dependency> <groupId>com.example</groupId> <artifactId>hello-spring-boot-autoconfigure</artifactId> <version>1.0.0</version> </dependency> </dependencies>
使用自定義Starter
在應(yīng)用中引入Starter依賴
<!-- 用戶項(xiàng)目的pom.xml --> <dependency> <groupId>com.example</groupId> <artifactId>hello-spring-boot-starter</artifactId> <version>1.0.0</version> </dependency>
在代碼中注入Bean
@RestController public class HelloController { @Autowired private HelloService helloService; @GetMapping("/hello") public String hello() { return helloService.sayHello(); } }
自定義配置(可選)
在 application.properties
中修改消息:
hello.message=你好, Spring Boot!
到此這篇關(guān)于Spring Boot Starter 自動(dòng)裝配原理的文章就介紹到這了,更多相關(guān)Spring Boot Starter 自動(dòng)裝配內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Spring Boot 自動(dòng)裝配原理及 Starter 實(shí)現(xiàn)原理解析
- SpringBoot詳細(xì)分析自動(dòng)裝配原理并實(shí)現(xiàn)starter
- SpringBoot多數(shù)據(jù)源解決方案:dynamic-datasource-spring-boot-starter
- SpringBoot利用dynamic-datasource-spring-boot-starter解決多數(shù)據(jù)源問題
- 解決mybatis-plus-boot-starter與mybatis-spring-boot-starter的錯(cuò)誤問題
- Springboot整合spring-boot-starter-data-elasticsearch的過程
- SpringBoot的父級(jí)依賴:spring-boot-starter-parent詳解
相關(guān)文章
springboot的maven多模塊混淆jar包的實(shí)現(xiàn)方法
springboot可以使用proguard-maven-plugin 這個(gè)插件 在 pom.xml 中自定義proguard 的指令,本文基于 springboot + maven + proguard 的maven多模塊架構(gòu)進(jìn)行代碼混淆,需要的朋友可以參考下2024-03-03jdk中動(dòng)態(tài)代理異常處理分析:UndeclaredThrowableException
最近在工作中遇到了報(bào)UndeclaredThrowableException的錯(cuò)誤,通過查找相關(guān)的資料,終于解決了,所以這篇文章主要給大家介紹了關(guān)于jdk中動(dòng)態(tài)代理異常處理分析:UndeclaredThrowableException的相關(guān)資料,需要的朋友可以參考下2018-04-04Spring?boot?admin?服務(wù)監(jiān)控利器詳解
這篇文章主要介紹了Spring?boot?admin?服務(wù)監(jiān)控利器詳解,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-08-08Java數(shù)據(jù)結(jié)構(gòu)實(shí)現(xiàn)二維數(shù)組與稀疏數(shù)組轉(zhuǎn)換詳解
稀疏數(shù)組是用于優(yōu)化,壓縮具有以下特點(diǎn)的二維數(shù)組:當(dāng)二維數(shù)組中的元素大部分相同,有意義的數(shù)據(jù)元素較少時(shí),可以使用稀疏數(shù)組進(jìn)行簡(jiǎn)化,節(jié)省存儲(chǔ)空間2021-10-10Spring Boot 中的 @ConditionalOnBean 注解作用及基
在 Spring Boot 中,@ConditionalOnBean 可以幫助我們根據(jù) 是否存在特定 Bean 來 動(dòng)態(tài)注冊(cè) Bean,廣泛用于 按需加載、自動(dòng)配置 等場(chǎng)景,本文給大家介紹Spring Boot 中的 @ConditionalOnBean 注解,感興趣的朋友一起看看吧2025-04-04