首页 > 编程语言 > 详细

SpringBoot 配置解析

时间:2020-01-02 14:28:28      阅读:112      评论:0      收藏:0      [点我收藏+]

SpringBoot 的配置解析是通过 Environment 来实现的。

Environment 本身实现了 PropertyResolver 接口,最终会委托给 PropertySourcesPropertyResolver 去解析配置。

org.springframework.core.env.PropertySourcesPropertyResolver.getProperty

 1 protected <T> T getProperty(String key, Class<T> targetValueType, boolean resolveNestedPlaceholders) {
 2     if (this.propertySources != null) {
 3         for (PropertySource<?> propertySource : this.propertySources) { // 1. 循环 PropertySources
 4             if (logger.isTraceEnabled()) {
 5                 logger.trace("Searching for key ‘" + key + "‘ in PropertySource ‘" +
 6                         propertySource.getName() + "‘");
 7             }
 8             Object value = propertySource.getProperty(key); // 2. 从 PropertySource 中获取 key 对应的配置
 9             if (value != null) {
10                 if (resolveNestedPlaceholders && value instanceof String) {
11                     value = resolveNestedPlaceholders((String) value); // 3. 解析占位符 ${}
12                 }
13                 logKeyFound(key, propertySource, value);
14                 return convertValueIfNecessary(value, targetValueType); // 4. 转换成指定类型
15             }
16         }
17     }
18     if (logger.isTraceEnabled()) {
19         logger.trace("Could not find key ‘" + key + "‘ in any property source");
20     }
21     return null;
22 }

 

技术分享图片

 

技术分享图片

 

技术分享图片

SpringBoot 配置解析

原文:https://www.cnblogs.com/kevin-yuan/p/12132589.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!