根据官网和极客时间丁雪丰老师 Spring 专课,总结归纳下 SpringBoot 加载配置的顺序,以便以后遇到问题了好排查。
外化配置优先级顺序:
- 开启 DevTools 时,~/.spring-boot-devtools.properties;
- 测试类上的 @TestPropertySource 注解;
- @SpringBootTest#properties 注解;
- 命令行参数,如 –server.port=8888;
- SPRING_APPLICATION_JSON 属性;
- ServletConfig 初始化参数;
- ServletContext 初始化参数;
- Java:comp/env 中的JNDI 属性;
- System.getProperties()属性;
- 操作系统环境变量;
- random.* RrandomValuePropertySource,如 my.age=${random.int}
- jar 包外部的 application-(profile).properties 或(yml)
- jar 包内部的 application-(profile).properties 或(yml)
- jar 包外部的 application.properties 或(yml)
- jar 包内部的 application.properties 或(yml)
- @Configuration 类上的 @PropertySpurce;
- SpringApplication.setDefaultProperties() 设置默认属性。