一、接口继承图
二、接口类功能概述与实现
PropertyResolver接口定义了按属性名获取对应属性配置的接口以及解析字符串中的属性表达式的接口,如${foo}/abc,foo对应的属性值为123,解析后为123/abc。
Environment接口继承自PropertyResolver,增加了获取Profiles相关接口
ConfigurablePropertyResolver接口继承自PropertyResolver,增加实现PropertyResolver所需的辅助接口
AbstractPropertyResolver是ConfigurablePropertyResolver接口的抽象实现类,提供了大部分接口方法的默认实现,将核心的getProperty(String key, Class<T> targetType)方法留给子类实现,resolvePlaceholders(String text)方法则由PropertyPlaceholderHelper提供默认实现。PropertySourcesPropertyResolver是该类的默认实现类,从全局变量PropertySources对象获取属性名key对应的属性值,查找时会遍历PropertySources中包含的多个PropertySource,直到找到对应的属性值。
ConfigurableEnvironment继承自Environment和ConfigurablePropertyResolver两个接口,并增加了Profiles维护,获取系统属性,系统环境变量的实用接口。
AbstractEnvironment为ConfigurableEnvironment接口的抽象实现类,提供了该接口的所有方法的默认实现,其中ConfigurablePropertyResolver的接口PropertySourcesPropertyResolver实例实现,将核心的MutablePropertySources全局变量包含的多个PropertySources的初始化交给子类实现,即protected方法customizePropertySources(MutablePropertySources propertySources)。
StandardEnvironment是AbstractEnvironment的默认实现子类,覆写了customizePropertySources方法,如下:
@Override
protected void customizePropertySources(MutablePropertySources propertySources) {
propertySources.addLast(new MapPropertySource(SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME, getSystemProperties()));
propertySources.addLast(new SystemEnvironmentPropertySource(SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, getSystemEnvironment()));
}
即spring 容器查找环境属性配置时会优