Spring Boot External setting priority

Overview

--Translated the document about the priority of the setting value of Externalized Configuration of Spring Boot 2.2.0.M4 into Japanese. --Original: Spring Boot Reference Documentation --4.2. Externalized Configuration

Japanese translation

External settings

Spring Boot can be externalized so that the same application code can run in different environments. You can externalize your settings using property files, YAML files, environment variables, and command line arguments. Settings can be injected directly into the bean using the @Value annotation, accessed through Spring's Environment, or bound to structured objects through @ConfigurationProperties.

Spring Boot uses a special ordering mechanism called PropertySource. It is designed to allow flexible overwriting of settings.

The settings are applied in the following order of priority:

  1. Devtools global configuration properties in your home directory (~ /.spring-boot-devtools.properties if devtools is active).
  2. Test @TestPropertySource annotation.
  3. Test property attributes. It can be used with @SpringBootTest and test annotations to test specific parts of your application.
  4. Command line arguments.
  5. Properties set to SPRING_APPLICATION_JSON (inline JSON embedded in environment variables and system properties).
  6. ServletConfig initialization parameters.
  7. ServletContext initialization parameters.
  8. JNDI attribute from java: comp / env.
  9. Java system properties (System.getProperties ()).
  10. OS environment variables
  11. RandomValuePropertySource that has only random. * Properties.
  12. Application properties (application- {profile} .properties and YAML) that are for a particular profile and are not packaged in a JAR.
  13. Application properties (application- {profile} .properties and YAML) that are packaged in a JAR for a particular profile.
  14. Application properties (application.properties and YAML) that are not packaged in the JAR.
  15. Application properties (application.properties and YAML) packaged in the JAR.
  16. @PropertySource annotation of the @Configuration class.
  17. Default properties (settings specified in SpringApplication.setDefaultProperties).

Original (English)

4.2. Externalized Configuration

Spring Boot lets you externalize your configuration so that you can work with the same application code in different environments. You can use properties files, YAML files, environment variables, and command-line arguments to externalize configuration. Property values can be injected directly into your beans by using the @Value annotation, accessed through Spring’s Environment abstraction, or be bound to structured objects through @ConfigurationProperties.

Spring Boot uses a very particular PropertySource order that is designed to allow sensible overriding of values. Properties are considered in the following order:

  1. Devtools global settings properties on your home directory (~/.spring-boot-devtools.properties when devtools is active).
  2. @TestPropertySource annotations on your tests.
  3. properties attribute on your tests. Available on @SpringBootTest and the test annotations for testing a particular slice of your application.
  4. Command line arguments.
  5. Properties from SPRING_APPLICATION_JSON (inline JSON embedded in an environment variable or system property).
  6. ServletConfig init parameters.
  7. ServletContext init parameters.
  8. JNDI attributes from java:comp/env.
  9. Java System properties (System.getProperties()).
  10. OS environment variables.
  11. A RandomValuePropertySource that has properties only in random.*.
  12. Profile-specific application properties outside of your packaged jar (application-{profile}.properties and YAML variants).
  13. Profile-specific application properties packaged inside your jar (application-{profile}.properties and YAML variants).
  14. Application properties outside of your packaged jar (application.properties and YAML variants).
  15. Application properties packaged inside your jar (application.properties and YAML variants).
  16. @PropertySource annotations on your @Configuration classes.
  17. Default properties (specified by setting SpringApplication.setDefaultProperties).

Operation check for priority order

Run the sample code to see the priority.

application.properties

Describe the setting value in application.properties.

message=Hello, application.properties!

Application.java

package sample;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

  public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
  }
}

Controller.java

Set value message is returned in JSON.

package sample;

import java.util.HashMap;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class Controller {

  @Value("${message}")
  private String message;

  @RequestMapping("/")
  public HashMap<String, Object> index() {
    return new HashMap<String, Object>() {
      {
        put("message", message);
      }
    };
  }
}

Example where application.properties is applied

Run the Spring Boot server with the java command.

$ java -jar target/sample.jar

When accessing the server, the message setting value described in application.properties is returned.

$ curl http://localhost:8080/
{"message":"Hello, application.properties!"}

Example of applying Java system properties

Specify JSON in the Java system property spring.application.json with -D and run the Spring Boot server with the java command.

$ java -Dspring.application.json='{"message":"Hello, Java System properties!"}' -jar target/sample.jar

Accessing the server returns the message setting specified in the Java system properties.

$ curl http://localhost:8080/
{"message":"Hello, Java System properties!"}

Java system properties take precedence over application.properties. This is because the 9th and 15th priorities in the previous list are higher than the 9th. "9. Java System Properties (System.getProperties ())." "15. Application properties packaged in JAR (application- {profile} .properties and YAML)."

Recommended Posts

Spring Boot External setting priority
Static file access priority in Spring boot
Challenge Spring Boot
Spring Boot Form
Spring Boot, Doma2, Gradle initial setting summary
Spring Boot Memorandum
gae + spring boot
SPRING BOOT learning record 01
Spring Boot + Heroku Postgres
Spring boot memo writing (1)
First Spring Boot (DI)
Spring Boot2 cheat sheet
Spring Boot Servlet mapping
Spring Boot learning procedure
Spring boot memo writing (2)
[Spring Boot] DataSourceProperties $ DataSourceBeanCreationException
Spring Boot 2.3 Application Availability
Spring boot tutorials Topics
Download with Spring Boot
Include external jar in package with Spring boot2 + Maven3
[Spring Boot] Environment construction (macOS)
Set context-param in Spring Boot
Spring + MyBatis connection setting method
Try Spring Boot from 0 to 100.
Generate barcode with Spring Boot
Hello World with Spring Boot
Spring Boot on Microsoft Azure
Implement GraphQL with Spring Boot
Spring RestTemplate Config Timeout setting
Get started with Spring boot
Hello World with Spring Boot!
Spring Boot 2 multi-project in Gradle
[Spring Boot] Web application creation
spring boot port duplication problem
Run LIFF with Spring Boot
SNS login with Spring Boot
Spring Boot Hot Swapping settings
[Java] Thymeleaf Basic (Spring Boot)
Introduction to Spring Boot ① ~ DI ~
File upload with Spring Boot
Spring Boot starting with copy
CICS-Run Java application-(4) Spring Boot application
Spring Boot starting with Docker
Spring Boot + Springfox springfox-boot-starter 3.0.0 Use
Spring Boot DB related tips
Hello World with Spring Boot
Set cookies with Spring Boot
[Spring Boot] Easy paging recipe
Use Spring JDBC with Spring Boot
Docker × Spring Boot environment construction
Major changes in Spring Boot 1.5
Add module with Spring Boot
Getting Started with Spring Boot
NoHttpResponseException in Spring Boot + WireMock
[Spring Boot] Send an email
Spring Boot performance related settings
Introduction to Spring Boot Part 1
Try using Spring Boot Security
[Java] [Spring] Spring Boot 1.4-> 1.2 Downgrade Note
Try Spring Boot on Mac
Create microservices with Spring Boot