In which class in the source code of spring-boot or spring is the application.yml file or application.properties processed?
CodePudding user response:
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.
CodePudding user response:
You can find your application.yml or application.properties at the src/main/resources. You can have as many as possible configurations for your spring boot application for every case. Lets assume that you have 3 local-profiles like demo, production and server, so you made 3 configuration and assumingyou set for active profile the demo at the application.yml . I hope you get the idea. Its the first thing that actually is running before the springboot is up.