@Value獲取值和@ConfigurationProperties獲取值比較

如果說,我們只是在某個業務邏輯中需要獲取一下配置文件中的某項值,使用@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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章