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 }
原文:https://www.cnblogs.com/kevin-yuan/p/12132589.html