Spring Boot Starter Explained
What is a Starter
Spring Boot Starter is a set of predefined dependency descriptors that bundle all dependencies needed for a specific functionality. Developers only need to include one starter to get complete feature support.
Starter Naming Conventions
| Type | Naming Rule | Example |
|---|---|---|
| Official Starter | spring-boot-starter-* | spring-boot-starter-web |
| Third-party Starter | *-spring-boot-starter | mybatis-spring-boot-starter |
How Starters Work
1. Dependency Transitivity
A starter is essentially a Maven/Gradle project defining all required dependencies:
xml<!-- spring-boot-starter-web pom.xml --> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-json</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </dependency> </dependencies>
2. Version Management
Spring Boot manages dependency versions through Parent POM:
xml<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.2.0</version> </parent>
3. Auto-Configuration Activation
Starters work with auto-configuration registered in META-INF/spring.factories:
propertiesorg.springframework.boot.autoconfigure.EnableAutoConfiguration=\ org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration
Creating Custom Starters
Step 1: Create Maven Project
xml<artifactId>my-spring-boot-starter</artifactId> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-autoconfigure</artifactId> </dependency> </dependencies>
Step 2: Create Properties Class
java@ConfigurationProperties(prefix = "my.service") public class MyServiceProperties { private String host = "localhost"; private int port = 8080; // getters and setters }
Step 3: Create Service Class
javapublic class MyService { private final MyServiceProperties properties; public MyService(MyServiceProperties properties) { this.properties = properties; } }
Step 4: Create Auto-Configuration
java@Configuration @ConditionalOnClass(MyService.class) @EnableConfigurationProperties(MyServiceProperties.class) public class MyServiceAutoConfiguration { @Bean @ConditionalOnMissingBean public MyService myService(MyServiceProperties properties) { return new MyService(properties); } }
Step 5: Register Auto-Configuration
In META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports:
shellcom.example.mystarter.MyServiceAutoConfiguration
Starter Benefits
- Simplified Dependency Management: One dependency includes all requirements
- Version Compatibility: Unified version management by Spring Boot
- Out-of-the-Box: Zero-configuration startup with auto-configuration
- Extensibility: Easy to customize and extend
Summary
Spring Boot Starter combines dependency aggregation and auto-configuration, simplifying development through Maven/Gradle's dependency transitivity and enabling plug-and-play functionality.