Spring Boot 配置文件详解
一、Spring Boot 配置文件
1.1 配置文件类型
1.1.1 配置文件介绍
在使用IntelliJ IDEA创建Spring Boot项目时,IDE会在src/main/java/resources
目录下创建一个application.properties
文件。
该文件是 SpringBoot 的配置文件,所有的配置信息都在该文件中配置,包括对服务器的配置、spring 的配置、springmvc 的配置、mybatis 的配置等等。
这个配置文件也可以换成为applicaiton.yml
文件,application.yml
配置文件拥有更为简洁的配置语法,但是对书写的格式要求更加严格,不过无论是用那种配置文件最终效果都是一致的。
1.1.2 配置文件语法
(1)application.properties
文件语法
示例
server.port=8080
server.error.include-stacktrace=always
server.error.include-exception=true
server.compression.enabled=true
spring.resources.static-locations=classpath:/static/, classpath:/templates/
spring.resources.chain.cache=true
spring.resources.chain.compressed=true
spring.aop.auto=true
显示效果
(2)application.yml
文件语法
示例
server:
port: 8080
error:
include-stacktrace: always
include-exception: true
compression:
enabled: true
spring:
resources:
static-locations: classpath:/static/, classpath:/templates/
chain:
cache: true
compressed: true
aop:
auto: true
显示效果
2.3.4 YAML
YAML(YAML Ain’t Markup Language),以数据为中心,比json, xm|等更适合做配置文件。
示例
server:
port: 8080
spring:
resources:
static-locations: classpath:/static/
说明
(1)k(空格)v
:表示一堆键值对,中间以空格隔开,以空格的缩进来控制层级关系,只要左对齐的一列数据都是属于同一个层级的。
(2)数字、字符串、布尔值的写法:k(空格)v
,字符串默认不用加上单引号或者双引号
- 双引号不会转义字符串里面的符号,而是会以其本身的意思来表达
- 单引号会将引号内的符号进行转译。
(3)对象、Map(属性和值)的写法:k(空格)v
,对象仍旧是以键值对的方式属性
student:
name: zhangsan
age: 18
或者
student: {name: zhangsan,age: 18}
(3)数组(List、Set)
用-
来表示数组中的元素
person:
- girl
- boy
- others
或者
person: [girl,boy,others]
注意:yml 文件书写格式要求非常严格,具体要求如下:
- 绝对不能使用tab键来创建空格,如果下一行是一个子级别的属性名按回车后输入一个空格然后继续填写属性值。
- 文件的最后不能有空行(无内容的行),有的话通过delete键删除
- 每个
:
后边都要空一格(注意一定要用空格键来操作,不能用Tab键来空格) - 每个级别的属性名要严格对齐
- 配置文件中间也不能有空行(无内容的行)
1.2 自定义属性
1.2.1 自定义属性示例
配置文件除了有系统的属性外,还可以自定义属性,以application.properties
配置文件为例进行示例。
自定义示例
student.name=张三
student.age=18
student.sex=Man
student.email=11234567@163.com
显示结果
1.2.2 读取自定义属性
在类中,如果需要读取配置文件的内容,那么只需要在属性上使用@Value("${属性名}")
,新建一个TestController,在其中创建一个test方法进行测试。
TestController的内容如下。
@RestController
public class TestController {
@Value("${student.name}")
private String name;
@Value("${student.age}")
private String age;
@Value("${student.name}")
private String sex;
@Value("${student.name}")
private String email;
@GetMapping("test")
public String test(){
return "student.name = " + name +
" student.age = " + age +
" student.sex = " + sex +
" student.email = " + email;
}
}
网页访问http://localhost:8080/test
,得到如下结果
在application.properties
中配置中文值,读取时会出现中文乱码问题。因为Java默认会使用ISO-8859-1的编码方式来读取*.properties
配置文件,而SpringBoot应用则以UTF-8的编码方式来读取,就导致产生了乱码问题。
对于这个问题,官方推荐的做法是:“Characters that cannot be directly represented in this encoding can be written using Unicode escapes”,大致意思就是使用Unicode的方式来展示字符。例如上述代码中的student.name=张三
应该配置成student.name=\u5f20\u4e09
。
再次访问得到如下结果
1.3 使用随机数
1.3.1 使用随机数示例
在配置文件中,还提供了随机数供我们使用,即在配置文件中使用${random}
来生成不同类型的随机数,大致分为随机数、随机uuid、随机字符串等。
示例
Student.name=\u5f20\u4e09
student.age=18
student.sex=Man
student.email=11234567@163.com
# 随机uuid
test.uuid=${random.uuid}
# 随机int值
test.intNum=${random.int}
# 100以内的随机int值
test.randomNum=${random.int(100)}
# 随机long值
test.longNum=${random.long}
# 随机字符串
test.value=${random.value}
# 自定义属性间引用
test.name=${student.name}
test.age=${student.age}
test.sex=${student.sex}
test.email=${student.email}
显示结果
1.3.2 获取随机属性
在配置属性后,可以使用JavaBean模式来给属性赋值,创建一个ConfigBean实体类。由于自定义属性的前缀都是由test开头的,因此我们可以在实体类上加入注解@ConfigurationProperties(prefix = "test")
,同时需要在启动类上加入注解@EnableConfigurationProperties(ConfigBean.class)
,表明启动这个配置类。
ConfigBean实体类
@ConfigurationProperties(prefix = "test")
public class ConfigBean {
private String uuid;
private int intNum;
private int randomNum;
private long longNum;
private String value;
private String name;
private int age;
public String getUuid() {
return uuid;
}
public void setUuid(String uuid) {
this.uuid = uuid;
}
public int getIntNum() {
return intNum;
}
public void setIntNum(int intNum) {
this.intNum = intNum;
}
public int getRandomNum() {
return randomNum;
}
public void setRandomNum(int randomNum) {
this.randomNum = randomNum;
}
public long getLongNum() {
return longNum;
}
public void setLongNum(long longNum) {
this.longNum = longNum;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
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;
}
}
@ConfigurationProperties
注解对应的依赖如下
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
在TestController中利用@Autowired
注解注入ConfigBean类,并且创建一个test方法进行测试。
@RestController
public class TestController {
@Autowired
private ConfigBean configBean;
@GetMapping("test")
public ConfigBean test(){
return configBean;
}
}
在浏览器上访问http://localhost:8080/test进行测试,显示结果如下:
1.4 多环境配置
1.4.1 多环境配置介绍
在开发Spring Boot项目的时候,可能有这样的情况,一套程序需要在不同的环境中发布,数据库配置、端口配置或者其他配置各不相同,如果每次都需要修改为对应环境配置,不仅耗费人力,而且特别容易出现错误,造成不必要的麻烦。
通常情况下,我们可以配置多个配置文件,在不同的情况下进行替换。而在Spring Boot项目中,我们新建几个配置文件,文件名以application-{name}.properties
的格式,其中的{name}
对应环境标识,比如:
- application-dev.properties:开发环境。
- application-test.properties:测试环境。
- application-prod.properties:生产环境。
-
- application-local.properties:本地环境。
然后,可以在主配置文件(application.properties
)中配置spring.profiles.active
来设置当前要使用的配置文件,比如:
spring.profiles.active=dev
1.4.2 多环境配置示例
创建application-dev.properties
配置文件,在文件中配置端口号为8081。
创建application-test.properties
配置文件,在文件中配置端口号为8082。
创建application-prod.properties
配置文件,在文件中配置端口号为8083。
创建application-local.properties
配置文件,在文件中配置端口号为8084。
在application.properties
配置文件中设置当前要使用的配置文件
启动项目,可以在控制台看到当前启动的端口
1.5 自定义配置文件
1.5 自定义配置文件示例
前面介绍了多环境配置文件,我们也可以使用自定义配置文件,比如新建一个test.properties
,配置文件内容下。
1.5.2 获取自定义配置文件的内容
新建一个javabean来读取配置文件。新建一个ConfigBean,在类上加上注解@PropertySource(value = "classpath:test.properties")
,并且和之前一样需要加入@ConfigurationProperties(prefix = "test")
@RestController
public class TestController {
@Autowired
private ConfigBean configBean;
@GetMapping("test")
public ConfigBean test(){
return configBean;
}
}
在TestController中注入bean并且创建测试方法test
@Component
@ConfigurationProperties(prefix = "test")
@PropertySource(value = "classpath:test.properties")
public class ConfigBean {
private String uuid;
private int intNum;
private int randomNum;
private long longNum;
private String value;
private String name;
private int age;
private String sex;
private String email;
public String getUuid() {
return uuid;
}
public void setUuid(String uuid) {
this.uuid = uuid;
}
public int getIntNum() {
return intNum;
}
public void setIntNum(int intNum) {
this.intNum = intNum;
}
public int getRandomNum() {
return randomNum;
}
public void setRandomNum(int randomNum) {
this.randomNum = randomNum;
}
public long getLongNum() {
return longNum;
}
public void setLongNum(long longNum) {
this.longNum = longNum;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
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 String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
使用浏览器访问http://localhost:8080/test
,可以看到显示如下内容: