springboot进行开发中,需要将配置文件里的数据绑定到javabean中,可以使用@ConfigurationProperties注解方便的进行绑定
首先准备两个javabean
@Data
public class Address {
private String province;
private String city;
}
@Data
public class Student {
private String name;
private Integer age;
private Address address;
}
在配置文件中对javabean进行配置信息
student.name=李磊
student.age=30
student.address.province=广东
student.address.city=深圳
对User类进行加注解@ConfigurationProperties(prefix = "user")和@Component
@ConfigurationProperties(prefix = "user")表示该类与配置文件中以“user”为前缀的配置信息进行绑定;
@Component表示将该JavaBean注入到容器中。
@Data
@Component
@ConfigurationProperties(prefix = "student")
public class Student {