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

What is the difference between putting a property in application.yml or bootstrap.yml on spring boot? In the case of logging.config, the application works differently.

+192
java spring spring-boot spring-cloud
07 Oct '15 at 16:13
source share
6 answers

I just asked the Spring Cloud guys and thought I should share the information I'm here.

bootstrap.yml loads before application.yml .

It is commonly used for the following:

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

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

+242
Feb 22 '16 at 3:03
source share

bootstrap.yml or bootstrap.properties

It is only used / necessary if you are using Spring Cloud , and your application configuration is stored on a remote configuration server (e.g. Spring Cloud Config Server).

From the documentation:

A Spring Cloud application works by creating a “bootstrap” context, which is the parent context for the main application. Out of the box, he is responsible for loading configuration properties from external sources , as well as decrypting properties in local external configuration files.

Please note that bootstrap.yml or bootstrap.properties may contain additional configuration (for example, by default), but usually you only need to set the bootstrap configuration here.

It usually contains two properties:

  • configuration server location ( spring.cloud.config.uri )
  • application name ( spring.application.name )

When launched, Spring Cloud makes an HTTP call to the configuration server with the application name and returns this application configuration back.

application.yml or application.properties

Contains the standard configuration of the application - usually the default configuration, since any configuration obtained during the boot process will override the configuration defined here.

+70
Apr 07 '17 at 3:31 on
source share

This answer was very beautifully explained in the book " Interview Questions on Micro Services for Java Developers (Spring Boot, Spring Cloud, Cloud Native Applications) " by Munish Chandel, version 1.30, 03/25/2018.

The following content has been taken from this book, and the overall merit in this answer belongs to the author of the book, i.e. E. Munish Chandel

application.yml

The application.yml / application.properties file is specific to Spring Boot applications. If you do not change the location of the application’s external properties, spring loading will always load application.yml from the following location:

 /src/main/resources/application.yml 

You can store all external properties for your application in this file. General properties available in any Spring Boot project can be found at: https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html. These properties can be configured as follows. according to your application needs. An example file is shown below:

spring: application: name: foobar data source: driverClassName: com.mysql.jdbc.Driver url: jdbc: mysql: // localhost / test server: port: 9000

bootstrap.yml

On the other hand, the bootstrap.yml file is specific to spring-cloud-config and is loaded before application.yml.

bootstrap.yml is only required if you are using Spring Cloud and your microservice configuration is stored on a remote Spring Cloud Config server.

Important points about bootstrap.yml

  1. When used with a Spring Cloud Config server, you must specify the application name and location of the git configuration using the following properties.
 spring.application.name: "application-name"
 spring.cloud.config.server.git.uri: "git-uri-config"

  1. When used with microservices (except for the cloud configuration server), we need to specify the application name and location of the configuration server using the following properties
 spring.application.name: 
 spring.cloud.config.uri: 
  1. This properties file may contain other configurations related to the Spring Cloud environment, such as the location of the eureka server, properties related to encryption / decryption.

Once launched, Spring Cloud makes an HTTP (S) call to the Spring Cloud Config server with the application name and returns the configuration of these applications.

application.yml contains the default configuration for the microservice, and any configuration received (from the cloud configuration server) during the boot process will override the configuration defined in application.yml

+16
04 Oct '18 at
source share

Bootstrap.yml is used to get the configuration from the server. It could be for a Spring cloud application or for others. Usually it looks like this:

 spring: application: name: "app-name" cloud: config: uri: ${config.server:http://some-server-where-config-resides} 

When we launch the application, it tries to connect to this server and read the configuration based on the spring profile mentioned in the run / debug configuration. bootstrap.yml loads the first

If the server is unavailable, the application may not even continue to work. However, if configurations matching the profile are present locally, the server settings are overridden.

Good approach:

Maintain a separate profile for the local one and run the application using different profiles.

+2
May 25 '18 at 5:24
source share

Just my 2 cents here ..

Bootstrap.yml or Bootstrap.properties is used to get configuration from Spring Cloud Server.

For example, in My Bootstrap.properties file, I have the following Config

 spring.application.name=Calculation-service spring.cloud.config.uri=http://localhost:8888 

When the application starts, it tries to get the configuration for the service by connecting to http: // localhost: 8888, and looks at the Calculation-service.properties presented on the Spring Cloud Config server.

You can check the same from the calculation-service logs at startup

INFO 10988 --- [ restartedMain] cccConfigServicePropertySourceLocator: Fetching config from server at: http://localhost:8888

+2
Jul 22 '18 at 1:17
source share

Well, I completely agree with the already existing answers on this question:

  • bootstrap.yml used to save parameters that indicate where the remote configuration is located, and with this remote configuration the Bootstrap application context is created.

In fact, it can also store ordinary properties in the same way as the application.yml file. But pay attention to this complicated thing:

  • If you put properties in bootstrap.yml , they will get a lower priority than almost any other property sources, including application.yml. As described here .

Let's clarify that there are two kinds of properties related to bootstrap.yml :

  • Properties that are loaded during the boot phase. We use bootstrap.yml to find the property holder (file system, git repository, or something else), and the properties that we get in this way have high priority, so they cannot be overridden by the local configuration. As described here .
  • Properties that are in bootstrap.yml . As explained earlier, they will receive a lower priority. Use them to set defaults, maybe this is a good idea.

So the differences between putting a property in application.yml or bootstrap.yml on spring boot:

  • Properties for loading configuration files in the boot phase can only be placed in bootstrap.yml .
  • As with all other property types, place them in application.yml will get a higher priority.
+1
Jun 02 '19 at 12:24
source share



All Articles