如果说,我们只是在某个业务逻辑中需要获取一下配置文件中的某项值,使用@Value;
@Value("${person.last-name}")
private String lastName;
如果说,我们专门编写了一个javaBean来和配置文件进行映射,我们就直接使用@ConfigurationProperties;
@Component
@ConfigurationProperties(prefix = "person")
public class Person {
private String lastName;
private Integer age;
private Boolean boss;
private Date birth;
private Map<String,Object> maps;
private List<Object> lists;
private Dog dog;
@ConfigurationProperties注解JSR303数据校验
@Component
@ConfigurationProperties(prefix = "person")
//校验注解
@Validated
public Person {
//该属性必须符合email书写格式
@Email
private String email;
}