千家信息网

Springboot中ConfigFileApplicationListener的作用是什么

发表于:2025-12-01 作者:千家信息网编辑
千家信息网最后更新 2025年12月01日,今天就跟大家聊聊有关Springboot中ConfigFileApplicationListener的作用是什么,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文
千家信息网最后更新 2025年12月01日Springboot中ConfigFileApplicationListener的作用是什么

今天就跟大家聊聊有关Springboot中ConfigFileApplicationListener的作用是什么,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

如下图1所示:

图1

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

List-1

@Overridepublic void onApplicationEvent(ApplicationEvent event) {        if (event instanceof ApplicationEnvironmentPreparedEvent) {                onApplicationEnvironmentPreparedEvent(                                (ApplicationEnvironmentPreparedEvent) event);        }        if (event instanceof ApplicationPreparedEvent) {                onApplicationPreparedEvent(event);        }}private void onApplicationEnvironmentPreparedEvent(                ApplicationEnvironmentPreparedEvent event) {        List postProcessors = loadPostProcessors();        postProcessors.add(this);        AnnotationAwareOrderComparator.sort(postProcessors);        for (EnvironmentPostProcessor postProcessor : postProcessors) {                postProcessor.postProcessEnvironment(event.getEnvironment(),                                event.getSpringApplication());        }}List loadPostProcessors() {        return SpringFactoriesLoader.loadFactories(EnvironmentPostProcessor.class,                        getClass().getClassLoader());}

List-1中onApplicationEnvironmentPreparedEvent方法里"postProcessors.add(this)",把当前实例也加入其中,所以来看看ConfigFileApplicationListener的postProcessEnvironment,如下List-2所示:

List-2

@Overridepublic 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 Loadersorg.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 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中ConfigFileApplicationListener的作用是什么有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注行业资讯频道,感谢大家的支持。

文件 内容 方法 事件 支持 作用 实例 更多 知识 篇文章 行业 资讯 资讯频道 频道 进一 封装 有关 配置 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 虚拟服务器管理器在哪里打开 江西现代软件开发直销价格 数据库新建表关键字 psv无法在限定时间内连接服务器或设备 数据库的关键字和约束怎么用 饥荒建房服务器启动失败 深圳电力储能网络安全 英雄联盟服务器谁负责 有软件开发的学校有哪些东西 省分行软件开发干什么 实验1软件开发团队与选题 家庭网络安全的措施 adb获取性能数据库 浙江计算机应用软件开发 净深圳市求聘网络技术工程师 数据库开发笔试题 视频服务器ip shodan搜索服务器 枣庄质量好的联想服务器总代理 分光测色仪软件开发 抢红包显示服务器错误怎么回事 内蒙古计算机网络技术专升本 网络安全教育手抄报 图片 深圳oa软件开发服务 软件开发有什么痛点 塔城服务器维保费用 成都小学智慧校园软件开发 网络安全等级测评资质要求 网络安全法心得体会400字 无锡操作性能好服务器生产商
0