如果說,我們只是在某個業務邏輯中需要獲取一下配置文件中的某項值,使用@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;
}