前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Springboot之ConfigFileApplicationListener

Springboot之ConfigFileApplicationListener

作者头像
克虏伯
发布2019-08-06 15:09:42
8920
发布2019-08-06 15:09:42
举报

    Springboot版本2.0.5.release。

    如下图1所示:

                                                                                     图1

    ConfigFileApplicationListener实现了ApplicationListener,所以会收到Springboot的Event事件,如下List-1所示onApplicationEvent收到ApplicationEnvironmentPreparedEvent事件后,通过onApplicationEnvironmentPreparedEvent方法,获取spring.factories中的所有EnvironmentPostProcessor,并逐个调用EnvironmentPostProcessor的postProcessEnvironment方法。

List-1

代码语言:javascript
复制
@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

代码语言:javascript
复制
@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

代码语言:javascript
复制
...
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

代码语言:javascript
复制
# 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

代码语言:javascript
复制
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

代码语言:javascript
复制
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

代码语言:javascript
复制
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

  1. 源码https://github.com/spring-projects/spring-boot/tree/v2.0.5.RELEASE

(adsbygoogle = window.adsbygoogle || []).push({});

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Reference
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档