Spring Boot 配置与 CLI 实用指南
1. 应用属性覆盖顺序
Spring Boot 中应用属性的覆盖顺序如下:
1. JNDI (java:comp/env)
2. System.getProperties()
3. OS 环境变量
4. RandomValuePropertySource (random.*)
5. 包外特定配置文件 (application-{profile}.jar)
6. 包内特定配置文件 (application-{profile}.jar)
7. 包外应用属性文件 (application.properties)
8. 包内应用属性文件 (application.properties)
9. @PropertySource
10. SpringApplication.setDefaultProperties
这里的“包外”和“包内”指的是,如果 JAR 库依赖中有 application.properties
(或 YAML 文件),而你的应用也有自己的 application.properties
文件,那么应用自身的文件优先级更高。
2. 命令行参数示例
以下是一个使用命令行参数设置应用属性的示例。首先,编辑主类 src/main/java/com/apress/spring/SpringBootConfigApplication.java
:
p