1. 新建my-spring-boot-starter
项目
完整的目录结构如下:
2. 引入相关依赖
pom.xml
<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.2.2.RELEASE</spring-boot.version>
</properties>
<!-- 版本依赖集中管理 -->
<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>
<!-- 引入相关依赖 -->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<!-- 打包插件 -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<configuration>
<mainClass>com.liuliu.starter.MySpringBootStarterApplication</mainClass>
<skip>true</skip>
</configuration>
<executions>
<execution>
<id>repackage</id>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
3.自定义服务类
MyStarterService.java
// 自定义服务类,供后续其他模块使用
public class MyStarterService {
private String message;
public MyStarterService(String message) {
this.message = message;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
4.自定义属性配置文件类
MyStarterProperties.java
// 属性配置类,把配置文件的属性通过k-v的形式绑定到当前bean的属性中
@Configuration
@ConfigurationProperties(prefix = "my.starter")
public class MyStarterProperties {
private String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
5.自定义Spring Boot启动自动配置类
MyStarterAutoConfiguration.java
/**
* MyStarter 自动配置类。
*
* 该类用于自动配置 MyStarter 的相关 Bean,当满足特定条件时会自动创建。
*/
@Configuration
@EnableConfigurationProperties(MyStarterProperties.class) // 启用 MyStarterProperties 属性绑定
public class MyStarterAutoConfiguration {
/**
* 创建 MyStarterService 的 Bean。
*
* 当上下文中不存在 MyStarterService 的 Bean 时,会创建一个新的 MyStarterService 实例。
*
* @param myStarterProperties MyStarterProperties 的配置类,提供必要的配置参数
* @return MyStarterService 实例
*/
@ConditionalOnMissingBean(MyStarterService.class) // 仅在没有 MyStarterService Bean 时创建
@Bean // 声明该方法返回的对象是一个 Bean
public MyStarterService myStarterService(MyStarterProperties myStarterProperties) {
// 使用配置属性中的消息创建并返回 MyStarterService 实例
return new MyStarterService(myStarterProperties.getMessage());
}
}
6.创建spring.factories
文件
在Resources/META-INF
目标下创建spring.factories
文件,指定SpringBoot
启动时自动配置类的全路径。
spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.liuliu.starter.config.MyStarterAutoConfiguration
7.项目打包
使用maven命令:
mvn clean install
或者idea直接install打包:
打包完成后,在target目录下生成:
8.在其他项目中使用my-spring-boot-starter
1.引入my-spring-boot-starter
坐标
<dependency>
<groupId>com.liuliu.starter</groupId>
<artifactId>my-spring-boot-starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
2.在application.yml
文件中编辑
在application.yml
文件中编辑,添加如下内容,下面的内容将会和my-spring-boot-starter
中的MyStarterProperties
属性配置类进行绑定。
my:
starter:
message: "Hello from My Spring Boot Starter!"
3.测试类测试
@Autowired
private MyStarterService myService;
@Test
void test1() {
System.out.println(myService.getMessage());
}
然后控制台输出:
Hello from My Spring Boot Starter!