SpringBoot 自定義 Starter
自定義 Starter
一、基礎搭建
-
創建項目和模塊 ( 建議使用阿里云鏡像地址初始化: http://start.aliyun.com )
-
創建一個空的 SpringBoot 2.7.6 項目, 作為父工程
-
創建一個空的名為
my3-spring-boot-autoconfigure? (簡稱autoconfigure?) SpringBoot 2.7.6 模塊, 作為子模塊作用: 用于編寫核心代碼
-
創建一個空的名為
my3-spring-boot-starter? (簡稱starter?) 普通Maven模塊, 作為子模塊作用: 用于依賴管理
-
-
調整文件結構
-
刪除父工程和
starter?子模塊下的 src 目錄, 效果如下![image]()
?
-
-
修改pom.xml
-
父模塊
刪除
build?標簽 , 效果如下<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>customStarter3</artifactId> <version>0.0.1-SNAPSHOT</version> <!-- 注意打包方式 --> <packaging>pom</packaging> <name>customStarter3</name> <description>customStarter3</description> <!-- 注意這里autoconfigure在starter前面 --> <modules> <module>my3-spring-boot-autoconfigure</module> <module>my3-spring-boot-starter</module> </modules> <properties> <java.version>1.8</java.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <spring-boot.version>2.7.6</spring-boot.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>${spring-boot.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> </project> -
?
autoconfigure? 子模塊添加
spring-boot-configuration-processor?依賴, 用于寫yml配置屬性具有提示注意這個依賴不要傳遞, 添加
<optional>true</optional>?, 效果如下<?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> <parent> <groupId>com.example</groupId> <artifactId>customStarter3</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>my3-spring-boot-autoconfigure</artifactId> <properties> <maven.compiler.source>17</maven.compiler.source> <maven.compiler.target>17</maven.compiler.target> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <!-- yml配置屬性提示 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> </dependencies> </project> -
?
starter? 子模塊添加依賴, 這個依賴就是
autoconfigure?子模塊<?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> <parent> <groupId>com.example</groupId> <artifactId>customStarter3</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>my3-spring-boot-starter</artifactId> <properties> <maven.compiler.source>17</maven.compiler.source> <maven.compiler.target>17</maven.compiler.target> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>com.example</groupId> <artifactId>my3-spring-boot-autoconfigure</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies> </project>
-
-
編寫核心代碼
一般需要在
autoconfigure?子模塊寫三個核心代碼 (配置屬性類, 自動配置類, 被裝配的類), 一個核心文件(.imports文件)-
配置屬性類 (用于讀取 application.yml 配置屬性)
package com.example; import org.springframework.boot.context.properties.ConfigurationProperties; // 讀取yml中以hello開頭的屬性 @ConfigurationProperties(prefix = "hello") public class HelloProperties { // hello.greeting 對應賦給 greeting, 沒有則給默認值為 "Hello" private String greeting = "Hello"; private String message = "World"; public String getGreeting() { return greeting; } public void setGreeting(String greeting) { this.greeting = greeting; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } } -
需要被裝配的類
package com.example; public class HelloService { private final String greeting; private final String message; public HelloService(String greeting, String message) { this.greeting = greeting; this.message = message; } public String sayHello() { return greeting + ", " + message + "!"; } } -
自動配置類
package com.example; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration @ConditionalOnClass(HelloService.class) @EnableConfigurationProperties(HelloProperties.class) public class HelloServiceAutoConfiguration { @Bean public HelloService helloService(HelloProperties helloproperties) { return new HelloService(helloproperties.getGreeting(), helloproperties.getMessage()); } } -
resources 目錄下, META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
com.example.HelloServiceAutoConfiguration
-
-
打包測試
-
在自定義 starter 項目中
點擊idea右側的Maven, 再點擊父模塊中的生命周期的 install, 安裝到本地倉庫
-
在別的項目中
-
引入
starter?依賴使用 (注意引入的是子模塊starter?, version 是starter?子模塊的版本號)<dependency> <groupId>com.example</groupId> <artifactId>my3-spring-boot-starter</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> -
application.yml 中填寫配置信息
hello: greeting: greeting6666 message: message6666 -
單元測試中, 自動注入被裝配的類就可以測試了
package com.exampleTest; import com.example.HelloService; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; @SpringBootTest class TestCustomStarterApplicationTests { @Autowired private HelloService helloService; @Test void contextLoads() { System.out.println(helloService.sayHello()); } }
-
-
?


浙公網安備 33010602011771號