Spring Boot Starter 详解
什么是 Starter
Spring Boot Starter 是一组预定义的依赖描述符,它整合了某个功能所需的全部依赖,开发者只需引入一个 starter,即可获得完整的功能支持。
Starter 的命名规范
| 类型 | 命名规则 | 示例 |
|---|---|---|
| 官方 Starter | spring-boot-starter-* | spring-boot-starter-web |
| 第三方 Starter | *-spring-boot-starter | mybatis-spring-boot-starter |
常见官方 Starter 列表
xml<!-- Web 应用 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- 数据访问 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <!-- 安全框架 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> <!-- 测试 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency>
Starter 的工作原理
1. 依赖传递机制
Starter 本质上是一个 Maven/Gradle 项目,通过 pom.xml 定义了功能所需的全部依赖:
xml<!-- spring-boot-starter-web 的 pom.xml --> <dependencies> <!-- Spring MVC --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-json</artifactId> </dependency> <!-- Tomcat --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </dependency> <!-- Spring Web --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> </dependency> </dependencies>
2. 版本管理
Spring Boot 通过 Parent POM 统一管理依赖版本:
xml<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.2.0</version> </parent>
在 spring-boot-dependencies 中定义了所有依赖的版本号:
xml<properties> <spring-framework.version>6.1.1</spring-framework.version> <tomcat.version>10.1.16</tomcat.version> <jackson.version>2.16.0</jackson.version> </properties>
3. 自动配置激活
Starter 通常配合自动配置使用,在 META-INF/spring.factories 中注册:
propertiesorg.springframework.boot.autoconfigure.EnableAutoConfiguration=\ org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration
自定义 Starter 开发
步骤一:创建 Maven 项目
xml<artifactId>my-spring-boot-starter</artifactId> <dependencies> <!-- 自动配置依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-autoconfigure</artifactId> </dependency> <!-- 配置处理器 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> </dependencies>
步骤二:创建属性配置类
java@ConfigurationProperties(prefix = "my.service") public class MyServiceProperties { private String host = "localhost"; private int port = 8080; private boolean enabled = true; // getters and setters }
步骤三:创建服务类
javapublic class MyService { private final MyServiceProperties properties; public MyService(MyServiceProperties properties) { this.properties = properties; } public String connect() { return "Connecting to " + properties.getHost() + ":" + properties.getPort(); } }
步骤四:创建自动配置类
java@Configuration @ConditionalOnClass(MyService.class) @EnableConfigurationProperties(MyServiceProperties.class) @ConditionalOnProperty(prefix = "my.service", value = "enabled", matchIfMissing = true) public class MyServiceAutoConfiguration { @Bean @ConditionalOnMissingBean public MyService myService(MyServiceProperties properties) { return new MyService(properties); } }
步骤五:注册自动配置
在 src/main/resources/META-INF/spring.factories 中添加:
propertiesorg.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.example.mystarter.MyServiceAutoConfiguration
Spring Boot 2.7+ 推荐使用 META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports:
shellcom.example.mystarter.MyServiceAutoConfiguration
步骤六:打包发布
bashmvn clean install
Starter 使用示例
xml<dependency> <groupId>com.example</groupId> <artifactId>my-spring-boot-starter</artifactId> <version>1.0.0</version> </dependency>
yamlmy: service: host: 192.168.1.100 port: 9090
java@Service public class MyApplicationService { @Autowired private MyService myService; public void doSomething() { String result = myService.connect(); System.out.println(result); } }
Starter 的优势
- 简化依赖管理:一个依赖引入全部所需
- 版本兼容性:Spring Boot 统一管理版本
- 开箱即用:配合自动配置,零配置启动
- 可扩展性:易于自定义和扩展
总结
Spring Boot Starter 是依赖聚合和自动配置的结合体,它通过 Maven/Gradle 的依赖传递机制简化依赖引入,配合自动配置实现功能的即插即用,是 Spring Boot 简化开发的核心机制之一。