乐闻世界logo
搜索文章和话题

What is the role and principle of Spring Boot Starter dependencies?

3月6日 21:58

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

TypeNaming RuleExample
Official Starterspring-boot-starter-*spring-boot-starter-web
Third-party Starter*-spring-boot-startermybatis-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:

properties
org.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

java
public 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:

shell
com.example.mystarter.MyServiceAutoConfiguration

Starter Benefits

  1. Simplified Dependency Management: One dependency includes all requirements
  2. Version Compatibility: Unified version management by Spring Boot
  3. Out-of-the-Box: Zero-configuration startup with auto-configuration
  4. 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.

标签:Spring Boot