Differencebetween Putting a Property on Application.Yml or Bootstrap.Yml in Spring Boot

What is the difference between putting a property on application.yml or bootstrap.yml in spring boot?

I have just asked the Spring Cloud guys and thought I should share the info I have here.

bootstrap.yml is loaded before application.yml.

It is typically used for the following:

  • when using Spring Cloud Config Server, you should specify spring.application.name and spring.cloud.config.server.git.uri inside bootstrap.yml
  • some encryption/decryption information

Technically, bootstrap.yml is loaded by a parent Spring ApplicationContext. That parent ApplicationContext is loaded before the one that uses application.yml.

In spring-cloud project, is it ok to replace application.yml with bootstrap.yml totally?

Basically idea is to get configurations from ConfigServer using Spring Cloud Config. But sometimes we need some configurations e.g. spring.application.name upfront so, I'm using those configurations in bootstrap.yml. Which generally overrides what's in an application.yml [if present]). Reference

Plus, yes you can put all your configurations in bootstrap.yml and it works simply fine. I've tested with more than four microservices.

application.yml vs application.properties for Spring Boot

Well, they are just different data formats. Which one's nicer and easier to read? That's obviously subjective. Here's a useful blog post.

As far as spring-boot configuration is concerned, note that there's only one documented shortcoming of using YAML. Per the documentation:

YAML files can’t be loaded via the @PropertySource annotation. So in the case that you need to load values that way, you need to use a properties file.

Spring Boot yml file read order

As Spring doc mentions

Profile specific properties are loaded from the same locations as
standard application.properties, with profiles specific files
overriding the default ones

This would mean that first the application.yml is read and then the application_dev.yml is read and overrides values from the default application.yml if needed.

Same for bootstrap.yml and bootstrap-dev.yml

Also as you can see here

bootstrap.yml is loaded before application.yml.

So to answer your question the order should be

  1. bootstrap.yml
  2. bootstrap_dev.yml
  3. application.yml
  4. application_dev.yml

In which class in the source code of spring-boot or spring is the application.yml or application.properties file processed?

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.

What is bootstrap yaml in spring boot

bootstrap.yml is used in spring cloud
It is starting before application.yml

It is almost use with spring cloud config server
Spring cloud config server is server which is used to externilize your application configuration.
And when starting your application bootstrap.yml will take the configuration from spring cloud config server.
It also can use encrypting and decrypting some information by :

'{cipher}someyour encoded text'

and server will decode it while pulling the configurations
But you need to create jks
You can reach the documentation for more information about spring cloud :

https://spring.io/guides/gs/centralized-configuration/

Configuration files in spring boot will be loaded in such order:

 1. src/main/resources/bootstrap.yml
2. src/main/resources/application.yml
3. config/application.yml


Related Topics



Leave a reply



Submit