SpringBoot中自定義Starter的完整教程
前言
在 Spring Boot 中,自定義 Starter 是一種非常強大的方式,可以將常用的功能模塊封裝成可復用的組件。本文將詳細介紹如何創(chuàng)建一個自定義 Starter,并提供完整的使用流程和代碼示例。
一、自定義 Starter 原理
Spring Boot 的自動配置機制依賴于 META-INF/spring.factories 文件中的 EnableAutoConfiguration 配置項。當項目啟動時,Spring Boot 會掃描所有 META-INF/spring.factories 文件,加載其中指定的自動配置類。
啟動流程圖解:
starter → autoconfigure → spring-boot-starter
↑
└─ META-INF/spring.factories
starter:Maven/Gradle 依賴包(啟動器)autoconfigure:包含自動配置邏輯的包spring.factories:配置自動配置類的入口文件
二、項目結(jié)構(gòu)設(shè)計
我們以一個簡單的“Hello World”功能為例,創(chuàng)建一個名為 atguigu-hello-spring-boot-starter 的自定義 Starter。
模塊劃分
├── atguigu-hello-spring-boot-starter (Starter)
│ ├── pom.xml
│ └── src/main/java/com/atguigu/hello/HelloService.java
│
├── atguigu-hello-spring-boot-starter-autoconfigure (Autoconfigure)
│ ├── pom.xml
│ ├── src/main/java/com/atguigu/hello/config/HelloAutoConfiguration.java
│ ├── src/main/java/com/atguigu/hello/config/HelloProperties.java
│ └── src/main/resources/META-INF/spring.factories
三、代碼實現(xiàn)
1. 創(chuàng)建 Autoconfigure 模塊
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.atguigu</groupId>
<artifactId>atguigu-hello-spring-boot-starter-autoconfigure</artifactId>
<version>1.0.0</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.1.5</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
</dependencies>
</project>
HelloProperties.java(配置屬性類)
package com.atguigu.hello.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "hello")
public class HelloProperties {
private String message = "Hello, World!";
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
HelloAutoConfiguration.java(自動配置類)
package com.atguigu.hello.config;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableConfigurationProperties(HelloProperties.class)
public class HelloAutoConfiguration {
@Bean
@ConditionalOnMissingBean
public HelloService helloService(HelloProperties properties) {
return new HelloService(properties.getMessage());
}
}
HelloService.java(業(yè)務(wù)邏輯類)
package com.atguigu.hello;
public class HelloService {
private final String message;
public HelloService(String message) {
this.message = message;
}
public String sayHello() {
return message;
}
public void printHello() {
System.out.println(sayHello());
}
}
META-INF/spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.atguigu.hello.config.HelloAutoConfiguration
注意:spring.factories 文件必須放在 src/main/resources/META-INF/ 目錄下。
2. 創(chuàng)建 Starter 模塊
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.atguigu</groupId>
<artifactId>atguigu-hello-spring-boot-starter</artifactId>
<version>1.0.0</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.1.5</version>
<relativePath/>
</parent>
<dependencies>
<!-- 引入 autoconfigure 包 -->
<dependency>
<groupId>com.atguigu</groupId>
<artifactId>atguigu-hello-spring-boot-starter-autoconfigure</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
</project>
說明:這個 starter 只是一個“門面”,真正實現(xiàn)自動配置的是 autoconfigure 模塊。
四、使用自定義 Starter
1. 在目標項目中引入 Starter
pom.xml
<dependency>
<groupId>com.atguigu</groupId>
<artifactId>atguigu-hello-spring-boot-starter</artifactId>
<version>1.0.0</version>
</dependency>
2. 配置文件application.yml
hello: message: "Hello, Spring Boot!"
3. 編寫測試代碼
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Autowired
private HelloService helloService;
@PostConstruct
public void init() {
helloService.printHello(); // 輸出: Hello, Spring Boot!
}
}
五、完整流程總結(jié)
| 步驟 | 描述 |
|---|---|
| 1 | 創(chuàng)建 autoconfigure 模塊,包含自動配置邏輯 |
| 2 | 編寫 @Configuration 類,使用 @EnableConfigurationProperties 綁定配置 |
| 3 | 在 META-INF/spring.factories 中注冊自動配置類 |
| 4 | 創(chuàng)建 starter 模塊,僅依賴 autoconfigure |
| 5 | 在主項目中引入 starter,無需額外配置 |
| 6 | 通過 application.yml 設(shè)置參數(shù),自動生效 |
六、關(guān)鍵注解說明
| 注解 | 作用 |
|---|---|
| @Configuration | 標記為配置類 |
| @EnableConfigurationProperties | 啟用配置屬性綁定 |
| @ConditionalOnMissingBean | 當容器中沒有該 Bean 時才創(chuàng)建 |
| @Component | 將類注冊為 Spring Bean |
| @ConfigurationProperties | 綁定配置文件前綴屬性 |
七、擴展建議
- 支持多環(huán)境配置(如
dev,prod) - 添加日志記錄或監(jiān)控功能
- 使用
@ConditionalOnClass或@ConditionalOnProperty實現(xiàn)更復雜的條件判斷 - 提供接口供外部擴展
八、注意事項
spring.factories文件格式必須正確,不能有空格或換行錯誤。- 確保
autoconfigure模塊的spring-boot-autoconfigure依賴已添加。 - 啟動器模塊不要直接寫業(yè)務(wù)邏輯,只做依賴管理。
- 版本號保持一致,避免沖突。
九、運行效果
啟動項目后,控制臺輸出
Hello, Spring Boot!
說明自定義 Starter 成功加載并執(zhí)行了自動配置邏輯。
總結(jié):通過這種方式,你可以輕松地將任何功能封裝為可復用的 Spring Boot Starter,提升開發(fā)效率與代碼復用性。
到此這篇關(guān)于SpringBoot中自定義Starter的完整教程的文章就介紹到這了,更多相關(guān)SpringBoot自定義Starter內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java?-jar參數(shù)詳解之掌握Java可執(zhí)行JAR文件的運行技巧
做項目的時候我們肯定接觸過很多jar包,下面這篇文章主要給大家介紹了關(guān)于Java?-jar參數(shù)詳解之掌握Java可執(zhí)行JAR文件的運行技巧,文中通過代碼介紹的非常詳細,需要的朋友可以參考下2023-11-11
java執(zhí)行shell并獲取shell輸出日志方式
這篇文章主要介紹了java執(zhí)行shell并獲取shell輸出日志方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-04-04
Java OpenCV實現(xiàn)圖像鏡像翻轉(zhuǎn)效果
這篇文章主要為大家詳細介紹了Java OpenCV實現(xiàn)圖像鏡像翻轉(zhuǎn)效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-07-07
Spring Cloud Ubuntu環(huán)境部署的步驟與注意事項
這篇文章主要給大家介紹了關(guān)于Spring Cloud Ubuntu環(huán)境部署的步驟與注意事項,文中通過示例代碼介紹的非常詳細,對大家學習或者使用Spring Cloud具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧2019-12-12

