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号