现有如下 .yml
或 .properties
全局配置文件:
# application.yml
user:
user-name: jack
age: 18
birthday: 1997/2/3
boss: true
hobbys: [eat,sleep,drink]
map: {k1: v1,k2: v2}
dog: {name: jumi,color: yellow}
# application.properties
user.user-name=jack
user.age=18
user.birthday=1997/2/3
user.boss=true
user.hobbys:eat,sleep,drink
user.map.k1=v1
user.map.k2=v2
user.dog.name=jumi
user.dog.color=yellow
@ConfiguartionProperties:从全局配置文件中绑定值到标注类的属性
1、编写测试的 POJO:
// com.zze.helloworld.pojo.Dog
public class Dog {
private String name;
private String color;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
@Override
public String toString() {
return "Dog{" +
"name='" + name + '\'' +
", color='" + color + '\'' +
'}';
}
}
// com.zze.helloworld.pojo.User
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.List;
import java.util.Map;
/**
* 将配置文件中配置的每一个属性的值,映射到这个组件中
*
* @ConfigurationProperties:告诉 SpringBoot 将本类中的所有属性和配置文件中相关的配置进行绑定
* prefix = "user":配置文件中哪个下面的属性进行映射
* <p>
* 只有这个组件是容器中的组件,才能使用容器提供的 @ConfigurationProperties 注解
*/
@Component
@ConfigurationProperties(prefix = "user")
public class User {
private String userName;
private Integer age;
private Date birthday;
private Boolean isBoss;
private List<String> hobbys;
private Map<String, String> map;
private Dog dog;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public Boolean getBoss() {
return isBoss;
}
public void setBoss(Boolean boss) {
isBoss = boss;
}
public List<String> getHobbys() {
return hobbys;
}
public void setHobbys(List<String> hobbys) {
this.hobbys = hobbys;
}
public Map<String, String> getMap() {
return map;
}
public void setMap(Map<String, String> map) {
this.map = map;
}
public Dog getDog() {
return dog;
}
public void setDog(Dog dog) {
this.dog = dog;
}
@Override
public String toString() {
return "User{" +
"name='" + userName + '\'' +
", age=" + age +
", birthday=" + birthday +
", isBoss=" + isBoss +
", hobbys=" + hobbys +
", map=" + map +
", dog=" + dog +
'}';
}
}
2、测试:
import com.zze.helloworld.pojo.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class HelloworldApplicationTests {
@Autowired
public User user;
@Test
public void test() {
System.out.println(user);
/*
User{name='jack', age=18, birthday=Mon Feb 03 00:00:00 CST 1997, isBoss=true, hobbys=[eat, sleep, drink], map={k1=v1, k2=v2}, dog=Dog{name='jumi', color='yellow'}}
*/
}
}
@Value:从全局配置文件绑定值到标注属性
1、编写测试 POJO:
// com.zze.helloworld.pojo.User
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.List;
import java.util.Map;
@Component
public class User {
@Value("${user.user-name}")
private String userName;
@Value("${user.age}")
private Integer age;
@Value("${user.birthday}")
private Date birthday;
@Value("${user.boss}")
private Boolean isBoss;
private List<String> hobbys;
private Map<String, String> map;
private Dog dog;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public Boolean getBoss() {
return isBoss;
}
public void setBoss(Boolean boss) {
isBoss = boss;
}
public List<String> getHobbys() {
return hobbys;
}
public void setHobbys(List<String> hobbys) {
this.hobbys = hobbys;
}
public Map<String, String> getMap() {
return map;
}
public void setMap(Map<String, String> map) {
this.map = map;
}
public Dog getDog() {
return dog;
}
public void setDog(Dog dog) {
this.dog = dog;
}
@Override
public String toString() {
return "User{" +
"name='" + userName + '\'' +
", age=" + age +
", birthday=" + birthday +
", isBoss=" + isBoss +
", hobbys=" + hobbys +
", map=" + map +
", dog=" + dog +
'}';
}
}
2、测试:
import com.zze.helloworld.pojo.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class HelloworldApplicationTests {
@Autowired
public User user;
@Test
public void test() {
System.out.println(user);
/*
User{name='jack', age=18, birthday=Mon Feb 03 00:00:00 CST 1997, isBoss=true, hobbys=null, map=null, dog=null}
*/
}
}
@ConfiguartionProperties 与 @Value 对比
@ConfiguartionProperties | @Value | |
---|---|---|
功能 | 批量给属性注入配置文件中值 | 一个个指定 |
松散语法 | 支持 | 不支持 |
SPEL | 不支持 | 支持 |
复杂类型封装 | 支持 | 不支持 |
在注入的同时还可以对将注入属性的值进行校验,遵循 JSR303 规范,如下:
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;
import javax.validation.constraints.Email;
import java.util.Date;
import java.util.List;
import java.util.Map;
@Component
@ConfigurationProperties(prefix = "user")
@Validated // 添加该注解表示要对注入的值进行校验
public class User {
@Email // 该注解要求注入的值为 email 格式
private String email;
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
评论区