@Configuration和@Value的区别

尝试使用@Value为对象的属性单独赋值,在给非基本类型赋值时报错

代码:

yml配置文件:

#person:
#  lastName: zhangsan
#  age: 18
#  boss: false
#  birth: 2017/12/12
#  maps: {k1: v1,k2: 12}
#  lists: [lisi,zhaoliu]
#  dog: {name: 小狗,age: 2}
lastName: zhangsan

dog: {name: 小狗,age: 2}

boss: false

maps: {k1: v1,k2: 12}

lists: [lisi,zhaoliu]

age: 18

birth: 2017/12/12

JavaBean:

Component
//@ConfigurationProperties(prefix="person")
public class Person {
    @Value("${lastName}")
    private String lastName;
    @Value("${age}")
    private Integer age;
    @Value("${boss}")
    private Boolean boss;
    @Value("${birth}")
    private Date birth;

    @Value("${maps}")
    private Map maps;
    @Value("${lists}")
    private List lists;
    @Value("${dog}")
    private Dog dog; 
  

结论:

@Configuration支持复杂类型的封装,@Value不支持

你可能感兴趣的:(spiring)