暴吵萌厨ios手游
2.01G · 2025-11-14
Spring Boot Starter是Spring Boot框架的核心特性之一,它通过提供一组预定义的依赖和自动配置,极大地简化了Spring应用的开发过程。本文将详细介绍Spring Boot Starter的概念、原理、使用方法以及如何自定义开发Starter。
Spring Boot Starter是一组便捷的依赖描述符,它整合了某个功能模块所需的所有依赖库,并提供了自动配置功能。通过引入相应的Starter,开发者可以快速搭建具有特定功能的Spring应用,无需手动添加和配置大量的依赖项。
spring-boot-starter-{module},例如spring-boot-starter-web{module}-spring-boot-starter,例如mybatis-spring-boot-starterSpring Boot自动配置是Starter的核心,它的工作原理主要基于以下几个关键技术:
自动配置的核心在于@EnableAutoConfiguration注解,它通过以下步骤工作:
META-INF/spring.factories文件中注册的自动配置类@ConditionalOnClass、@ConditionalOnProperty等)判断是否需要加载对应的配置类@Bean注解将相关Bean注册到Spring容器中<!-- Web开发 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- WebFlux响应式Web开发 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<!-- JPA数据访问 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- JDBC数据访问 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!-- MongoDB数据访问 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<!-- Spring Security安全框架 -->
<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>
在项目的pom.xml文件中添加所需的Starter依赖:
<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>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
package com.example.demo.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping("/hello")
public String hello() {
return "Hello, Spring Boot Starter!";
}
}
一个完整的自定义Starter通常包含两个模块:
pom.xml配置:
<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>custom-spring-boot-autoconfigure</artifactId>
<version>1.0.0</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<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>
</project>
package com.example.custom.autoconfigure.properties;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "custom.service")
public class CustomServiceProperties {
private String prefix = "Hello";
private String suffix = "!";
private boolean enable = true;
// getters and setters
public String getPrefix() {
return prefix;
}
public void setPrefix(String prefix) {
this.prefix = prefix;
}
public String getSuffix() {
return suffix;
}
public void setSuffix(String suffix) {
this.suffix = suffix;
}
public boolean isEnable() {
return enable;
}
public void setEnable(boolean enable) {
this.enable = enable;
}
}
package com.example.custom.autoconfigure.service;
import com.example.custom.autoconfigure.properties.CustomServiceProperties;
public class CustomService {
private CustomServiceProperties properties;
public CustomService(CustomServiceProperties properties) {
this.properties = properties;
}
public String doSomething(String input) {
if (!properties.isEnable()) {
return input;
}
return properties.getPrefix() + " " + input + " " + properties.getSuffix();
}
}
package com.example.custom.autoconfigure.config;
import com.example.custom.autoconfigure.properties.CustomServiceProperties;
import com.example.custom.autoconfigure.service.CustomService;
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
@EnableConfigurationProperties(CustomServiceProperties.class)
@ConditionalOnProperty(prefix = "custom.service", name = "enable", havingValue = "true", matchIfMissing = true)
public class CustomAutoConfiguration {
@Bean
public CustomService customService(CustomServiceProperties properties) {
return new CustomService(properties);
}
}
在resources目录下创建META-INF文件夹,并在其中创建spring.factories文件:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.example.custom.autoconfigure.config.CustomAutoConfiguration
pom.xml配置:
<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>custom-spring-boot-starter</artifactId>
<version>1.0.0</version>
<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>custom-spring-boot-autoconfigure</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
</project>
<dependency>
<groupId>com.example</groupId>
<artifactId>custom-spring-boot-starter</artifactId>
<version>1.0.0</version>
</dependency>
custom.service.enable=true
custom.service.prefix=Hi
custom.service.suffix=!!!
package com.example.demo.controller;
import com.example.custom.autoconfigure.service.CustomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class CustomController {
@Autowired
private CustomService customService;
@GetMapping("/custom")
public String custom(String name) {
return customService.doSomething(name);
}
}
Spring Boot提供了多种条件注解,可以根据不同的条件决定是否加载配置:
@ConditionalOnBean:当容器中存在指定Bean时@ConditionalOnMissingBean:当容器中不存在指定Bean时@ConditionalOnClass:当类路径下存在指定类时@ConditionalOnMissingClass:当类路径下不存在指定类时@ConditionalOnProperty:当指定的属性有指定的值时@ConditionalOnResource:当类路径下存在指定资源时@ConditionalOnWebApplication:当应用是Web应用时@ConditionalOnNotWebApplication:当应用不是Web应用时spring-boot-starter-{module}{module}-spring-boot-starterSpring Boot Starter是Spring Boot框架的重要特性,它通过整合依赖和提供自动配置,极大地简化了Spring应用的开发。本文详细介绍了Spring Boot Starter的概念、原理、使用方法以及如何自定义开发Starter。通过学习本文,开发者可以更好地理解和应用Spring Boot Starter,提高开发效率,构建更加模块化、可复用的Spring应用。
在实际开发中,合理使用现有的Starter可以快速构建应用,而开发自定义Starter则可以将常用功能模块化,提高代码复用率和维护性。希望本文能够帮助开发者更好地掌握Spring Boot Starter的使用和开发技巧。
gitee.com/tree_boss/s…