yaml配置注入实体类:
- 在实体类上写上注解@Component和@ConfigurationProperties(prefix = “类名”)
- jsr303需要写注解@Validated
- 例如:
@Component
@ConfigurationProperties(prefix = "person")
@Validated
public class Person {
@Email(message = "邮箱格式的问题")
String name;
@NotNull(message = "不能为blank")
int age;
Boolean happy;
Date bir;
Dog dog;
Map<String,Object> map;
List<Object> list;
public Person() {
}
4.yaml
server:
port: 8081
#通过yml给类赋值
person:
name: 1191132442@qq.com
age: 5
happy: true
bir: 2019/8/9
map: {name: 1,sex: 5}
list:
- code
- music
- girl
- lubao
dog:
name: ${person.address:santian}
age: ${random.int}
dog:
name: 旺财
age: 7