项目地址https://gitee.com/better-code/SpringBoot-Example/springboot04/
application.properties官文属性文档
application.properties属性大全1
application.properties属性大全2
加载配置文件
SpringApplication
默认从 以下位置加载 application.properties
文件
- 当前目录下的
/config
子目录 - 当前目录
- classpath 下的
/config
包 - classpath 根目录(root)
可以通过指定 spring.config.name
环境属性来修改配置文件名称,也可以使用 spring.config.location
环境属性加载一个明确路径的配置文件。
java -jar xxx.jar --spring.config.name=myconfig
java -jar xxx.jar --spring.config.location=classpath:/default.properties,classpath:/override.properties
属性值的合理覆盖
Spring Boot 允许对属性值进行合理的覆盖,属性会以如下的顺序进行设置:
- home目录下的devtools全局设置属性( ~/.spring-boot-devtools.properties ,如果devtools激活)。
- 测试用例上的@TestPropertySource注解。
- 测试用例上的@SpringBootTest#properties注解。
- 命令行参数
- 来自 SPRING_APPLICATION_JSON 的属性(环境变量或系统属性中内嵌的内JSON)。
- ServletConfig 初始化参数。
- ServletContext 初始化参数。
- 来自于 java:comp/env 的JNDI属性。
- Java系统属性(System.getProperties())。
- 操作系统环境变量。
- RandomValuePropertySource,只包含 random.* 中的属性。
- 没有打进jar包的Profile-specific应用属性( application-{profile}.properties 和YAML变量)。
- 打进jar包中的Profile-specific应用属性( application-{profile}.properties 和YAML变量)。
- 没有打进jar包的应用配置( application.properties 和YAML变量)。
- 打进jar包中的应用配置( application.properties 和YAML变量)。
- @Configuration 类上的 @PropertySource 注解。
- 默认属性(使用 SpringApplication.setDefaultProperties 指定。
测试
import org.springframework.stereotype.*
import org.springframework.beans.factory.annotation.*
@Component
public class MyBean {
@Value("${name}")
private String name;
// ...
}
classpath 路径下的 application.properties
内容如下:
name="springboot05 dev"
当在新的环境中运行时,可以在jar包外提供一个 application.properties 覆盖 name 属性。
对于一次性的测试,你可以使用特定的命令行开关启动应用(比如java -jar app.jar --name="springboot05 test"
)。
在springboot项目中,可以使用properties文件、YAML文件、环境变量和命令行参数来配置。
YAML
YAML是JSON的一个超集,也是一种方便的定义层次配置数据的格式。只要将SnakeYAML 库放到classpath下, SpringApplication 就会自动支持YAML,以作为properties的替换。
注:如果你使用'Starters',添加 spring-boot-starter 依赖会自动加载SnakeYAML
列表
# 配置列表
myconfig:
servers:
- dev.bar.com
- dev.foo.com
或者
# 配置列表
myconfig:
servers: dev.bar.com,dev.foo.com
单个文件中配置多个YAML文档
spring:
profiles:
active: dev
# 配置列表
myconfig:
servers:
- dev.bar.com
- dev.foo.com
---
# 开发环境配置
spring:
profiles: dev
myconfig:
servers:
- dev.bar.com
- dev.foo.com
---
# 测试环境配置
spring:
profiles: test
myconfig:
servers:
- test.bar.com
- test.foo.com
---
# 正式环境配置
spring:
profiles: prod
myconfig:
servers:
- prod.bar.com
- prod.foo.com
---
表示文档开始分隔符,也可以使用 ...
表示文档结束分隔符
合并YAML列表
spring:
profiles:
active: dev
# 配置列表
foo:
list:
- name: my name
description: my description
- name: another name
description: another description
---
# 开发环境配置
spring:
profiles: dev
foo:
list:
- name: my another name
Java Bean如下
@Component
@ConfigurationProperties("foo")
public class FooProperties {
private List<MyPojo> list = new ArrayList<MyPojo>();
public List<MyPojo> getList() {
return list;
}
public void setList(List<MyPojo> list) {
this.list = list;
}
}
最后注入的fooProperties
对象中变量 list
只有一个MyPojo元素,该元素的name
属性为my another name
,description
属性为 null
。
注解 @ConfigurationProperties
@ConfigurationProperties
不仅可以注解在类上,也可以注解在public@Bean
方法上
// 千万别忘记了类的注解 @Configuration
@Configuration
public class MyBar {
@ConfigurationProperties(prefix = "bar")
@Bean
public BarProperties getBarProperties() {
return new BarProperties();
}
}
网友评论