在 Spring Boot 3.x 中自动装配的 SPI 机制发生了一个关键变化不再使用META-INF/spring.factories而是改用META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports文件。下面将基于 Spring Boot 3为你提供一个实现自定义 Starter 的完整示例。我们将创建一个名为hello-spring-boot-starter的项目它提供一个可以根据配置输出不同问候语的HelloService。 第一步创建 Maven 项目首先创建一个普通的 Maven 项目并遵循非官方 Starter 的命名规范{name}-spring-boot-starter。项目结构:hello-spring-boot-starter/ ├── src/ │ ├── main/ │ │ ├── java/com/example/hello/ │ │ │ ├── HelloProperties.java │ │ │ ├── HelloService.java │ │ │ └── HelloAutoConfiguration.java │ │ └── resources/ │ │ └── META-INF/ │ │ └── spring/ │ │ └── org.springframework.boot.autoconfigure.AutoConfiguration.imports │ └── test/ └── pom.xmlpom.xml配置:这里我们引入spring-boot-autoconfigure和用于处理配置属性的spring-boot-configuration-processor。?xml version1.0 encodingUTF-8? project xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd modelVersion4.0.0/modelVersion parent groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-parent/artifactId version3.2.0/version !-- 使用 Spring Boot 3.x -- relativePath/ /parent groupIdcom.example/groupId artifactIdhello-spring-boot-starter/artifactId version1.0.0-SNAPSHOT/version packagingjar/packaging dependencies !-- 核心依赖提供自动配置相关功能 -- dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-autoconfigure/artifactId /dependency !-- 可选依赖生成配置元数据提供IDE提示 -- dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-configuration-processor/artifactId optionaltrue/optional /dependency !-- Lombok简化代码 -- dependency groupIdorg.projectlombok/groupId artifactIdlombok/artifactId scopeprovided/scope /dependency /dependencies /project 第二步编写核心代码配置属性类 (HelloProperties.java)这个类用于绑定application.yml中的配置。package com.example.hello; import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; Data ConfigurationProperties(prefix hello) public class HelloProperties { /** * 是否启用 HelloService */ private boolean enabled true; /** * 问候语的前缀 */ private String prefix Hello; }业务服务类 (HelloService.java)这是 Starter 对外提供的核心功能。package com.example.hello; import lombok.RequiredArgsConstructor; RequiredArgsConstructor public class HelloService { private final HelloProperties properties; public String sayHello(String name) { if (!properties.isEnabled()) { return Service is disabled.; } return properties.getPrefix() , name !; } }自动配置类 (HelloAutoConfiguration.java)这是整个 Starter 的核心它使用条件注解来决定是否将HelloService注册到 Spring 容器中。package com.example.hello; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; Configuration // 1. 启用配置属性将 HelloProperties 注入到容器中 EnableConfigurationProperties(HelloProperties.class) // 2. 条件注解只有当配置文件中 hello.enabledtrue (或不配置使用默认值) 时此配置类才生效 ConditionalOnProperty(prefix hello, name enabled, havingValue true, matchIfMissing true) public class HelloAutoConfiguration { // 3. 向容器中注册 HelloService Bean Bean // 4. 条件注解只有当容器中不存在 HelloService 这个 Bean 时才创建此 Bean。 // 这允许使用者自定义 HelloService 来覆盖默认的实现。 ConditionalOnMissingBean public HelloService helloService(HelloProperties properties) { return new HelloService(properties); } }⚙️ 第三步声明自动配置(Spring Boot 3 的关键)在src/main/resources/META-INF/spring/目录下创建org.springframework.boot.autoconfigure.AutoConfiguration.imports文件。文件内容:com.example.hello.HelloAutoConfiguration这个文件的作用是告诉 Spring Boot“请在启动时加载com.example.hello.HelloAutoConfiguration这个配置类”。 第四步安装到本地 Maven 仓库在项目根目录下执行以下命令将你的 Starter 打包并安装到本地仓库以便其他项目使用。mvn clean install 第五步在其他项目中使用这个自定义的starter现在创建一个新的 Spring Boot 3 项目来测试你的 Starter。在pom.xml中添加依赖:dependencies dependency groupIdcom.example/groupId artifactIdhello-spring-boot-starter/artifactId version1.0.0-SNAPSHOT/version /dependency dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-web/artifactId /dependency /dependencies在application.yml中进行配置:hello: enabled: true prefix: Hi there创建一个 Controller 进行测试:RestController public class HelloController { // 直接使用 Autowired 注入Spring 会自动找到你自定义 Starter 中创建的 HelloService Bean Autowired private HelloService helloService; GetMapping(/hello) public String hello(String name) { return helloService.sayHello(name); } }启动应用并访问http://localhost:8080/hello?nameWorld你将看到返回Hi there, World!。这就完成了自定义 Starter 的创建和使用。