写作时间:2018-12-22
Spring Boot: 2.1 ,JDK: 1.8, IDE: IntelliJ IDEA,
配置文件说明
Spring Boot 配置文件允许为同一套应用,为不同的环境用不同的配置文件。比如开发环境、测试环境、生成环境。你可以用properties 文件, YAML 文件, 环境变量 , 和 命令行参数 去定制配置文件. 属性可以通过注解 @Value 注入内容,结构化对象可以通过注解 @ConfigurationProperties 注入内容。配置文件英文关键词: Externalized Configuration .
Spring Boot 配置文件加载优先级如下:
- Devtools全局设置属性在路径 (~/.spring-boot-devtools.properties 当devtools激活的时候).
- 测试用例上的 @TestPropertySource .
- 测试用例上的 @SpringBootTest#properties注解.
- 命令行参数.
- 配置在SPRING_APPLICATION_JSON 的属性(环境变量或系统属性中内嵌的内联JSON).
- ServletConfig初始化参数.
- ServletContext 初始化参数.
- 配置在
java:comp/env
的 JNDI(Java Naming and Directory Interface) 属性. - Java系统属性 (System.getProperties()).
- 操作系统OS(Operating System)环境变量.
- 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).
本章记录最常用的配置文件application.properties 和 YAML。
配置文件位置读取优先级
application.properties
或者 application.yml
读取优先级如下:
file:./config/
当前位置的子目录/config
下file:./
当前位置classpath:/config/
classpath/config
的包中classpath:/
classpath的根目录
Properties文件中的占位符
Placeholders占位符,实际上是可以被后面的属性运用的变量。
通过{占位符}
来替换掉里面对象的内容。比如下面app.description=MyApp is a Spring Boot Application
app.name=MyApp
app.description=${app.name} is a Spring Boot application
用YAML代替 Properties
YAML是JSON的超集,可以分层来定义数据. SpringApplication
类自动支持 YAML 替换 properties 当classpath
加载了类库SnakeYAML.
spring-boot-starter
自动加载了类库SnakeYAML。
- YAML分层的例子:
environments:
dev:
url: http://dev.example.com
name: Developer Setup
prod:
url: http://another.example.com
name: My Cool App
等价于properties
environments.dev.url=http://dev.example.com
environments.dev.name=Developer Setup
environments.prod.url=http://another.example.com
environments.prod.name=My Cool App
- YAML配置list的例子,
my:
servers:
- dev.example.com
- another.example.com
等价于properties,数组list带了[index]
my.servers[0]=dev.example.com
my.servers[1]=another.example.com
加载YAML
Spring Framework 提供了两个方便的方法加载YAML文件. YamlPropertiesFactoryBean
加载 YAML 当做 Properties,YamlMapFactoryBean
加载 YAML 当做a Map.
自定义属性
参照教程【SpringBoot 2.1 | 第一篇:构建第一个SpringBoot工程】新建一个Spring Boot项目,名字叫democonfig, 在目录src/main/java/resources
下找到配置文件application.properties
,重命名为application.yml
。
定义属性如下:
account:
name: zgpeace
age: 18
number: ${random.int}
uuid: ${random.uuid}
max: ${random.int(10)}
value: ${random.value}
greeting: hi, I'm ${my.name}
其中配置文件中用到了${random}
,它可以用来生成各种不同类型的随机值。
读取配置文件里面的内容,在属性上用注解@Value("${属性名}")
,
新建类com.zgpeace.democonfig.MineController.java
, 实现如下:
package com.zgpeace.democonfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MineController {
@Value("${account.name}")
private String name;
@Value("${account.age}")
private int age;
@RequestMapping(value = "/author")
public String mine() {
return name+ ": " + age;
}
}
启动应用,命令行访问结果如下:
$ curl http://localhost:8080/author
zgpeace: 18
将配置文件的属性赋给实体类
配置文件的字段可以赋值给JavaBean, 结构化对象可以通过注解 @ConfigurationProperties 注入内容,创建类com.zgpeace.democonfig.bean.ConfigBean
, 实现如下:
package com.zgpeace.democonfig.bean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@ConfigurationProperties(prefix = "account")
@Component
public class ConfigBean {
private String name;
private int age;
private int number;
private String uuid;
private int max;
private String value;
private String greeting;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public String getUuid() {
return uuid;
}
public void setUuid(String uuid) {
this.uuid = uuid;
}
public int getMax() {
return max;
}
public void setMax(int max) {
this.max = max;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public String getGreeting() {
return greeting;
}
public void setGreeting(String greeting) {
this.greeting = greeting;
}
}
实现类com.zgpeace.democonfig.MineController
修改为:
package com.zgpeace.democonfig;
import com.zgpeace.democonfig.bean.ConfigBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@EnableConfigurationProperties({ConfigBean.class})
public class MineController {
@Autowired
ConfigBean configBean;
@Value("${account.name}")
private String name;
@Value("${account.age}")
private int age;
@RequestMapping(value = "/author")
public String mine() {
return name+ ": " + age;
}
@RequestMapping(value = "/lucy")
public String person() {
return configBean.getGreeting() + " >>>" + configBean.getName() + " >>>" +configBean.getUuid() + " >>>" + configBean.getMax();
}
}
启动应用,在命令行访问:
$ curl http://localhost:8080/lucy
hi, I'm ${my.name} >>>zgpeace >>>86cc4c39-8715-46ef-9e9f-497a2c6fac22 >>>8
自定义配置文件
上面介绍的文件是放在properties.yml
中,现在定义一个自己的文件
specific.properties
, 内容如下:
com.zgpeace.hobby=run marathon
com.zgpeace.fruit=orange
赋值给JavaBean,需要三个注解
@Configuration
@PropertySource(value = “classpath:specific.properties”)
@ConfigurationProperties(prefix = “com.zgpeace”)
新建类com.zgpeace.democonfig.bean.User
,
package com.zgpeace.democonfig.bean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
@Configuration
@PropertySource(value = "classpath:specific.properties")
@ConfigurationProperties(prefix = "com.zgpeace")
public class User {
private String hobby;
private String fruit;
public String getHobby() {
return hobby;
}
public void setHobby(String hobby) {
this.hobby = hobby;
}
public String getFruit() {
return fruit;
}
public void setFruit(String fruit) {
this.fruit = fruit;
}
}
运行程序,在命令行测试:
$ curl http://localhost:8080/user
hobby: run marathon ; fruit: orange
多个环境配置文件
除了application.properties
或者 application.yml
外,可以自定义环境配置文件的格式为application-{profile}.properties
。环境有默认的定制配置文件application-default.properties
,默认会加载。但是用户定制的文件会覆盖掉默认的文件。
比如开发环境、测试环境、生成环境定义三个配置文件
application-test.properties
:测试环境
server:
port: 8082
application-dev.properties
:开发环境
server:
port: 8086
application-prod.properties
:生产环境
server:
port: 8088
Spring Profiles提供了一种隔离应用程序配置的方式,并让这些配置只在特定的环境下生效。启用方式为设置application.yml 中设置profile: active :
spring:
profiles:
active: dev
启动应用,发现程序的端口不再是8080,而是8082。
代码地址
https://github.com/zgpeace/Spring-Boot2.1/tree/master/democonfig
参考
https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html
https://blog.csdn.net/forezp/article/details/70437576