spring boot 2.0特性之外部化配置
简介
PropertySource允许你覆盖其值,其覆盖顺序如下:
~/.spring-boot-devtools.properties
被设置,并且被激活)
@TestPropertySource
@SpringBootTest#properties
注解了属性
ServletConfig
初始化参数
ServletContext
初始化参数
java:comp/env
.的JNDI属性
RandomValuePropertySource
。
application-{profile}.properties
与 YAML 变量
)
application-{profile}.properties
与 YAML 变量
)
application.properties
与 YAML 变量
)
application.properties
与 YAML 变量
)
SpringApplication.setDefaultProperties指定
)
import org.springframework.stereotype.*
import org.springframework.beans.factory.annotation.*
@Component
public class MyBean {
@Value("${name}")
private String name;
// ...
}
在应用根路径上,可以提供一个
application.properties
指定默认的环境变量。当运行一个新的环境变量的时候,可以通过指定application.properties覆盖name的值。对于一次性测试,可以通过命名行发送指定的变量,例如代码如下:java -jar app.jar --name="Spring"可以在linux命名行中指定环
SPRING_APPLICATION_JSON
环境变量:代码如下: SPRING_APPLICATION_JSON='{"acme":{"name":"test"}}' java -jar myapp.jar也可以通过如下方式:
java -Dspring.application.json='{"name":"test"}' -jar myapp.jar也可以通过命名行的形式:
java -jar myapp.jar --spring.application.json='{"name":"test"}'
配置随机变量
RandomValuePropertySource
主要是用于注入随机变量。其能够生成
integers, longs, uuids 或者是 strings类型:如下代码所示my.secret=${random.value}
my.number=${random.int}
my.bignumber=${random.long}
my.uuid=${random.uuid}
my.number.less.than.ten=${random.int(10)}
my.number.in.range=${random.int[1024,65536]}其中
random.int*可以指定区间范围,既最大值与最小值的区间范围。
链接命名行属性
SpringApplication
会转化任何命名行的参数(其参数通过“--”开始)为属性并将其添加到spring的环境变量中。命名行的参数总是优先覆盖其他来源的参数。
SpringApplication.setAddCommandLineProperties(false).
应用属性文件
SpringApplication
将会从如下位置加载application.properties到spring的环境变量中:
#通过第一种方式
java -jar myproject.jar --spring.config.name=myproject
##方式二
java -jar myproject.jar --spring.config.location=classpath:/default.properties,classpath:/override.properties如果
spring.config.location
也包含具体目录,需要使用“/”.
spring.config.location
不支持具体额配置环境变量,将会被默认的配置环境变量替换。其配置搜索的顺序与配置的顺序刚好相反。例如:配置顺序为:
classpath:/,classpath:/config/,file:./,file:./config/,其搜索顺序如下:
file:./config/file:./classpath:/config/classpath:/
classpath:/custom-config/,file:./custom-config/,
其搜索的顺序如下:
file:./custom-config/classpath:custom-config/file:./config/file:./classpath:/config/classpath:/
特定环境配置属性(profile-specific)
属性中的占位符
app.name=MyApp
app.description=${app.name} is a Spring Boot application
使用YAML代替属性
SpringApplication
默认支持YAML的配置。yaml默认被
spring-boot-starter支持。
YamlMapFactoryBean
作为一个MAP加载YAML。
environments:
dev:
url: http://dev.example.com
name: Developer Setup
prod:
url: http://another.example.com
name: My Cool App其将会被转换为如下的属性:
environments.dev.url=http://dev.example.com
environments.dev.name=Developer Setup
environments.prod.url=http://another.example.com
environments.prod.name=My Cool Appyaml的列表可以通过{index}的方式应用:如下列子:
my:
servers:
- dev.example.com
- another.example.com被转换为属性之后的结果为:
my.servers[0]=dev.example.com
my.servers[1]=another.example.com通过使用
DataBinder
工具绑定变量的时候,在被绑定的类上必须要有一个List的类或者是set,提供一个setter方法,如下代码:@ConfigurationProperties(prefix="my")
public class Config {
private List<String> servers = new ArrayList<String>();
public List<String> getServers() {
return this.servers;
}
}
YamlPropertySourceLoader
去暴露。
server:
address: 192.168.1.100
---
spring:
profiles: development
server:
address: 127.0.0.1
---
spring:
profiles: production
server:
address: 192.168.1.120上述环境中,如果开发环境被触发:则sever地址为:
127.0.0.1,如果为生产地址:则地址为
192.168.1.120
.,当其两个都不可用的时候,默认为
192.168.1.100。
PropertySource
进行加载
@ConfigurationProperties("acme")
public class AcmeProperties {
private final List<MyPojo> list = new ArrayList<>();
public List<MyPojo> getList() {
return this.list;
}
}
-----------------------------------------------
acme:
list:
- name: my name
description: my description
---
spring:
profiles: dev
acme:
list:
- name: my another name如果
dev
没有被激活,则只包含一个元素
MyPojo
,如果
dev
被激活,其仍然只包含一个元素,其不会添加第二个元素进去,其没有合并其项。当其要进行合并的时候,必须要指定一个高亮的:如下代码所示:acme:
list:
- name: my name
description: my description
- name: another name
description: another description
---
spring:
profiles: dev
acme:
list:
- name: my another name上述代码中的第一个list所示
acme:
list:
- name: my name
description: my description
- name: another name
description: another description
---
spring:
profiles: dev
acme:
list:
- name: my another name
类型安全的配置属性
@Value("${property}")
注入配置属性有时候是使笨重的,特别是有多个属性或者是数据本质上有层级关系的。
Spring Boot提供了一种可选的方法:让安全的bean管理和验证器应用程序:如下例子:
通过使用
@EnableConfigurationProperties
注册bean:
@Configuration
@EnableConfigurationProperties(AcmeProperties.class)
public class MyConfiguration {
}上述列子将会被注册为
@Component
@ConfigurationProperties(prefix="acme")
public class AcmeProperties {
// ... see the preceding example
}
这种对于外部配置的使一种很好的方式:代码如下:# application.yml
acme:
remote-address: 192.168.1.1
security:
username: admin
roles:
- USER
- ADMIN
# additional configuration as required
我们简单的使用即可:如下代码所示:@Service
public class MyService {
private final AcmeProperties properties;
@Autowired
public MyService(AcmeProperties properties) {
this.properties = properties;
}
//...
@PostConstruct
public void openConnection() {
Server server = new Server(this.properties.getRemoteAddress());
// ...
}
}
01. 第三方配置
@Bean
方法,这种方式适用于外部控制的一个组件的属性构造。如下代码@ConfigurationProperties(prefix = "another")
@Bean
public AnotherComponent anotherComponent() {
...
}02. 释放绑定
@ConfigurationProperties(prefix="acme.my-project.person")
public class OwnerProperties {
private String firstName;
public String getFirstName() {
return this.firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
}
@Validated
注解的属性,spring boot总要去验证
@ConfigurationProperties
的合法性。可以通过
JSR-303
javax.validation
去验证器合法性 ,如下代码:
@ConfigurationProperties(prefix="acme")
@Validated
public class AcmeProperties {
@NotNull
private InetAddress remoteAddress;
// ... getters and setters
}为了能触发验证器,必须要关联其赌赢的@validate,如下代码:
@ConfigurationProperties(prefix="acme")
@Validated
public class AcmeProperties {
@NotNull
private InetAddress remoteAddress;
@Valid
private final Security security = new Security();
// ... getters and setters
public static class Security {
@NotEmpty
public String username;
// ... getters and setters
}
}可以通过自定义的验证器去验证。
@ConfigurationProperties
与
@Value
的支持:
| 特性 | @ConfigurationProperties |
@Value |
| 松绑定 | yes | no |
| 元数据支持 | yes | No |
| SpEL evaluation |
no | yes |