这份指南详细整理了 spring boot 中最核心的配置绑定、自动装配以及条件注解。这些注解是理解 spring boot "约定大于配置" (convention over configuration) 机制的基石。
本文档详细解析 spring boot 中用于配置绑定、自动装配流程控制及条件加载的核心注解。
1. 配置属性绑定 (configuration binding)
这一组注解的主要作用是将 application.yml 或 application.properties 中的配置值绑定到 java bean 中。
@configurationproperties
- 用途:将配置文件中指定前缀的属性值自动绑定到 java 类的字段上。
- 关键点:
- 仅仅加上这个注解,spring 容器并不会自动注册它。它需要配合 @enableconfigurationproperties 或 @component (配合扫描) 使用。
- 支持松散绑定 (relaxed binding),例如 first-name 可以绑定到 firstname。
@enableconfigurationproperties
- 用途:显式地开启 @configurationproperties 注解类的功能,并将该类注册为 spring bean。
- 场景:通常用在 @configuration 类上,用于引入第三方的配置类,或者当你不想在配置类上加 @component 时使用。
@configurationpropertiesscan
- 用途:从 spring boot 2.2 开始引入。它会扫描指定包路径下所有标注了 @configurationproperties 的类,并自动注册为 bean。
- 优势:省去了在配置类上写一堆 @enableconfigurationproperties({a.class, b.class}) 的麻烦。
📝 代码示例
1. 配置文件 (application.yml)
app:
server:
timeout: 5000
enabled: true2. 配置类 pojo
import org.springframework.boot.context.properties.configurationproperties;
// 声明前缀,spring 会去查找 app.server.*
@configurationproperties(prefix = "app.server")
public class serverproperties {
private integer timeout;
private boolean enabled;
// 必须要有 getter/setter
public integer gettimeout() { return timeout; }
public void settimeout(integer timeout) { this.timeout = timeout; }
public boolean isenabled() { return enabled; }
public void setenabled(boolean enabled) { this.enabled = enabled; }
}3. 启用配置 (三种方式选其一)
方式 a:配合 @component (最简单)
- 在
serverproperties上直接加@component,配合@configurationproperties。
- 在
方式 b:使用 @enableconfigurationproperties (推荐用于库开发)
@configuration @enableconfigurationproperties(serverproperties.class) public class myconfig { ... }方式 c:使用 @configurationpropertiesscan (适合大型项目)
@springbootapplication @configurationpropertiesscan("com.example.config") // 扫描指定包 public class myapplication { ... }
2. 自动装配核心 (auto configuration)
@enableautoconfiguration
用途:spring boot 的魔法核心。它告诉 spring boot 根据添加的 jar 依赖自动配置 spring 应用。
原理:
- 它利用 springfactoriesloader 机制。
- 扫描 classpath 下所有 jar 包中的 meta-inf/spring.factories (spring boot 2.x) 或 meta-inf/spring/org.springframework.boot.autoconfigure.autoconfiguration.imports (spring boot 3.x)。
- 加载其中声明的配置类,但在加载前会通过 @conditional 系列注解进行过滤。
注意:通常包含在 @springbootapplication 中,很少单独使用。
3. 条件注解 (conditionals)
这一组注解决定了 bean 是否会被创建并注册到容器中。这是实现“自动装配”逻辑判断的关键。
@conditional
- 用途:最底层的注解。它接收一个实现了 condition 接口的类。只有当 matches 方法返回 true 时,bean 才会被创建。
- 场景:编写非常复杂的自定义逻辑判断时使用。
@conditionalonproperty
用途:这是 @conditional 的扩展,专门用于判断配置文件中是否存在某个属性,或该属性的值是否等于特定值。
常用属性:
- prefix: 属性前缀。
- name: 属性名。
- havingvalue: 期望的值(如果匹配则加载)。
- matchifmissing: 如果配置文件中没写这个属性,默认是否加载(true 为加载)。
📝 代码示例:基于配置开关功能的 bean
假设我们根据配置决定启用 "短信通知" 还是 "邮件通知"。
1. 配置文件
feature: notification: sms # 改为 email 则加载 emailservice
2. 业务 bean 定义
import org.springframework.boot.autoconfigure.condition.conditionalonproperty;
import org.springframework.context.annotation.bean;
import org.springframework.context.annotation.configuration;
@configuration
public class notificationconfig {
@bean
// 当 feature.notification = email 时生效
@conditionalonproperty(prefix = "feature", name = "notification", havingvalue = "email")
public notificationservice emailservice() {
return new emailnotificationservice();
}
@bean
// 当 feature.notification = sms 时生效
// matchifmissing = true 表示如果不配这个配置,默认也启用 sms
@conditionalonproperty(prefix = "feature", name = "notification", havingvalue = "sms", matchifmissing = true)
public notificationservice smsservice() {
return new smsnotificationservice();
}
}📝 代码示例:自定义 @conditional (高阶用法)
如果 @conditionalonproperty 无法满足需求(比如判断操作系统、判断某个文件是否存在),可以使用 @conditional。
// 1. 定义判断逻辑
public class windowscondition implements condition {
@override
public boolean matches(conditioncontext context, annotatedtypemetadata metadata) {
return context.getenvironment().getproperty("os.name").contains("windows");
}
}
// 2. 使用注解
@bean
@conditional(windowscondition.class) // 只有在 windows 系统下才加载该 bean
public cmdservice cmdservice() {
return new windowscmdservice();
}4. 总结速查表
| 注解 | 作用域 | 核心作用 | 一句话记忆 |
|---|---|---|---|
| @configurationproperties | 类 | 定义配置属性与 pojo 的映射 | "把 yml 变成 java 对象" |
| @enableconfigurationproperties | 配置类 | 激活并注册 @configurationproperties 类 | "手动开关:启用属性对象" |
| @configurationpropertiesscan | 启动类 | 自动扫描并注册属性类 | "自动开关:扫码全注册" |
| @enableautoconfiguration | 启动类 | 开启自动装配机制 (spi) | "spring boot 的自动魔法引擎" |
| @conditional | 方法/类 | 自定义复杂的加载条件 | "万能的 if 判断" |
| @conditionalonproperty | 方法/类 | 基于配置属性值决定是否加载 bean | "根据开关 (toggle) 加载 bean" |
到此这篇关于springboot自动装配注解的实现示例的文章就介绍到这了,更多相关springboot自动装配注解内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论