标签:protoc setname bean 时间 最大值 min string 约束 between
person:
name: qinjiang
age: ${random.int} #使用占位符设置值
happy: false
birth: 2000/01/01
maps: {k1: v1,k2: v2}
lists:
- code
- girl
- music
dog:
dog-name: 旺财
age: ${random.int}
dog:
dog-name: 阿黄 #名字与属性松散绑定
age: 5
@Component @ConfigurationProperties(prefix = "dog") public class Dog { private String dogName; private Integer age; public Dog() { } public Dog(String dogName, Integer age) { this.dogName = dogName; this.age = age; } public String getDogName() { return dogName; } public void setDogName(String dogName) { this.dogName = dogName; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Override public String toString() { return "Dog{" + "dogName=‘" + dogName + ‘\‘‘ + ", age=" + age + ‘}‘; } }
空检查
Booelan检查
长度检查
日期检查
数值检查
建议使用在Stirng,Integer类型,不建议使用在int类型上,因为表单值为“”时无法转换为int,但可以转换为Stirng为”“,Integer为null
代码展示:
1 @Component 2 @ConfigurationProperties(prefix = "person") 3 @Validated 4 public class Person { 5 6 @Email() 7 private String name; 8 private Integer age; 9 private Boolean happy; 10 private Date birth; 11 private Map<String,Object> maps; 12 private List<Object> lists; 13 private Dog dog; 14 15 public Person() { 16 } 17 18 public Person(String name, Integer age, Boolean happy, Date birth, Map<String, Object> maps, List<Object> lists, Dog dog) { 19 this.name = name; 20 this.age = age; 21 this.happy = happy; 22 this.birth = birth; 23 this.maps = maps; 24 this.lists = lists; 25 this.dog = dog; 26 } 27 28 public String getName() { 29 return name; 30 } 31 32 public void setName(String name) { 33 this.name = name; 34 } 35 36 public Integer getAge() { 37 return age; 38 } 39 40 public void setAge(Integer age) { 41 this.age = age; 42 } 43 44 public Boolean getHappy() { 45 return happy; 46 } 47 48 public void setHappy(Boolean happy) { 49 this.happy = happy; 50 } 51 52 public Date getBirth() { 53 return birth; 54 } 55 56 public void setBirth(Date birth) { 57 this.birth = birth; 58 } 59 60 public Map<String, Object> getMaps() { 61 return maps; 62 } 63 64 public void setMaps(Map<String, Object> maps) { 65 this.maps = maps; 66 } 67 68 public List<Object> getLists() { 69 return lists; 70 } 71 72 public void setLists(List<Object> lists) { 73 this.lists = lists; 74 } 75 76 public Dog getDog() { 77 return dog; 78 } 79 80 public void setDog(Dog dog) { 81 this.dog = dog; 82 } 83 84 @Override 85 public String toString() { 86 return "Person{" + 87 "name=‘" + name + ‘\‘‘ + 88 ", age=" + age + 89 ", happy=" + happy + 90 ", birth=" + birth + 91 ", maps=" + maps + 92 ", lists=" + lists + 93 ", dog=" + dog + 94 ‘}‘; 95 } 96 }
源码位置:
file:./config/ ,项目目录下建立一个config文件夹,config文件夹下建立application.yaml
file:./ ,项目目录下直接建立一个application.yaml
classpath:/config/ ,resources下建立一个config文件夹,config文件夹下建立application.yaml
classpath:/ ,resources下直接建立一个application.yaml
1)、SpringBoot启动会加载大量的自动配置类
2)、我们看我们需要的功能有没有在SpringBoot默认写好的自动配置类当中;
3)、我们再来看这个自动配置类中到底配置了哪些组件;(只要我们要用的组件存在在其中,我们就不需要再手动配置了)
4)、给容器中自动配置类添加组件的时候,会从properties类中获取某些属性。我们只需要在配置文件中指定这些属性的值即可;
xxxxAutoConfigurartion:自动配置类;给容器中添加组件
xxxxProperties:封装配置文件中相关属性;
application.yaml:用于修改springboot的属性
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
3.1.补充:SpringBoot属性赋值,JSR303校验,多环境配置以及配置文件位置
标签:protoc setname bean 时间 最大值 min string 约束 between
原文地址:https://www.cnblogs.com/zhihaospace/p/12348283.html