Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the function of ConfigFileApplicationListener in Springboot

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

Today, I will talk to you about the role of ConfigFileApplicationListener in Springboot. Many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.

This is shown in figure 1 below:

Figure 1

ConfigFileApplicationListener implements ApplicationListener, so it will receive the Event event of Springboot. After onApplicationEvent receives the ApplicationEnvironmentPreparedEvent event as shown in List-1 below, it obtains all the EnvironmentPostProcessor in spring.factories through the onApplicationEnvironmentPreparedEvent method, and calls the postProcessEnvironment method of EnvironmentPostProcessor one by one.

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 ());}

Add the current instance to "postProcessors.add (this)" in the onApplicationEnvironmentPreparedEvent method in List-1, so take a look at the postProcessEnvironment of ConfigFileApplicationListener, as shown in 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 ();}

Loader in List-2 is an inner class. In ConfigFileApplicationListener, the construction method of Loader is shown in List-3, and the PropertySourceLoader is obtained from spring.factories. There are two implementation classes, PropertiesPropertySourceLoader and 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 follows List-5, which supports properties and xml files, and eventually parses to Properties, which is sealed and transferred to 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 as shown in List-6 below. The supported files are yml and yaml. The contents of the yaml file are parsed into map, and then encapsulated into 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;}.

You can see from ConfigFileApplicationListener that Springboot triggers the operation of parsing the configuration file (at listeners.environmentPrepared (environment) in List-7 below), where properties and xml files are parsed into JDK's Properties and then added to Springboot's evironment; yml and yaml files are parsed into JDK's map and then added to 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 ()). ConvertEnvironment getClassLoader (environment, deduceEnvironmentClass ());} ConfigurationPropertySources.attach (environment) Return environment;}... After reading the above, do you have any further understanding of the role of ConfigFileApplicationListener in Springboot? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report