1 Star 0 Fork 157

南风不竞 / Spring-Analysis

forked from huifer / Code-Analysis 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
Spring-PropertySourcesPropertyResolver.md 3.36 KB
一键复制 编辑 原始数据 按行查看 历史
huifer 提交于 2020-11-09 10:08 . :fire:doc: env + PropertyResolver

Spring PropertySourcesPropertyResolver

  • 类全路径: org.springframework.core.env.PropertySourcesPropertyResolver

类图 AbstractPropertyResolver.png

  • 内部属性
	@Nullable
	private final PropertySources propertySources;

关于 PropertySources 详见: PropertySources-分析

getProperty

PropertySourcesPropertyResolver 内部方法围绕着 PropertySources 进行展开其中最重要的方法: getProperty

@Nullable
protected <T> T getProperty(String key, Class<T> targetValueType, boolean resolveNestedPlaceholders) {
   if (this.propertySources != null) {
      // 循环
      for (PropertySource<?> propertySource : this.propertySources) {
         if (logger.isTraceEnabled()) {
            logger.trace("Searching for key '" + key + "' in PropertySource '" +
                  propertySource.getName() + "'");
         }
         // 获取对象结果
         Object value = propertySource.getProperty(key);
         if (value != null) {
            // 是否需要处理嵌套
            // 是否是 string 类型
            if (resolveNestedPlaceholders && value instanceof String) {
               // 嵌套获取数据
               value = resolveNestedPlaceholders((String) value);
            }
            // 日志
            logKeyFound(key, propertySource, value);
            // 类型转换
            return convertValueIfNecessary(value, targetValueType);
         }
      }
   }
   if (logger.isTraceEnabled()) {
      logger.trace("Could not find key '" + key + "' in any property source");
   }
   return null;
}

resolveNestedPlaceholders

  • 处理占位符,获取真实数据

  • org.springframework.core.env.AbstractPropertyResolver#resolveNestedPlaceholders

protected String resolveNestedPlaceholders(String value) {
   return (this.ignoreUnresolvableNestedPlaceholders ?
         resolvePlaceholders(value) : resolveRequiredPlaceholders(value));
}
  • 上面的方法最后都会指向: org.springframework.core.env.AbstractPropertyResolver#doResolvePlaceholders 方法

    private String doResolvePlaceholders(String text, PropertyPlaceholderHelper helper) {
       return helper.replacePlaceholders(text, this::getPropertyAsRawString);
    }

PropertyPlaceholderHelper 解析请看: Spring-PropertyPlaceholderHelper

convertValueIfNecessary

  • 值类型转换

  • org.springframework.core.env.AbstractPropertyResolver#convertValueIfNecessary

@SuppressWarnings("unchecked")
@Nullable
protected <T> T convertValueIfNecessary(Object value, @Nullable Class<T> targetType) {
   if (targetType == null) {
      // 类型强制转换
      return (T) value;
   }
   ConversionService conversionServiceToUse = this.conversionService;
   if (conversionServiceToUse == null) {
      // Avoid initialization of shared DefaultConversionService if
      // no standard type conversion is needed in the first place...
      if (ClassUtils.isAssignableValue(targetType, value)) {
         return (T) value;
      }
      // 默认的转换接口
      conversionServiceToUse = DefaultConversionService.getSharedInstance();
   }
   // 通过默认接口进行转换
   return conversionServiceToUse.convert(value, targetType);
}
Java
1
https://gitee.com/limengcanyu/spring-analysis.git
git@gitee.com:limengcanyu/spring-analysis.git
limengcanyu
spring-analysis
Spring-Analysis
master

搜索帮助