官方文档: Spring Boot Reference Documentation
老文档 :25. Externalized Configuration
加载顺序
- 开启 DevTools 时,~/.spring-boot-devtools.properties
- 测试类上的 @TestPropertySource 注解
- @SpringBootTest#properties 属性
- 命令⾏参数( --server.port=9000 )
- SPRING_APPLICATION_JSON 中的属性
- ServletConfig 初始化参数
- ServletContext 初始化参数
- java:comp/env 中的 JNDI 属性
- System.getProperties()
通过 java -D 指定的参数 - 操作系统环境变量
- random.* 涉及到的 RandomValuePropertySource
可以获取随机值
profile会比不加profile的优先 - jar 包外部的 application-{profile}.properties 或 .yml
- jar 包内部的 application-{profile}.properties 或 .yml
- jar 包外部的 application.properties 或 .yml
- jar 包内部的 application.properties 或 .yml
- @Configuration 类上的 @PropertySource
- SpringApplication.setDefaultProperties() 设置的默认属性
application.properties可放的位置
- ./config 当前路径
- ./ 当前路径
- CLASSPATH /config classpth中
- CLASSPATH / classpth中
修改配置文件名称和位置
在org.springframework.boot.context.config.ConfigFileApplicationListener中
- spring.config.name
- spring.config.location
- spring.config.additional-location
SpringBoot对Relaxed Binding的支持
Spring Boot使用一些relaxed rules在@ConfigurationProperties bean上绑定环境配置,所以环境属性名和bean属性名不需要精确匹配.
Property | Note |
acme.my-project.person.first-name | 横线规则Kebab case, which is recommended for use in .properties and .yml files. |
acme.myProject.person.firstName | 标准驼峰 Standard camel case syntax. |
acme.my_project.person.first_name | 下划线Underscore notation, which is an alternative format for use in .properties and .yml files. |
ACME_MYPROJECT_PERSON_FIRSTNAME | 全大写下划线Upper case format, which is recommended when using system environment variables. |