Spring Boot Starter 自動裝配原理全解析
Spring Boot Starter 的核心設(shè)計理念是 約定優(yōu)于配置,其核心實(shí)現(xiàn)基于 自動配置(Auto-Configuration) 和 條件化注冊(Conditional Registration)。以下是其生效原理:
約定大于配置
通過預(yù)定義合理的默認(rèn)行為和規(guī)范,減少開發(fā)者需要手動配置的步驟。比較顯著的變化就是減少XML配置。還有一些實(shí)際體現(xiàn)如下所示:
- 項(xiàng)目結(jié)構(gòu)約定
- 默認(rèn)目錄結(jié)構(gòu):如
src/main/java存放代碼,src/main/resources存放配置文件。 - 配置文件命名:
application.properties或application.yml自動被加載,無需顯式指定路徑。
- 默認(rèn)目錄結(jié)構(gòu):如
- 自動配置(Auto-Configuration)
- 條件化 Bean 注冊:根據(jù)類路徑依賴(如存在
DataSource類)自動配置數(shù)據(jù)庫連接池。 - 默認(rèn)參數(shù)值:如嵌入式 Tomcat 默認(rèn)端口為
8080,無需手動指定。
- 條件化 Bean 注冊:根據(jù)類路徑依賴(如存在
- Starter 依賴
- 依賴聚合:引入
spring-boot-starter-web即自動包含 Web 開發(fā)所需的所有依賴(如 Tomcat、Jackson、Spring MVC)。 - 開箱即用:無需手動管理版本兼容性。
- 依賴聚合:引入
- RESTful 路由映射
- 注解驅(qū)動:通過
@GetMapping("/path")即可定義接口,無需在 XML 中配置路由規(guī)則。
- 注解驅(qū)動:通過
自動配置機(jī)制
觸發(fā)階段:@EnableAutoConfiguration
- 應(yīng)用啟動時,
@SpringBootApplication組合了@EnableAutoConfiguration,觸發(fā)自動配置流程。 AutoConfigurationImportSelector被調(diào)用,負(fù)責(zé)加載所有候選的自動配置類。
public String[] selectImports(AnnotationMetadata metadata) {
// 1. 加載所有候選自動配置類
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 對應(yīng)的配置類。在 Spring Boot 3.x 中,自動配置類的加載方式從 spring.factories 過渡到 AutoConfiguration.imports,并引入了 ImportCandidates 類來處理這一變化。
- 去重與過濾
移除重復(fù)的配置類,并通過條件注解(如 @ConditionalOnClass ,@ConditionalOnMissingBean ) 有選擇的保留當(dāng)前環(huán)境的配置類。
- @ConditionalOnClass:類路徑存在指定類時生效
- @ConditionalOnMissingBean:容器中不存在指定 Bean 時生效
- @ConditionalOnProperty:配置屬性匹配時生效
排序
根據(jù) @AutoConfigureOrder 或 @AutoConfigureAfter 調(diào)整配置類的加載順序。
Bean 注冊
- 篩選后的自動配置類被解析為標(biāo)準(zhǔn)的
@Configuration類。 - 每個配置類中的
@Bean方法根據(jù)條件注解動態(tài)注冊 Bean 到 Spring 容器。
編寫自定義Spring Boot Starter
項(xiàng)目結(jié)構(gòu)規(guī)劃
建議分為兩個模塊:
- 自動配置模塊:包含核心邏輯和自動配置類(如
hello-spring-boot-autoconfigure)。 - Starter模塊:空項(xiàng)目,僅作為依賴聚合(如
hello-spring-boot-starter)。
hello-spring-boot-starter-parent(父POM) ├── hello-spring-boot-autoconfigure(自動配置模塊) └── 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)建自動配置模塊(hello-spring-boot-autoconfigure)
添加Maven依賴
<!-- pom.xml -->
<dependencies>
<!-- Spring Boot 自動配置基礎(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; }
}編寫自動配置類
@Configuration
@EnableConfigurationProperties(HelloProperties.class) // 啟用配置屬性
@ConditionalOnClass(HelloService.class) // 當(dāng)HelloService在類路徑時生效
public class HelloAutoConfiguration {
@Bean
@ConditionalOnMissingBean // 當(dāng)用戶未自定義HelloService時生效
public HelloService helloService(HelloProperties properties) {
HelloService service = new HelloService();
service.setMessage(properties.getMessage());
return service;
}
}注冊自動配置
在 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>
<!-- 引入自動配置模塊 -->
<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 自動裝配原理的文章就介紹到這了,更多相關(guān)Spring Boot Starter 自動裝配內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Spring Boot 自動裝配原理及 Starter 實(shí)現(xiàn)原理解析
- SpringBoot詳細(xì)分析自動裝配原理并實(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的錯誤問題
- Springboot整合spring-boot-starter-data-elasticsearch的過程
- SpringBoot的父級依賴:spring-boot-starter-parent詳解
相關(guān)文章
Java?設(shè)計模式以虹貓藍(lán)兔的故事講解簡單工廠模式
簡單工廠模式是屬于創(chuàng)建型模式,又叫做靜態(tài)工廠方法(Static Factory Method)模式,但不屬于23種GOF設(shè)計模式之一。簡單工廠模式是由一個工廠對象決定創(chuàng)建出哪一種產(chǎn)品類的實(shí)例。簡單工廠模式是工廠模式家族中最簡單實(shí)用的模式,可以理解為是不同工廠模式的一個特殊實(shí)現(xiàn)2022-03-03
javaSE泛型、反射與注解的核心原理與實(shí)際應(yīng)用
在日常開發(fā)中,必不可少的會使用到泛型,這篇文章主要介紹了javaSE泛型、反射與注解的核心原理與實(shí)際應(yīng)用的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2025-08-08
Java經(jīng)典設(shè)計模式之適配器模式原理與用法詳解
這篇文章主要介紹了Java經(jīng)典設(shè)計模式之適配器模式,簡單說明了適配器模式的概念、原理,并結(jié)合實(shí)例形式分析了java適配器模式的用法與相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-08-08

