@ConfigurationProperties
与@Value
二者的区别:
@ConfigurationProperties | @Value | |
---|---|---|
功能 | 批量注入配置文件的属性 | 一个一个的指定 |
松散绑定 | 支持 | 不支持 |
SPEL | 不支持 | 支持 |
JSR303数据校验 | 支持 | 不支持 |
复杂类型封装 | 支持 | 不支持 |
1、@ConfigurationProperties的使用
首先导入依赖:
<!--导入配置文件处理器,配置文件进行绑定就会有提示-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
定义一个School类:
School.java
@Component
@ConfigurationProperties(prefix = "school")
public class School {
private String lastName;
private String address;
private Date birth;
private Map<String,Object> maps;
private List<Object> lists;
private Student student;
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
public Map<String, Object> getMaps() {
return maps;
}
public void setMaps(Map<String, Object> maps) {
this.maps = maps;
}
public List<Object> getLists() {
return lists;
}
public void setLists(List<Object> lists) {
this.lists = lists;
}
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
@Override
public String toString() {
return "School{" +
"lastName='" + lastName + '\'' +
", address='" + address + '\'' +
", birth=" + birth +
", maps=" + maps +
", lists=" + lists +
", student=" + student +
'}';
}
Student.java
public class Student {
private String name;
private Integer age;
private String score;
public Student() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getScore() {
return score;
}
public void setScore(String score) {
this.score = score;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", score='" + score + '\'' +
'}';
}
配置文件:
application.yml
school:
lastName: xx学校
address: 江苏
birth: 2020/01/01
maps: {
k1:v1,k2:v2}
lists:
- list1
- list2
student:
name: 张三
age: 17
测试类:
@SpringBootTest
class Springboot02ApplicationTests {
@Autowired
School school;
@Test
public void contextLoads() {
System.out.println(school);
}
}
运行结果:
2、@Value的使用
@Value 也能从配置文件中获取属性的值,需要一个属性一个属性的进行绑定。
School.java
@Component
//@ConfigurationProperties(prefix = "school")
public class School {
@Value("XXX高级中学")
private String lastName;
@Value("${school.address}")
private String address;
private Date birth;
private Map<String,Object> maps;
private List<Object> lists;
private Student student;
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
public Map<String, Object> getMaps() {
return maps;
}
public void setMaps(Map<String, Object> maps) {
this.maps = maps;
}
public List<Object> getLists() {
return lists;
}
public void setLists(List<Object> lists) {
this.lists = lists;
}
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
@Override
public String toString() {
return "School{" +
"lastName='" + lastName + '\'' +
", address='" + address + '\'' +
", birth=" + birth +
", maps=" + maps +
", lists=" + lists +
", student=" + student +
'}';
}
在JavaBean里写的是lastName(驼峰写法),配置文件里可以写成lastName,也可以写成last-name。
测试结果:
注意:@ConfigurationProperties
是支持这种命名的;而@Value
是不支持!
SpEL即Spring表达式语言(Spring Expression Language)。SpEL表达式的默认格式为: #{expression}
。SpEL表达式以“#”
开头,表达式主体包围在花括号中。
@Component
public class Person {
private String name;
@Value("#{2*6}")
private Integer age;
测试结果:
@ConfigurationProperties
不支持 SPEL不再演示。
注:使用之前需要添加jar包:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
示例代码:
@Component
@Validated
@ConfigurationProperties(prefix = "school")
public class School {
// @Value("${school.last_name}")
@Length(min=5, max=20, message="用户名长度必须在5-20之间")
private String lastName;
application.yml
school:
last_name: 学校
运行效果:
注:@ConfigurationProperties
支持 JSR303
数据校验。
@Value("${school.last_name}")
@Length(min=5, max=20, message="用户名长度必须在5-20之间")
private String lastName;
@Value("${school.student}")
private Student student;
这样程序运行是直接报错的。@Value
不支持复杂类型封装。在上面的例子中已经证明@ConfigurationProperties
是支持复杂类型封装。
若文章中有错误的地方欢迎大家反馈或者留言,十分感谢!!!