Spring Boot Properties
- https://cloud.spring.io/spring-cloud-gateway/reference/html/appendix.html
- https://docs.spring.io/spring-boot/docs/current/reference/html/application-properties.html
- https://docs.spring.io/spring-boot/docs/2.7.x-SNAPSHOT/reference/htmlsingle/#features.external-config
主要配置对象
配置对象都通过 Adapter/Configurer 的类进行配置, 只是对内部配置进行修改而不是直接替换.
- org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter
- org.springframework.security.config.annotation.authentication.configurers.GlobalAuthenticationConfigurerAdapter
- org.springframework.data.rest.webmvc.config.RepositoryRestConfigurerAdapter
- org.springframework.security.config.annotation.SecurityConfigurerAdapter
- org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter
- org.springframework.scheduling.annotation.AsyncConfigurer
- org.springframework.cache.annotation.CachingConfigurer
- org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer
- org.springframework.security.config.annotation.web.configurers.HeadersConfigurer
- org.springframework.boot.autoconfigure.security.oauth2.resource.JwtAccessTokenConverterConfigurer
- org.springframework.web.servlet.config.annotation.PathMatchConfigurer
- org.springframework.security.config.annotation.web.configurers.RequestCacheConfigurer
- org.springframework.transaction.annotation.TransactionManagementConfigurer
配置属性加载顺序
- 命令行参数
- 环境变量
SPRING_APPLICATION_JSON
指定的 JSON 配置或系统属性 - JNDI 属性
java:comp/env
- Java 系统属性 (System.getProperties())
- 操作系统环境变量
RandomValuePropertySource
为random.*
设置随机属性值- Jar 包外基于 profile 的应用属性 (application-{profile}.properties 或 YAML)
- Jar 包内基于 profile 的应用属性 (application-{profile}.properties 或 YAML)
- Jar 包外的应用属性配置 (application.properties 或 YAML)
- Jar 包内的应用属性配置 (application.properties 或 YAML)
@Configuration
类上的@PropertySource
- 默认属性 (通过 SpringApplication.setDefaultProperties 指定)
示例
SPRING_APPLICATION_JSON='{"foo":{"bar":"spam"}}' java -jar myapp.jar
java -Dspring.application.json='{"foo":"bar"}' -jar myapp.jar
java -jar myapp.jar --spring.application.json='{"foo":"bar"}'
查找属性文件的顺序
- 当前目录下的
/config
子目录 - 当前目录
- 类路径下的
/config
包 - 类路径根
修改
# 修改配置文件名
java -jar myproject.jar --spring.config.name=myproject
# 修改配置文件目录
java -jar myproject.jar --spring.config.location=classpath:/default.properties,classpath:/override.properties
# 通过环境变量修改文件名
SPRING_CONFIG_NAME=myproject java -jar myproject.jar
# 可同时加载多个配置名
# --spring.config.name=server-app,env
# 可在启动时激活相应配置
# --spring.profiles.active=local
# 可修改配置的搜索路径
# --spring.profiles.location=file:./conf
# 修改默认 Profile
# --spring.profiles.default=test
# 可直接传递 JSON 参数
# --spring.application.json={"redis.port":6379}
注意:
- 使用 Profile 时, include 的优先级会比当前配置的优先级高,因此在使用时建议避免使用 include 来包含默认配置.
- 指定多个 profile 时后面的会覆盖前面的
特殊配置说明
- DevTool 会尝试查找 HOME 下的
.spring-boot-devtools.properties
配置文件 spring.main
下的参数会应用到SpringApplication
上,因此可以在配置文件中配置spring.main.web_environment: false
这样的参数- Spring 启动时的处理类为
org.springframework.boot.context.config.ConfigFileApplicationListener
- 负责加载给定的配置
spring.config.name
,spring.profiles.active
,spring.profiles.location
- 负责将
spring.main
上的 属性绑定到SpringApplication
上
- 负责加载给定的配置
- 可启用日志,记录所有尝试加载的配置文件名
<logger name="org.springframework.boot.context.config.ConfigFileApplicationListener"
level="TRACE"
additivity="false">
<appender-ref ref="CONSOLE"/>
<appender-ref ref="FILE"/>
</logger>
属性文件可使用占位符
app.name=MyApp
app.description=${app.name:Default Name} is a Spring Boot application
单个 YAML 可指定多个 profile
server:
address: 192.168.1.100
---
spring:
profiles: development
server:
address: 127.0.0.1
---
spring:
profiles: production
server:
address: 192.168.1.120
配置注解
默认会开启 @EnableConfigurationProperties
注解配置, 有 @ConfigurationProperties
注解会自动使用 Environment
进行初始化.也可以直接通过 @EnableConfigurationProperties
来注册配置对象