Springboot版本2.0.5.release。
如下圖1所示:
圖1
ConfigFileApplicationListener實作了ApplicationListener,是以會收到Springboot的Event事件,如下List-1所示onApplicationEvent收到ApplicationEnvironmentPreparedEvent事件後,通過onApplicationEnvironmentPreparedEvent方法,擷取spring.factories中的所有EnvironmentPostProcessor,并逐個調用EnvironmentPostProcessor的postProcessEnvironment方法。
List-1
@Override
public void onApplicationEvent(ApplicationEvent event) {
if (event instanceof ApplicationEnvironmentPreparedEvent) {
onApplicationEnvironmentPreparedEvent(
(ApplicationEnvironmentPreparedEvent) event);
}
if (event instanceof ApplicationPreparedEvent) {
onApplicationPreparedEvent(event);
}
}
private void onApplicationEnvironmentPreparedEvent(
ApplicationEnvironmentPreparedEvent event) {
List<EnvironmentPostProcessor> postProcessors = loadPostProcessors();
postProcessors.add(this);
AnnotationAwareOrderComparator.sort(postProcessors);
for (EnvironmentPostProcessor postProcessor : postProcessors) {
postProcessor.postProcessEnvironment(event.getEnvironment(),
event.getSpringApplication());
}
}
List<EnvironmentPostProcessor> loadPostProcessors() {
return SpringFactoriesLoader.loadFactories(EnvironmentPostProcessor.class,
getClass().getClassLoader());
}
List-1中onApplicationEnvironmentPreparedEvent方法裡"postProcessors.add(this)",把目前執行個體也加入其中,是以來看看ConfigFileApplicationListener的postProcessEnvironment,如下List-2所示:
List-2
@Override
public void postProcessEnvironment(ConfigurableEnvironment environment,
SpringApplication application) {
addPropertySources(environment, application.getResourceLoader());
}
...
protected void addPropertySources(ConfigurableEnvironment environment,
ResourceLoader resourceLoader) {
RandomValuePropertySource.addToEnvironment(environment);
new Loader(environment, resourceLoader).load();
}
List-2中的Loader是個内部類,在ConfigFileApplicationListener中,Loader的構造方法如下List-3所示,從spring.factories中獲得PropertySourceLoader,實作類有倆個,即PropertiesPropertySourceLoader和YamlPropertySourceLoader。
List-3
...
Loader(ConfigurableEnvironment environment, ResourceLoader resourceLoader) {
this.environment = environment;
this.resourceLoader = (resourceLoader != null) ? resourceLoader
: new DefaultResourceLoader();
this.propertySourceLoaders = SpringFactoriesLoader.loadFactories(
PropertySourceLoader.class, getClass().getClassLoader());
}
...
List-4
# PropertySource Loaders
org.springframework.boot.env.PropertySourceLoader=\
org.springframework.boot.env.PropertiesPropertySourceLoader,\
org.springframework.boot.env.YamlPropertySourceLoader
PropertiesPropertySourceLoader如下List-5,它支援的有properties和xml檔案,并最終解析為Properties,封轉到PropertiesPropertySource中。
List-5
public class PropertiesPropertySourceLoader implements PropertySourceLoader {
public String[] getFileExtensions() {
return new String[]{"properties", "xml"};
}
public PropertySource<?> load(String name, Resource resource, String profile) throws IOException {
if (profile == null) {
Properties properties = PropertiesLoaderUtils.loadProperties(resource);
if (!properties.isEmpty()) {
return new PropertiesPropertySource(name, properties);
}
}
return null;
}
}
YamlPropertySourceLoader如下List-6所示,支援的檔案有yml和yaml,将yaml檔案内容解析為map,之後封裝到MapPropertySource。
List-6
public class YamlPropertySourceLoader implements PropertySourceLoader {
public String[] getFileExtensions() {
return new String[]{"yml", "yaml"};
}
public PropertySource<?> load(String name, Resource resource, String profile) throws IOException {
if (ClassUtils.isPresent("org.yaml.snakeyaml.Yaml", (ClassLoader)null)) {
YamlPropertySourceLoader.Processor processor = new YamlPropertySourceLoader.Processor(resource, profile);
Map<String, Object> source = processor.process();
if (!source.isEmpty()) {
return new MapPropertySource(name, source);
}
}
return null;
}
...
從ConfigFileApplicationListener中可以看到,Springboot通過事件觸發解析配置檔案的操作(如下List-7中的listeners.environmentPrepared(environment)處),其中properties和xml檔案解析到JDK的Properties中,之後加入到Springboot的evironment中;yml和yaml檔案解析到JDK的map中,之後加入到evironment中。
List-7
private ConfigurableEnvironment prepareEnvironment(SpringApplicationRunListeners listeners,ApplicationArguments applicationArguments) {
// Create and configure the environment
ConfigurableEnvironment environment = getOrCreateEnvironment();
configureEnvironment(environment, applicationArguments.getSourceArgs());
listeners.environmentPrepared(environment);
bindToSpringApplication(environment);
if (!this.isCustomEnvironment) {
environment = new EnvironmentConverter(getClassLoader())
.convertEnvironmentIfNecessary(environment, deduceEnvironmentClass());
}
ConfigurationPropertySources.attach(environment);
return environment;
}
...
通過上面這些步驟,Springboot将配置檔案的内容全部解析到evironment中。
Reference
- 源碼https://github.com/spring-projects/spring-boot/tree/v2.0.5.RELEASE
轉載于:https://my.oschina.net/u/2518341/blog/3083051