1、概述
Spring Boot Starter 是 Spring Boot 提供的一种非常方便的依赖管理方式,它允许开发者通过添加一个依赖来引入一组库,同时附带有预设的配置,从而简化了项目的搭建和开发过程。
Spring Boot Starter 的主要特点包括:
- 简化依赖管理:通过引入 Starter,开发者无需手动添加和管理大量的依赖项。
- 约定优于配置:Starter 遵循“约定优于配置”的原则,提供默认配置以满足大多数场景的需求。
- 自动配置:Spring Boot 能够根据类路径下的类、资源文件和
META-INF/spring.factories配置文件自动配置项目所需的各种组件和服务。 - 易于扩展:开发者可以通过自定义 Starter 来扩展 Spring Boot 的功能。
Starter 的命名通常遵循一定的规则:
- 官方 Starter:
spring-boot-starter-xxx - 非官方 Starter:
xxx-spring-boot-starter
2、自定义 Spring Boot Starter
1)引入依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
</dependencies>
2)编写自动配置
- 编写属性类:
@ConfigurationProperties(prefix = xxxProperties.PREFIX)
public class xxxProperties {
public static final String PREFIX = "spring.xxx";
private boolean enabled;
private String host;
private int port;
//getter setter
}
- 编写自动配置类:
@Configuration
@EnableConfigurationProperties(xxxProperties.class)
public class xxxAutoConfiguration {
private static final Logger logger = LoggerFactory.getLogger(xxxAutoConfiguration.class);
@Autowired
private xxxProperties properties;
@Bean
@ConditionalOnMissingBean
@ConditionalOnProperty(prefix = xxxProperties.PREFIX, value = "enabled", havingValue = "true")
public XxlJobSpringExecutor xxx() {
logger.info(">>>>>>>>>>> xxx init.");
xxx bean = new xxx();
bean.setHost(properties.getHost());
bean.setPort(properties.getPort());
return bean;
}
}
3)创建配置文件
- Spring Boot 2.x:
META-INF/spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.xxx.xxx.xxx.xxxConfiguration
- Spring Boot 3.x:
META-INF/org.springframework.boot.autoconfigure.AutoConfiguration.imports
com.xxx.xxx.xxxAutoConfiguration
3、相关注解
@ConfigurationProperties:用于将外部配置(如 application.properties 或 application.yml 文件中的属性)绑定到 Java 对象。
- prefix:指定配置文件中相关属性的前缀,将它们映射到 Java 对象的字段上。
- locations:指定配置属性文件的位置,默认情况下,Spring Boot 会从
application.properties或application.yml文件中加载配置。
@EnableConfigurationProperties:用于启用配置属性,通常与 @ConfigurationProperties 注解配合使用。
- value/classes:指定要注册的
@ConfigurationProperties注解的类。
@ConditionalOnClass:当指定的类在类路径上存在时,才会创建 Bean。
- value:指定需要存在的类全限定名数组。
@ConditionalOnMissingClass:当指定的类在类路径上不存在时,才会创建 Bean。
- value:指定不应该存在的类的全限定名数组。
@ConditionalOnBean:当容器中存在指定的 Bean 时,才会创建 Bean。
- value/name:指定需要存在的bean的类型。
- type:指定需要存在的bean的类型,可以是类全限定名或别名。
- search:指定搜索策略,默认为
SearchStrategy.ALL。
@ConditionalOnMissingBean:当容器中不存在指定的 Bean 时,才会创建 Bean。
- value/name:指定不应该存在的bean的类型。
- type:指定不应该存在的bean的类型,可以是类全限定名或别名。
@ConditionalOnProperty:当指定的属性在配置文件中有特定的值时,才会创建 Bean。
- prefix:指定配置属性的前缀。
- name:指定配置属性的名称。
- havingValue:指定配置属性应该有的值。
- matchIfMissing:指定在配置属性缺失时是否匹配。
@ConditionalOnResource:当指定的资源在类路径上存在时,才会创建 Bean。
- resources:指定必须存在的资源路径数组。
@ConditionalOnWebApplication:当应用程序是 Web 应用程序时,才会创建 Bean。
- type:指定需要匹配的Web应用程序类型(
Type.SERVLET或Type.REACTIVE)。
@ConditionalOnExpression:当 SpEL 表达式计算为 true 时,才会创建 Bean。
- value:指定需要评估的SpEL表达式。
@ConditionalOnJava:当 JVM 版本符合指定的条件时,才会创建 Bean。
- value:指定需要匹配的Java版本。
4、@Conditional 原理
@Conditional 相关注解都会通过 @Conditional(xxx.class) 来指定一个实现了 Condition 接口的类,Spring 会调用 OnxxxCondition 的 matches(),返回一个布尔值,表示条件是否满足。接收两个参数:ConditionContext 和 AnnotatedTypeMetadata。
ConditionContext:提供了对当前 Spring 容器环境的访问。AnnotatedTypeMetadata提供了关于注解所在的类或方法的元数据信息。