SpringBoot的配置文件及获取配置信息

application.properties基本使用

application.properties的使用,主要用来配置数据库连接、日志相关配置等。

应用配置文件位置

  • spring会从classpath下的/config目录或者classpath的根目录查找application.propertiesapplication.yml
  • /config优先于classpath根目录
  • @PropertySource这个注解可以指定具体的属性配置文件,优先级比较低。
  • 相同优先级位置同时有application.propertiesapplication.yml,那么application.yml里面的属性就会覆盖application.properties里的属性。

应用配置文件(.properties或.yml)

在配置文件中直接写:

1
2
name=MaxZhao
server.port=8080

.yml格式的配置文件如:

1
2
3
4
5
name: MaxZhao
server:
port: 8080
servlet:
context-path: /gtboot

当有前缀的情况下,使用.yml格式的配置文件更简单。详情请查看*
这里*

注意:使用.yml时,属性名的值和冒号中间必须有空格,如name: MaxZhao正确,name:MaxZhao就是错的。


application.properties其它用法

自定义属性加载

通常我们需要配置文件来定义一些自己的属性,比如:

1
2
my.name=MaxZhao
my.sex=1

在代码中我们可以这样引用:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

@Component
public class MyInfoProperties {
@Value("${my.name}")
private String name;
@Value("${my.title}")
private String sex;

// 省略getter和setter
}

// 或者
@ConfigurationProperties(prefix = "my")
public class MyInfoProperties {
private String name;
private String sex;

// 省略getter和setter
}

然后其它类中引用

1
2
3
4
public class MyInfoPropertiesDemo {
@Autowired
private MyInfoProperties myInfoProperties;
}

List、Map 配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14

@ConfigurationProperties(prefix = "my")
public class MyInfoProperties {
private String name;
private String sex;
private MyInfoProperties info;
private List<MyInfoProperties> infos;
private List<String> names;
private List<String> names2;
private Map<String, MyInfoProperties> infoMap;
private Map<String, String> nameMap;
private Map<String, String> nameMap2;
private Map<String, String> nameMap3;
}

下面展示一下 yml 配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
my:
name: MaxZhao
sex: 1
info:
name: 用户1
sex:
info2: { name: 用户1, sex: }
infos:
- name: 用户1
sex:
- name: 用户2
sex:
names:
- 用户1
- 用户2
names2: 用户1,用户2
infoMap:
用户1:
name: 用户1
ex:
用户2:
name: 用户2
ex:
nameMap:
用户1:
用户2:
nameMap2[用户1]:
nameMap2[用户2]:
nameMap3: { 用户1: ,用户2: }

通过命令行设置属性值

java -jar xxx.jar --server.port=8888,这三通过使用—server.port属性来设置xxx.jar应用的端口为8888。
jjava -jar xxx.jar --server.port=8888j命令,等价于我们在application.properties中添加属性server.port=8888

可以用SpringApplication.setAddCommandLineProperties(false)禁用命令行设置属性值。

通过代码设置属性值

1
2
3
4
5
6
7
8
9
10
public class MyInfoPropertiesDemo {
public static void main(String[] args) {
SpringApplication application = new SpringApplication(Application.class);
Map<String, Object> defaultMap = new HashMap<String, Object>();
defaultMap.put("name", "MaxZhao");
//还可以是Properties对象
application.setDefaultProperties(defaultMap);
application.run(args);
}
}

多环境配置

通常同一套程序会被应用和安装到几个不同的环境,比如:开发、测试、生产等。其中每个环境的数据库地址、服务器端口等等配置都会不同。 在Spring
Boot中多环境配置文件名需要满足application-{profile}.properties的格式,其中{profile}对应你的环境标识,比如:

  • application-dev.properties:开发环境
  • application-test.properties:测试环境
  • application-prod.properties:生产环境

至于哪个具体的配置文件会被加载,需要在 application.properties 文件中通过 spring.profiles.active属性来设置,其值对应 {profile}值。

如:spring.profiles.active=test就会加载application-test.properties配置文件内容
可以用命令行执行java -jar xxx.jar --spring.profiles.active=test

按照上面的实验,可以如下总结多环境的配置思路:

application.properties中配置通用内容,并设置spring.profiles.active=dev,以开发环境为默认配置。
application-{profile}.properties中配置各个环境不同的内容。 通过命令行方式去激活不同环境的配置。

多环境配置的另一种方式pom.xml

application.yml中配置:

1
2
3
4
spring:
profiles:
# 获取 pom文件的内容,需要加引号,单双引号都可以
active: '@spring.active@'

pom.xml中配置:

1
2
3
4
5
6
7
8
9

<profiles>
<profile>
<id>dev</id>
<properties>
<spring.active>dev</spring.active>
</properties>
</profile>
</profiles>

最后在mavenProfiles中选择dev,运行就可以了,这样就能很好的区分开发环境和上线环境。

随机数的使用

application.properties中:

1
2
3
4
5
6
7
8
9
10
# 随机字符串
random.value=${random.value}
# 随机int
random.number=${random.int}
# 随机long
random.bignumber=${random.long}
# 10以内的随机数
random.test1=${random.int(10)}
# 10-20的随机数
random.test2=${random.int[10,20]}

参数间的引用

application.properties中:

1
2
3
my.name=MaxZhao
my.sex=1
my.des=My name is ${MaxZhao}.

通过属性占位符还能缩短命令参数

例如修改web默认端口需要使用--server.port=9090方式,如果在配置中写上:

1
server.port=${port:8080}

那么就可以使用更短的--port=9090,当不提供该参数的时候使用默认值8080

Java系统属性

注意Java系统属性位置java -Dname="MaxZhao" -jar xxx.jar,可以配置的属性都是一样的,优先级不同。

例如java -Dname="MaxZhao" -jar xxx.jar --name="Spring!"name值为Spring!

属性名匹配规则 例如有如下配置对象:

1
2
3
4
5
6

@Component
@ConfigurationProperties(prefix = "person")
public class ConnectionSettings {
private String firstName;
}

firstName可以使用的属性名如下:

  • person.firstName,标准的驼峰式命名
  • person.first-name,虚线(-)分割方式,推荐在.properties和.yml配置文件中使用
  • PERSON_FIRST_NAME,大写下划线形式,建议在系统环境变量中使用

devtools 配置

1
2
3
4
5
spring.devtools.restart.enabled=true
# 设置重启目录
spring.devtools.restart.additional-paths=src/main/java
# 排除不需要重启的目录,默认值在spring-boot-devtools/2.1.2.RELEASE/spring-boot-devtools-2.1.2.RELEASE-sources.jar!/org/springframework/boot/devtools/autoconfigure/DevToolsProperties.java
#spring.devtools.restart.exclude=

MySQL8 配置

1
2
3
4
5
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/maxzhao_ittest?charset=utf8mb4&useSSL=false
spring.datasource.username=maxzhao
spring.datasource.password=maxzhao
#com.mysql.cj.PerConnectionLRUFactory

Redis

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# Redis 数据库索引
spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.password=maxzhao
# 连接池最大连接数
spring.redis.jedis.pool.max-active=1000
# 连接池最大阻塞等待时间,负值没有限制
spring.redis.jedis.pool.max-wait=-1
# 连接池中最大空闲连接
spring.redis.jedis.pool.max-idle=10
# 连接池中的最小空闲连接
spring.redis.jedis.pool.min-idle=2
# 超时时间 毫秒
spring.redis.timeout=1000

上传文件大小限制配置

1
2
3
4
5
6
7
spring:
application:
name: maxzhao
servlet:
multipart:
max-file-size: 100MB # MB一定要大写
max-request-size: 100MB

jackson时区配置

1
2
3
4
5
6
7
8
# 方法1
spring:
application:
name: maxzhao
jackson:
date-format: yyyy-MM-dd
time-zone: GMT+8
# 方法2 在pojo上添加JonFormat

日志

1
2
3
4
5
6
7
8
9
10
11
logging:
config: classpath:log-boot.xml
level:
# 全部路径 TARCE < DEBUG < INFO < WARN < ERROR < FATAL
root: info
# 指定包
com:
jackie:
springbootdemo:
config: debug
# file:

RabbitMQ

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
spring:
rabbitmq:
host: 127.0.0.1
port: 5672
username: guest
password: guest
#支持发布确认
publisher-confirms: true
#支持发布返回
publisher-returns: true
# 默认 /
virtual-host: vhost
listener:
simple:
#采用手动应答
acknowledge-mode: manual
#指定最小的消费者数量
concurrency: 1
#指定最大的消费者数量
max-concurrency: 1
retry:
#是否支持重试
enabled: true

本文地址:SpringBoot的配置文件及获取配置信息

本文地址: https://github.com/maxzhao-it/blog/post/56669/