12.@Value读取配置文件及验证处理

 

@Value读取配置文件及验证处理

1. @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
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import lombok.Data;

@Component
@Data
public class Person {

@Value("${person.id}")
private Integer id;
@Value("${person.user-name}")
private String userName;
@Value("${random.int(1,100)}")
private Integer age;
// @Value("${person.hobby}")
private String[] hobby;
private List<String> course;
private Set<String> book;
private Map<String, String> score;
// @Value("${person.birthday.time}")
private Date birthday;

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package com.example.demo;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import com.example.demo.vo.Person;

@SpringBootTest
class SpringBoot007ApplicationTests {

@Autowired
private Person person;

@Test
public void testPerson() {
System.out.println(person);
}

}

output:

1
Person(id=2, userName=xiaozhang, age=77, hobby=[reading, exercise, writing], course=[literature, mathematics, geography, chemistry, biology], book=[book1, book2, book3, 2], score={math=90, chemistry=85}, birthday=Sun Jan 17 21:43:11 CST 2021)

2. 总结说明:

1,@Value只能注入简单类型的属性(基本数据类型和String),其它的复杂类型是不能取到值的

2,如果属性是使用驼峰命名法则不能使用属性名注入,要使用@Value("${student.user-name}")来取值,不能使用@Value("${student.userName}")来取值

3. @Value和@ConfigurationProperties取值比较

项目 @ConfigurationProperties @Value
功能 批量注入配置文件中的属性 一个一个指定
松散绑定(松散语法;驼峰命名法) 支持 不支持
SpEL(Spring的EL表达式) 不支持 支持
JSR303数据校验 支持 不支持
复杂类型封装 支持 不支持

@Value主动获取值,@ConfigurationProperties被动接收注入

SpEL: 例如#{2*10}

4. 验证处理

@Validated注解

@Email注解

pom.xml文件中加入依赖:

1
2
3
4
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>

加入javax.validation依赖运行会报错

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
package com.example.demo.vo;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.validation.constraints.Email;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;

import lombok.Data;

@Component
@ConfigurationProperties(prefix = "person")
@Data
@Validated
public class Person {

private Integer id;
private String userName;
private Integer age;
private String[] hobby;
private List<String> course;
private Set<String> book;
private Map<String, String> score;
private Date birthday;

@Email
private String email;

}
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
server:
port: 8890

#config
person:
id: 2
user-name: xiaozhang
age: ${random.int(1,100)}
hobby:
- reading
- exercise
- writing
course:
- literature
- mathematics
- geography
- chemistry
- ${course}
book:
- book1
- book2
- book3
- ${person.id}
score:
math: 90
chemistry: 85
birthday:
time: 1610890991736
email: Jobs123@qq.com

course: biology

output:

1
Person(id=2, userName=xiaozhang, age=3, hobby=[reading, exercise, writing], course=[literature, mathematics, geography, chemistry, biology], book=[book1, book2, book3, 2], score={math=90, chemistry=85}, birthday=Sun Jan 17 21:43:11 CST 2021, email=Jobs123@qq.com)

如果使用给email注入的不是一个正确的邮箱会报错

1
2
3
4
Caused by: org.springframework.boot.context.properties.bind.validation.BindValidationException: Binding validation errors on person
- Field error in object 'person' on field 'email': rejected value [Jobs123qq.com]; codes [Email.person.email,Email.email,Email.java.lang.String,Email]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [person.email,email]; arguments []; default message [email],[Ljavax.validation.constraints.Pattern$Flag;@23ee75c5,.*]; default message [不是一个合法的电子邮件地址]; origin class path resource [application.yml] - 29:10
at org.springframework.boot.context.properties.bind.validation.ValidationBindHandler.validateAndPush(ValidationBindHandler.java:139) ~[spring-boot-2.4.2.jar:2.4.2]
...