SpringBoot配置文件注入值数据校验
1 package com.hoje.springboot.bean; 2 3 import org.springframework.beans.factory.annotation.Value; 4 import org.springframework.boot.context.properties.ConfigurationProperties; 5 import org.springframework.boot.context.properties.EnableConfigurationProperties; 6 import org.springframework.stereotype.Component; 7 import org.springframework.validation.annotation.Validated; 8 9 import javax.validation.constraints.Email; 10 import java.util.Date; 11 import java.util.List; 12 import java.util.Map; 13 14 /** 15 * 将配置文件中配置的每一个属性的值,映射到这个组件中 16 * @ConfigurationProperties :告诉SpringBOot将本类中的所有属性和配置文件中相关配置进行绑定; 17 * prefix = "person":配置文件中哪个下面的所有属性进行一一映射 18 * 19 * 只有这个组件是容器中的组件,才能使用容器中的功能; 20 */ 21 @Component 22 @ConfigurationProperties(prefix="person") 23 @Validated 24 public class Person { 25 26 /** 27 *<bean class="Person"> 28 * <property name="LastName" value="字面量/${key}从环境变量、配置文件中获取值/#{SpEL}"></> 29 * </> 30 */ 31 /* @Value("${person.last-name}")*/ 32 @Email 33 private String lastName; 34 /* @Value("#{11*2}")*/ 35 private Integer age; 36 /* @Value("true")*/ 37 private Boolean boss; 38 private Date bitrh; 39 private Map<String,Object> maps; 40 private List<Object> lists; 41 private Dog dog; 42 43 @Override 44 public String toString() { 45 return "Person{" + 46 "lastName='" + lastName + '\'' + 47 ", age=" + age + 48 ", boss=" + boss + 49 ", bitrh=" + bitrh + 50 ", maps=" + maps + 51 ", lists=" + lists + 52 ", dog=" + dog + 53 '}'; 54 } 55 56 public String getLastName() { 57 return lastName; 58 } 59 60 public void setLastName(String lastName) { 61 this.lastName = lastName; 62 } 63 64 public Integer getAge() { 65 return age; 66 } 67 68 public void setAge(Integer age) { 69 this.age = age; 70 } 71 72 public Boolean getBoss() { 73 return boss; 74 } 75 76 public void setBoss(Boolean boss) { 77 this.boss = boss; 78 } 79 80 public Date getBitrh() { 81 return bitrh; 82 } 83 84 public void setBitrh(Date bitrh) { 85 this.bitrh = bitrh; 86 } 87 88 public Map<String, Object> getMaps() { 89 return maps; 90 } 91 92 public void setMaps(Map<String, Object> maps) { 93 this.maps = maps; 94 } 95 96 public List<Object> getLists() { 97 return lists; 98 } 99 100 public void setLists(List<Object> lists) { 101 this.lists = lists; 102 } 103 104 public Dog getDog() { 105 return dog; 106 } 107 108 public void setDog(Dog dog) { 109 this.dog = dog; 110 } 111 }