Spring Boot Configuration Loading sequence detailed

Source: Internet
Author: User

Using Spring Boot involves a variety of configurations, such as development, testing, and at least 3 sets of configuration information on the wire. Spring Boot can easily help us use the same code to make development, testing, and online environments use different configurations.

In Spring Boot, the configuration can be loaded in several ways. This chapter is based on Spring Boot 2.0.

1, properties file;

2, Yaml file;

3, System environment variables;

4, command line parameters;

Wait a minute......

We can use the values loaded in these configuration files directly in the Spring Beans, such as:

1, the use @Value of annotations directly into the corresponding values, which can get to the value of Spring Environment ;

2. Use @ConfigurationProperties annotations to bind the corresponding values to an object;

3, direct access Environment to inject for access;

There are many ways to configure properties, and Spring boot uses a unique PropertySource value that can be easily overridden for properties.

The configuration properties are loaded in the following order:

1、开发者工具 `Devtools` 全局配置参数;2、单元测试上的 `@TestPropertySource` 注解指定的参数;3、单元测试上的 `@SpringBootTest` 注解指定的参数;4、命令行指定的参数,如 `java -jar springboot.jar --name="Java技术栈"`;5、命令行中的 `SPRING_APPLICATION_JSONJSON` 指定参数, 如 `java -Dspring.application.json=‘{"name":"Java技术栈"}‘ -jar springboot.jar`6、`ServletConfig` 初始化参数;7、`ServletContext` 初始化参数;8、JNDI参数(如 `java:comp/env/spring.application.json`);9、Java系统参数(来源:`System.getProperties()`);10、操作系统环境变量参数;11、`RandomValuePropertySource` 随机数,仅匹配:`ramdom.*`;12、JAR包外面的配置文件参数(`application-{profile}.properties(YAML)`)13、JAR包里面的配置文件参数(`application-{profile}.properties(YAML)`)14、JAR包外面的配置文件参数(`application.properties(YAML)`)15、JAR包里面的配置文件参数(`application.properties(YAML)`)16、`@Configuration`配置文件上 `@PropertySource` 注解加载的参数;17、默认参数(通过 `SpringApplication.setDefaultProperties` 指定);

The higher the priority of the small number, the smaller the number will override the numeric value of the parameter, and we will verify the load order of the above configuration parameters in practice.

1. Add Java system parameters to the main application.

@Beanpublic CommandLineRunner commandLineRunner() {    return (args) -> {        System.setProperty("name", "javastack-system-properties");    };}

2. Add attributes to the Application.properties file.

name = javastack-application

3. Add attributes to the Application-dev.properties file.

name = javastack-application-dev

4. Add Test class

@RunWith(SpringRunner.class)@SpringBootTest(value = { "name=javastack-test", "sex=1" })@ActiveProfiles("dev")public class SpringBootBestPracticeApplicationTests {    @Value("${name}")    private String name;    @Test    public void test() {        System.out.println("name is " + name);    }}

Run Test unit tests, program output:

name is javastack-test

Dynamic adjustment According to the above parameters, the parameters are found to be correctly overwritten. Understanding the loading order of the various configurations of Spring Boot, we know what the problem is if the configuration is overwritten.

Spring boot configuration loading sequence The chapter is here, more Spring boot tutorials please follow our Java技术栈 public number.

Recommended: Spring Boot & Cloud's Strongest technology tutorials

Scan our public number to get more dry goods.

Spring Boot Configuration Loading sequence detailed

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.