1. 程式人生 > >SpringBoot配置文件註入值數據校驗

SpringBoot配置文件註入值數據校驗

pan const pre ted object lec map public list

  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 }

SpringBoot配置文件註入值數據校驗