引言
之前我们说过,在配置文件中能够自定义配置很多数据,在我的工作中经常遇到这种场景,在配置文件中定义数据从而在程序中获取应用,从而避免了频繁修改代码。那么在Spring Boot项目中是如何获取配置文件中自定义的数据呢?带着这个问题,听我讲解。
获取配置文件中值的几种方式
获取配置文件中的值有以下几种方式:
- @ConfigurationProperties
- @Value
- @PropertySource
@ConfigurationProperties
Spring Boot项目中可以利用该注解,一次性注入整个对象的值,例如:
student:
studentName: 张三
age: 18
gender: 男
className: 高一一班
active: true
birthDay: 2001/01/23
favorite:
- swimming
- running
entity
package com.frank.entity;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.List;
@ConfigurationProperties(prefix = "student") // 将配置文件中的student对象值注入该类
@Component// 将该类加入到Spring容器中
public class Student {
private String studentName;
private Integer age;
private String gender;
private String className;
private Boolean active;
private Date birthDay;
private List<String> favorite;
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
public Boolean getActive() {
return active;
}
public void setActive(Boolean active) {
this.active = active;
}
public Date getBirthDay() {
return birthDay;
}
public void setBirthDay(Date birthDay) {
this.birthDay = birthDay;
}
public List<String> getFavorite() {
return favorite;
}
public void setFavorite(List<String> favorite) {
this.favorite = favorite;
}
@Override
public String toString() {
return "Student{" +
"studentName='" + studentName + '\'' +
", age=" + age +
", gender='" + gender + '\'' +
", className='" + className + '\'' +
", active=" + active +
", birthDay=" + birthDay +
", favorite=" + favorite +
'}';
}
}
注意:
- @ConfigurationProperties只针对于Spring容器中的组件才会生效。
- @ConfigurationProperties要求属性必须有setter方法。
@Value
//@ConfigurationProperties(prefix = "student") // 将配置文件中的student对象值注入该类
@Component// 将该类加入到Spring容器中
public class Student {
@Value("${student.studentName}")
private String studentName;
@Value("${student.age}")
private Integer age;
@Value("${student.gender}")
private String gender;
@Value("${student.className}")
private String className;
@Value("${student.active}")
private Boolean active;
@Value("${student.birthDay}")
private Date birthDay;
// @Value("#{'${student.favorite}'.split(',')}")
private List<String> favorite;
注意:@Value的参数必须为String 类型,因此对于特殊类型注意,否则会出现类型转换失败的错误。
@PropertySource
该注解是引入指定配置文件,配合@ConfigurationProperties 或@Value注解将该配置文件中的值注入到实体类中。
新建一个yml
student:
studentName: 张三
age: 18
gender: 男
className: 高一一班
active: true
birthDay: 2001/01/23
favorite:
- swimming
- running
@PropertySource(value = {"classpath:student.properties"})
@ConfigurationProperties(prefix = "student") // 将配置文件中的student对象值注入该类
@Component// 将该类加入到Spring容器中
public class Student {
注意:
- 该注解只支持properties,并不支持yml。
- 使用properties文件是注意将编码改为ASCII.否则会出现中文乱码问题。