SpringBoot中自定義Starter的完整教程
前言
在 Spring Boot 中,自定義 Starter 是一種非常強(qiáng)大的方式,可以將常用的功能模塊封裝成可復(fù)用的組件。本文將詳細(xì)介紹如何創(chuàng)建一個自定義 Starter,并提供完整的使用流程和代碼示例。
一、自定義 Starter 原理
Spring Boot 的自動配置機(jī)制依賴于 META-INF/spring.factories 文件中的 EnableAutoConfiguration 配置項(xiàng)。當(dāng)項(xiàng)目啟動時,Spring Boot 會掃描所有 META-INF/spring.factories 文件,加載其中指定的自動配置類。
啟動流程圖解:
starter → autoconfigure → spring-boot-starter
↑
└─ META-INF/spring.factories
starter:Maven/Gradle 依賴包(啟動器)autoconfigure:包含自動配置邏輯的包spring.factories:配置自動配置類的入口文件
二、項(xiàng)目結(jié)構(gòu)設(shè)計(jì)
我們以一個簡單的“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
三、代碼實(shí)現(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 只是一個“門面”,真正實(shí)現(xiàn)自動配置的是 autoconfigure 模塊。
四、使用自定義 Starter
1. 在目標(biāo)項(xiàng)目中引入 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 | 在主項(xiàng)目中引入 starter,無需額外配置 |
| 6 | 通過 application.yml 設(shè)置參數(shù),自動生效 |
六、關(guān)鍵注解說明
| 注解 | 作用 |
|---|---|
| @Configuration | 標(biāo)記為配置類 |
| @EnableConfigurationProperties | 啟用配置屬性綁定 |
| @ConditionalOnMissingBean | 當(dāng)容器中沒有該 Bean 時才創(chuàng)建 |
| @Component | 將類注冊為 Spring Bean |
| @ConfigurationProperties | 綁定配置文件前綴屬性 |
七、擴(kuò)展建議
- 支持多環(huán)境配置(如
dev,prod) - 添加日志記錄或監(jiān)控功能
- 使用
@ConditionalOnClass或@ConditionalOnProperty實(shí)現(xiàn)更復(fù)雜的條件判斷 - 提供接口供外部擴(kuò)展
八、注意事項(xiàng)
spring.factories文件格式必須正確,不能有空格或換行錯誤。- 確保
autoconfigure模塊的spring-boot-autoconfigure依賴已添加。 - 啟動器模塊不要直接寫業(yè)務(wù)邏輯,只做依賴管理。
- 版本號保持一致,避免沖突。
九、運(yùn)行效果
啟動項(xiàng)目后,控制臺輸出
Hello, Spring Boot!
說明自定義 Starter 成功加載并執(zhí)行了自動配置邏輯。
總結(jié):通過這種方式,你可以輕松地將任何功能封裝為可復(fù)用的 Spring Boot Starter,提升開發(fā)效率與代碼復(fù)用性。
到此這篇關(guān)于SpringBoot中自定義Starter的完整教程的文章就介紹到這了,更多相關(guān)SpringBoot自定義Starter內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java單機(jī)環(huán)境實(shí)現(xiàn)定時任務(wù)技術(shù)
這篇文章主要介紹了Java單機(jī)環(huán)境實(shí)現(xiàn)定時任務(wù)技術(shù),文章內(nèi)容介紹詳細(xì),具有一定的參考價值,需要的小伙伴可以參考一下2022-04-04
IntelliJ IDEA maven 構(gòu)建簡單springmvc項(xiàng)目(圖文教程)
在工作當(dāng)中,我們有時需要創(chuàng)建一個全新的工程,而基于spring-mvc web的工程較為常見,這篇文章主要介紹了IntelliJ IDEA maven 構(gòu)建簡單springmvc項(xiàng)目(圖文教程),感興趣的小伙伴們可以參考一下2018-05-05
最長重復(fù)子數(shù)組 findLength示例詳解
今天給大家分享一道比較常問的算法面試題,最長重復(fù)子數(shù)組 findLength,文中給大家分享解題思路,結(jié)合示例代碼介紹的非常詳細(xì),需要的朋友參考下吧2023-08-08
SpringMVC Controller解析ajax參數(shù)過程詳解
這篇文章主要介紹了SpringMVC Controller解析ajax參數(shù)過程詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-07-07
Java項(xiàng)目在Idea中開發(fā)遇到所有代碼爆紅的問題與解決辦法
今天打開項(xiàng)目時發(fā)現(xiàn)idea竟然爆紅,通過查找相關(guān)資料用于解決,下面這篇文章主要給大家介紹了關(guān)于Java項(xiàng)目在Idea中開發(fā)遇到所有代碼爆紅的問題與解決辦法的相關(guān)資料,需要的朋友可以參考下2023-06-06

