Spring Boot Starter 是一组方便的依赖项,它们帮助开发者快速集成 Spring Boot 应用所需的常用功能。通过使用 Starter,开发者可以减少配置和依赖管理的复杂性。
手写一个 Spring Boot Starter 包可以帮助你快速复用某些功能或集成特定的库。下面是创建一个简单的 Spring Boot Starter 的步骤,包括项目结构、代码示例和配置说明。
1. 项目结构
首先,定义一个典型的项目结构:
spring-boot-starter-example
├── pom.xml
└── src
└── main
├── java
│ └── com
│ └── example
│ ├── AutoConfiguration.java
│ ├── ExampleService.java
│ └── ExampleProperties.java
└── resources
└── META-INF
└── spring.factories
2. 创建 pom.xml
创建 Maven 项目的 pom.xml,包含 Spring Boot 相关依赖和插件配置:
<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.example</groupId>
<artifactId>spring-boot-starter-example</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<properties>
<spring-boot.version>3.0.0</spring-boot.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3. 创建配置类
3.1 ExampleProperties.java
作用:定义自定义的配置属性类,用于映射 application.properties 中的配置项。
@ConfigurationProperties:将以example为前缀的属性映射到该类中,允许从配置文件中读取值。- 提供
getMessage()和setMessage()方法,用于访问和修改配置属性。
用于定义自定义的配置属性:
package com.example;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "example")
public class ExampleProperties {
private String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
3.2 AutoConfiguration.java
作用:实现自动配置的核心逻辑。
@AutoConfiguration:标识这是一个自动配置类,Spring Boot 会自动识别它。@EnableConfigurationProperties(ExampleProperties.class):启用ExampleProperties,使其可以从配置文件中读取属性。exampleService()方法:创建并返回ExampleService的实例,注入ExampleProperties中的配置值。
用于自动配置逻辑:
package com.example;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
@AutoConfiguration
@EnableConfigurationProperties(ExampleProperties.class)
@AutoConfigureAfter(SomeOtherAutoConfiguration.class) // 可以配置依赖的自动配置
public class AutoConfiguration {
private final ExampleProperties exampleProperties;
public AutoConfiguration(ExampleProperties exampleProperties) {
this.exampleProperties = exampleProperties;
}
@Bean
public ExampleService exampleService() {
return new ExampleService(exampleProperties.getMessage());
}
}
3.3 ExampleService.java
作用:实现具体的服务功能。
- 接收配置值
message,并提供printMessage()方法,输出该值。这个类的存在为 Starter 提供了实际功能。
示例服务类,使用自定义的配置属性:
package com.example;
public class ExampleService {
private final String message;
public ExampleService(String message) {
this.message = message;
}
public void printMessage() {
System.out.println("Example message: " + message);
}
}
4. 创建 spring.factories
作用:用于告知 Spring Boot 自动配置的类。
- 在
META-INF/spring.factories文件中,使用org.springframework.boot.autoconfigure.EnableAutoConfiguration键将自动配置类注册到 Spring Boot 中。 - Spring Boot 启动时会查找这个文件,从中读取需要自动配置的类。
在 src/main/resources/META-INF 目录下创建 spring.factories 文件,用于注册自动配置类:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.example.AutoConfiguration
5. 使用 Starter
在另一个 Spring Boot 项目中引入刚刚创建的 Starter:
5.1 在 pom.xml 中添加依赖
<dependency>
<groupId>com.example</groupId>
<artifactId>spring-boot-starter-example</artifactId>
<version>1.0.0</version>
</dependency>
5.2 在 application.properties 中配置
example.message=Hello, Spring Boot Starter!
5.3 使用示例
在 Spring Boot 应用中使用 ExampleService:
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
@Component
public class StartupRunner implements CommandLineRunner {
private final ExampleService exampleService;
public StartupRunner(ExampleService exampleService) {
this.exampleService = exampleService;
}
@Override
public void run(String... args) {
exampleService.printMessage();
}
}
401



被折叠的 条评论
为什么被折叠?



