配置文件
Spring Boot 配置文件
spring.jpa.databaseplatform=mysql
spring.jpa.database-platform=mysql
spring.jpa.databasePlatform=mysql
spring.JPA.database_platform=mysql
spring:
jpa:
databaseplatform: mysql
database-platform: mysql
databasePlatform: mysql
database_platform: mysql
推荐使用全小写配合 -
分隔符的方式来配置,比如:spring.jpa.database-platform=mysql
。
YAML
environments:
dev:
url: http://dev.bar.com
name: Developer Setup
prod:
url: http://foo.bar.com
name: My Cool App"
# 表示数组
spring:
my-example:
url:
- http://example.com
- http://spring.io
spring:
my-example:
url: http://example.com, http://spring.io
yaml:
str: 字符串可以不加引号
specialStr: "双引号直接输出\n特殊字符"
specialStr2: '单引号可以转义\n特殊字符'
flag: false
num: 666
Dnum: 88.88
list:
- one
- two
- two
set: [1, 2, 2, 3]
map: { k1: v1, k2: v2 }
positions:
- name: wx
salary: 15000.00
- name: wxBlog
salary: 18888.88
创建实体类
/**
* YAML 语法实体类
* 切记点:
* 一、冒号后面加空格,即 key:(空格)value
* 二、每行参数左边空格数量决定了该参数的层级,不可乱输入。
*/
@Component
@ConfigurationProperties(prefix = "yaml")
public class YamlEntity {
// 字面值,字符串,布尔,数值
private String str; // 普通字符串
private String specialStr; // 转义特殊字符串
private String specialStr2; // 输出特殊字符串
private Boolean flag; // 布尔类型
private Integer num; // 整数
private Double dNum; // 小数
// 数组,List 和 Set,两种写法:第一种:-空格value,每个值占一行,需缩进对齐;第二种:[1,2,...n] 行内写法
private List<Object> list; // list可重复集合
private Set<Object> set; // set不可重复集合
// Map 和实体类,两种写法:第一种:key 空格 value,每个值占一行,需缩进对齐;第二种:{key: value,....} 行内写法
private Map<String, Object> map; // Map K-V
private List<Position> positions; // 复合结构,集合对象
// 省略getter,setter,toString方法
}
通过spring.profiles
属性来定义多个不同的环境配置。例如下面的内容,在指定为server.port
将使用server.port
将使用server.port
将使用
server:
port: 8881
---
spring:
profiles: test
server:
port: 8882
---
spring:
profiles: prod
server:
port: 8883"
命名约定
在
properties 格式:
spring.jpa.databaseplatform=mysql
spring.jpa.database-platform=mysql
spring.jpa.databasePlatform=mysql
spring.JPA.database_platform=mysql
yaml 格式:
spring:
jpa:
databaseplatform: mysql
database-platform: mysql
databasePlatform: mysql
database_platform: mysql
推荐使用全小写配合[]
来定位列表类型,比如:
spring.my-example.url[0]=http://example.com
spring.my-example.url[1]=http://spring.io
也支持使用逗号分割的配置方式,上面与下面的配置是等价的:
spring.my-example.url=http://example.com,http://spring.io
而在
spring:
my-example:
url:
- http://example.com
- http://spring.io
也支持逗号分割的方式:
spring:
my-example:
url: http://example.com, http://spring.io
注意:在
foo[0]=a
foo[2]=b
在
properties 格式:
spring.my-example.foo=bar
spring.my-example.hello=world
yaml 格式:
spring:
my-example:
foo: bar
hello: world
注意:如果-
的字符,需要用[]
括起来,比如:
spring:
my-example:
'[foo.baz]': bar
嵌入属性
public class Credentials {
private String authMethod;
private String username;
private String password;
// standard getters and setters
}
public class ConfigProperties {
private String host;
private int port;
private String from;
private List<String> defaultRecipients;
private Map<String, String> additionalHeaders;
private Credentials credentials;
// standard getters and setters
}
其对应的属性文件如下:
#Simple properties
mail.hostname=mailer@mail.com
mail.port=9000
mail.from=mailer@mail.com
#List properties
mail.defaultRecipients[0]=admin@mail.com
mail.defaultRecipients[1]=owner@mail.com
#Map Properties
mail.additionalHeaders.redelivery=true
mail.additionalHeaders.secure=true
#Object properties
mail.credentials.username=john
mail.credentials.password=password
mail.credentials.authMethod=SHA1
参数引用
在
book.name=xxx
book.author=xxx
book.desc=${book.author} is writing《${book.name}》
- 在配置文件中配置环境变量
spring.redis.host=${REDIS_HOST:127.0.0.1}
spring.redis.port=6379
spring.redis.timeout=30000
以上表示
- 在启动
Docker 容器时传入环境参数
$ docker run -d --name test2 {镜像名} -e REDIS_HOST=192.168.0.1
使用随机数
在一些特殊情况下,有些参数我们希望它每次加载的时候不是一个固定的值,比如:密钥、服务端口等。在${random}
配置来产生随机的${random}
的配置方式主要有一下几种,读者可作为参考使用。
# 随机字符串
com.didispace.blog.value=${random.value}
# 随机int
com.didispace.blog.number=${random.int}
# 随机long
com.didispace.blog.bignumber=${random.long}
# 10以内的随机数
com.didispace.blog.test1=${random.int(10)}
# 10-20的随机数
com.didispace.blog.test2=${random.int[10,20]}
Properties
userinfo.account=wx
userinfo.age=25
userinfo.active=true
userinfo.created-date=2018/03/31 16:54:30
userinfo.map.k1=v1
userinfo.map.k2=v2
userinfo.list=one,two,three
userinfo.position.name=Test
userinfo.position.salary=19999.99
在
spring.my-example.url[0]=http://example.com
spring.my-example.url[1]=http://spring.io"
也支持使用逗号分割的配置方式,上面与下面的配置是等价的:
spring.my-example.url=http://example.com,http://spring.io
在