Issue
In which class in the source code of spring-boot or spring is the application.yml file or application.properties processed?
Solution
For spring boot (version 2.x) the application properties are loaded from the environment into the context via a PropertySourceLoader.
In for example the spring-boot-2.6.3.jar
we can find the following file:
META-INF/spring.factories
# PropertySource Loaders
org.springframework.boot.env.PropertySourceLoader=\
org.springframework.boot.env.PropertiesPropertySourceLoader,\
org.springframework.boot.env.YamlPropertySourceLoader
Where PropertiesPropertySourceLoader
loads .properties
and .xml
files, and YamlPropertySourceLoader
loads .yml
and .yaml
.
These are loaded with the SpringFactoriesLoader, which we can see in action in org.springframework.boot.context.config.ConfigFileApplicationListener
(deprecated) or org.springframework.boot.context.config.StandardConfigDataLocationResolver
(via ConfigDataEnvironmentPostProcessor
-> ConfigDataEnvironment
-> ConfigDataLocationResolvers
) :
this.propertySourceLoaders = SpringFactoriesLoader.loadFactories(PropertySourceLoader.class,
getClass().getClassLoader());
You can read in the ConfigFileApplicationListener JavaDoc that the properties are indeed loaded with this class:
EnvironmentPostProcessor that configures the context environment by loading properties from well known file locations. By default properties will be loaded from 'application.properties' and/or 'application.yml' files in the following locations: file:./config/ file:./config/*/ file:./ classpath:config/ classpath: ...
If you're interested in context loading from the environment in spring(boot), I suggest you setup your project with maven, download the sources jars, and have a look around in the mentioned factories
file. You will find more relevant code in the org.springframework.boot.env
and org.springframework.boot.context
(config
and properties
) packages.
Answered By - slindenau
Answer Checked By - David Goodson (JavaFixing Volunteer)