配置文件

配置文件

application.yaml

1
2
3
4
5
6
7
8
9
10
11
12
13
person:
lastname: VV
name: vvf${person.lastname}
sex: 1
habits:
- ball
- aaaa
secret: ${random.value}
number: ${random.int}
bigNum: ${random.long}
uuid: ${random.uuid}
less10: ${random.int(10)}
range: ${random.int[1024,65525]}

注入属性

解析配置文件到自定义类的两种方式:

  • 使用@ConfigurationProperties
  • 使用@Value

@ConfigurationProperties

实体类Person
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
31
32
33
34
35
36
37
38
39
40
41
42
@ConfigurationProperties(prefix = "person")
@Component
public class Person {
String name;
Integer sex;

List<String> habits;

public List<String> getHabits() {
return habits;
}

public void setHabits(List<String> habits) {
this.habits = habits;
}

@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", sex=" + sex +
", habits=" + habits +
'}';
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Integer getSex() {
return sex;
}

public void setSex(Integer sex) {
this.sex = sex;
}
}

使用@Value

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
31
32
@Component
public class Person2 {
@Value("${person.name}")
String name;
@Value("${person.sex}")
Integer sex;

@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", sex=" + sex +
'}';
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Integer getSex() {
return sex;
}

public void setSex(Integer sex) {
this.sex = sex;
}
}

测试类

1
2
3
4
5
6
7
8
9
10
11
12
13
@SpringBootTest
class ApplicationTests {
@Autowired
Person p;
@Autowired
Person2 p2;

@Test
void contextLoads() {
System.out.println(p);
System.out.println(p2);
}
}

对比@ConfigurationProperties 和 @Value

使用表达式

1
2
@Value("#{10+1}")
Integer age;

JSR303数据校验

pom

1
2
3
4
5
6
7
8
9
10
11
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>2.0.1.Final</version>
</dependency>
// 给validation-api提供实现
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.0.1.Final</version>
</dependency>

类添加注解@Validated

字段添加注解@Email等具体验证

注意字段要有getter、setter方法

1
2
3
4
5
6
7
8
9
10
@ConfigurationProperties(prefix = "person")
@Component
@Validated
public class Person {
String name;
Integer sex;
@Email
String email;
...
}

多环境配置

使用随机数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@Value("${person.number}")
Integer number;
//@Value("${person.}")
@Value("${person.secret}")
String secret;
@Value("${person.bigNum}")
Long bigNum;
@Value("${person.uuid}")
String uuid;

@Value("${person.less10}")
Integer less10;

@Value("${person.range}")
Integer range;

多环境配置

分文件多环境配置

resources文件夹下配置多个文件
application-dev.ymal
application-test.ymal
application-prod.ymal
application.ymal

在application.ymal中配置

1
2
3
spring:
profiles:
active: dev # 启用application-dev.ymal

单一文件多环境配置

  • 不同配置使用—分隔
  • active 标志启用
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    spring:
    profiles: dev
    server:
    port: 8091
    ---
    spring:
    profiles:
    active: test # 启用test配置
    server:
    port: 8092
    ---
    spring:
    profiles: prod
    server:
    port: 8093

配置文件位置优先级

  • file :./config/
  • file :./
  • classpath:/config/
  • classpath:/
    classpath:resource文件夹