--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
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:
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:
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);
}
};
}
}
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!"}
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