博主社群介绍: ① 群内初中生、高中生、本科生、研究生、博士生遍布,可互相学习,交流困惑。
② 热榜top10的常客也在群里,也有数不清的万粉大佬,可以交流写作技巧,上榜经验,涨粉秘籍。
③ 群内也有职场精英,大厂大佬,跨国企业主管,可交流技术、面试、找工作的经验。
进群免费赠送写作秘籍一份,助你由写作小白晋升为创作大佬,进群赠送CSDN评论防封脚本,送真活跃粉丝,助你提升文章热度。
群公告里还有全网大赛约稿汇总/博客提效工具集/CSDN自动化运营脚本 有兴趣的加文末联系方式,备注自己的CSDN昵称,拉你进群,互相学习共同进步。
文章目录
- [1. Spring Boot 配置属性绑定概览](#1. Spring Boot 配置属性绑定概览)
- [2. @ConfigurationProperties 注解原理](#2. @ConfigurationProperties 注解原理)
- [3. Binder 核心类与核心接口](#3. Binder 核心类与核心接口)
- [4. 配置属性解析流程(源码级调用链)](#4. 配置属性解析流程(源码级调用链))
-
-
- [4.1 Binder.bind()](#4.1 Binder.bind())
- [4.2 doBind 核心](#4.2 doBind 核心)
-
- [5. PropertySourcesPlaceholdersResolver 与 PropertySource 的角色](#5. PropertySourcesPlaceholdersResolver 与 PropertySource 的角色)
- [6. Binder 的类型转换机制(ConversionService)](#6. Binder 的类型转换机制(ConversionService))
- [7. 复杂类型与嵌套绑定](#7. 复杂类型与嵌套绑定)
- [8. @ConstructorBinding 构造器绑定](#8. @ConstructorBinding 构造器绑定)
- [9. Mermaid 图:Binder 配置属性绑定流程](#9. Mermaid 图:Binder 配置属性绑定流程)
- [10. 实战示例:多数据源配置绑定](#10. 实战示例:多数据源配置绑定)
-
-
- [Java Bean](#Java Bean)
- 使用示例
-
- [11. 表格总结常用配置属性绑定场景](#11. 表格总结常用配置属性绑定场景)
- [12. Spring Boot Binder 源码关联图](#12. Spring Boot Binder 源码关联图)
- [13. 参考文档](#13. 参考文档)
- 结束语

1. Spring Boot 配置属性绑定概览
Spring Boot 提供 @ConfigurationProperties + Binder 机制,核心作用:
将
application.properties/application.yml中的配置绑定到 Java 对象上,实现类型安全配置。
特性:
- 支持嵌套对象绑定
- 支持集合、Map、List 绑定
- 支持类型转换(Integer → Long → Enum → Duration 等)
- 支持构造器绑定(@ConstructorBinding)
- 支持 Spring Environment 与 PropertySource 无缝集成
绑定体系图:
Environment / PropertySource Binder PropertySourcesPlaceholdersResolver ConversionService 目标 Java Bean
2. @ConfigurationProperties 注解原理
java
@Target(TYPE)
@Retention(RUNTIME)
@Documented
@Component
public @interface ConfigurationProperties {
String prefix() default "";
boolean ignoreUnknownFields() default true;
boolean ignoreInvalidFields() default true;
boolean ignoreNestedProperties() default false;
}
核心逻辑:
@ConfigurationProperties标注 Bean- 由 ConfigurationPropertiesBindingPostProcessor 处理
- 自动调用
Binder将 Environment 中配置绑定到 Bean
3. Binder 核心类与核心接口
Spring Boot 2.x 核心绑定类:
| 类 | 功能 |
|---|---|
| Binder | 核心入口,负责将 PropertySource 数据绑定到目标类型 |
| Bindable | 封装目标类型信息(Type + 泛型信息 + 对象实例) |
| BindHandler | 绑定过程处理接口(可扩展钩子) |
| ConversionService | 类型转换 |
| PropertySourcesPlaceholdersResolver | 属性值解析 |
核心调用关系:
Environment Binder Bindable BindHandler ConversionService PropertySourcesPlaceholdersResolver 目标 Bean
4. 配置属性解析流程(源码级调用链)
核心绑定入口:
java
Binder binder = Binder.get(environment);
MyProperties properties = binder.bind("my.datasource", Bindable.of(MyProperties.class)).get();
源码拆解:
4.1 Binder.bind()
java
public <T> BindResult<T> bind(String name, Bindable<T> target) {
// 1. 构建 ConfigurationPropertySource
ConfigurationPropertySource source = asConfigurationPropertySource(this.propertySources);
// 2. 调用 doBind
return doBind(name, target, source, null);
}
4.2 doBind 核心
java
private <T> BindResult<T> doBind(String name, Bindable<T> target, ConfigurationPropertySource source, BindHandler handler) {
BindContext context = new BindContext(source, handler, this.conversionService);
return BinderUtils.bind(name, target, context);
}
5. PropertySourcesPlaceholdersResolver 与 PropertySource 的角色
Spring Boot 允许 ${...} 占位符:
yaml
my:
datasource:
url: ${DB_URL}
- PropertySourcesPlaceholdersResolver 负责解析占位符
- ConfigurationPropertySource 提供统一接口访问 PropertySource(包括 Environment)
6. Binder 的类型转换机制(ConversionService)
Binder 内置 ConversionService,支持:
| 类型 | 转换示例 |
|---|---|
| 原始类型 | String → Integer / Long / Boolean |
| 枚举 | "PROD" → Env.PROD |
| Duration | "5s" → Duration.ofSeconds(5) |
| List | "a,b,c" → List |
| Map | "key1=val1,key2=val2" → Map<String,String> |
源码:
java
T value = conversionService.convert(sourceValue, targetType);
支持自定义 ConversionService,通过 @ConfigurationPropertiesBinding 注册。
7. 复杂类型与嵌套绑定
示例:
java
@ConfigurationProperties(prefix="my.datasource")
public class MyDataSourceProperties {
private String url;
private Credentials credentials;
private List<String> replicas;
public static class Credentials {
private String username;
private String password;
}
}
绑定规则:
my.datasource.credentials.username→ Credentials.usernamemy.datasource.replicas[0]→ replicas.get(0)
Binder 会递归解析嵌套对象。
8. @ConstructorBinding 构造器绑定
Spring Boot 2.2+ 支持:
java
@ConfigurationProperties(prefix="my.datasource")
@ConstructorBinding
public class MyDataSourceProperties {
private final String url;
private final String username;
public MyDataSourceProperties(String url, String username) {
this.url = url;
this.username = username;
}
}
Binder 调用构造器绑定流程:
java
Constructor<?> constructor = findPrimaryConstructor(beanClass);
Object instance = Binder.bindConstructor(constructor, properties);
特点:
- Bean 是不可变对象
- 强类型安全
- 支持嵌套对象绑定
9. Mermaid 图:Binder 配置属性绑定流程
Environment / PropertySource Binder ConfigurationPropertySource Bindable / TargetType BindHandler / ConversionService 递归绑定嵌套对象 返回目标 Bean
10. 实战示例:多数据源配置绑定
yaml
my:
datasource:
url: jdbc:mysql://localhost:3306/db
username: root
password: 123456
replicas:
- jdbc:mysql://replica1:3306/db
- jdbc:mysql://replica2:3306/db
Java Bean
java
@ConfigurationProperties(prefix="my.datasource")
@ConstructorBinding
public class MyDataSourceProperties {
private final String url;
private final String username;
private final String password;
private final List<String> replicas;
public MyDataSourceProperties(String url, String username, String password, List<String> replicas) {
this.url = url;
this.username = username;
this.password = password;
this.replicas = replicas;
}
}
使用示例
java
@Autowired
private MyDataSourceProperties props;
System.out.println(props.getUrl());
System.out.println(props.getReplicas().get(0));
输出:
jdbc:mysql://localhost:3306/db
jdbc:mysql://replica1:3306/db
11. 表格总结常用配置属性绑定场景
| 场景 | 注解 | 特点 |
|---|---|---|
| 普通 Bean | @ConfigurationProperties | Setter 绑定 |
| 构造器绑定 | @ConstructorBinding | 不可变对象,强类型安全 |
| 集合 / List | Binder 支持 | 支持 [0],[1] 索引 |
| Map | Binder 支持 | key=value 形式 |
| 占位符 | ${...} | PropertySourcesPlaceholdersResolver 解析 |
| 类型转换 | ConversionService | 支持 Duration, Enum, String → 数字等 |
12. Spring Boot Binder 源码关联图
Environment / PropertySource ConfigurationPropertySource Binder Bindable.of(TargetType) BindHandler / ConversionService 递归绑定嵌套对象 返回绑定后的 Bean
13. 参考文档
- Spring Boot 官方文档
https://docs.spring.io/spring-boot/docs/current/reference/html/features.html#features.external-config.typesafe-configuration-properties - Spring Boot GitHub 源码
https://github.com/spring-projects/spring-boot - Binder Javadoc
https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/context/properties/bind/Binder.html
结束语

👨💻 关于我
持续学习 | 追求真我
如果本篇文章帮到了你 不妨点个赞吧~ 我会很高兴的。想看更多 那就点个关注吧 我会尽力带来有趣的内容 😎。
感谢订阅专栏 三连文章

掘金点击访问Qiuner CSDN点击访问Qiuner GitHub点击访问Qiuner Gitee点击访问Qiuner
| 专栏 | 简介 |
|---|---|
| 📊 一图读懂系列 | 图文并茂,轻松理解复杂概念 |
| 📝 一文读懂系列 | 深入浅出,全面解析技术要点 |
| 🌟持续更新 | 保持学习,不断进步 |
| 🎯 人生经验 | 经验分享,共同成长 |
你好,我是Qiuner. 为帮助别人少走弯路而写博客
如果本篇文章帮到了你 不妨点个赞 吧~ 我会很高兴的 😄 (^ ~ ^) 。想看更多 那就点个关注吧 我会尽力带来有趣的内容 😎。
代码都在Github或Gitee上,如有需要可以去上面自行下载。记得给我点星星哦😍
如果你遇到了问题,自己没法解决,可以去我掘金评论区问。CSDN评论区和私信消息看不完 掘金消息少一点.
| 上一篇推荐 | 链接 |
|---|---|
| Java程序员快又扎实的学习路线 | 点击该处自动跳转查看哦 |
| 一文读懂 AI | 点击该处自动跳转查看哦 |
| 一文读懂 服务器 | 点击该处自动跳转查看哦 |
| 2024年创作回顾 | 点击该处自动跳转查看哦 |
| 一文读懂 ESLint配置 | 点击该处自动跳转查看哦 |
| 老鸟如何追求快捷操作电脑 | 点击该处自动跳转查看哦 |
| 未来会写什么文章? | 预告链接 |
|---|---|
| 一文读懂 XX? | 点击该处自动跳转查看哦 |
| 2025年终总结 | 点击该处自动跳转查看哦 |
| 一图读懂 XX? | 点击该处自动跳转查看哦 |
