繼上一篇博客SpringBoot系列之YAML配置用法以後,再寫一篇@Value、@ConfigurationProperties的對比博客html
這兩個主鍵都是能夠獲取配置文件屬性的,不過是有比較大的區別的,因此本博客作一下對比,ok,繼續拿上一篇博客的例子來實驗java
## 測試ConfigurationProperties user: userName: root isAdmin: true regTime: 2019/11/01 isOnline: 1 maps: {k1 : v1,k2: v2} lists: - list1 - list2 address: tel: 15899988899 name: 上海市
例子:
將yml的配置改一下spring
user: user-name: root
先用@ConfigurationProperties測試app
package org.muses.jeeplatform.bean; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; import java.util.Date; import java.util.List; import java.util.Map; @Component @ConfigurationProperties(prefix = "user") public class User { private String userName; private boolean isAdmin; private Date regTime; private Long isOnline; private Map<String,Object> maps; private List<Object> lists; private Address address; @Override public String toString() { return "User{" + "userName='" + userName + '\'' + ", isAdmin=" + isAdmin + ", regTime=" + regTime + ", isOnline=" + isOnline + ", maps=" + maps + ", lists=" + lists + ", address=" + address + '}'; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public boolean isAdmin() { return isAdmin; } public void setAdmin(boolean admin) { isAdmin = admin; } public Date getRegTime() { return regTime; } public void setRegTime(Date regTime) { this.regTime = regTime; } public Long getIsOnline() { return isOnline; } public void setIsOnline(Long isOnline) { this.isOnline = isOnline; } public Map<String, Object> getMaps() { return maps; } public void setMaps(Map<String, Object> maps) { this.maps = maps; } public List<Object> getLists() { return lists; } public void setLists(List<Object> lists) { this.lists = lists; } public Address getAddress() { return address; } public void setAddress(Address address) { this.address = address; } }
Junit測試,能夠讀到數據ide
User{userName='root', isAdmin=false, regTime=Fri Nov 01 00:00:00 CST 2019, isOnline=1, maps={k1=v1, k2=v2}, lists=[list1, list2], address=Address{tel='15899988899', name='上海市'}}
改一下,用@Value去讀測試
user: userName: root is-admin: true
@Value("${userName}") private String userName; @Value("${is-Admin}") private boolean isAdmin;
ok,發現報錯了ui
org.springframework.beans.TypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'boolean'
ok,本博客例子是用application.yml配置的,對於application.properties的本博客沒介紹,不過仍是要隨便提一下,application.properties文件的默認文件編碼是utf8,因此寫中文時候有時候會出現亂碼問題
這時候能夠修改編碼:file->settings->Editor->file encodings
this
本博客之對比一下,這兩種註解編碼
ok,先改下配置,看看@ConfigurationProperties能獲取到?.net
user: isOnline: #{1*1}
debug了一下,發現不能正常計算
ok,驗證@value
@Value("#{1*1}") private Long isOnline;
junit測試,ok,@Value是支持的
User{userName='null', isAdmin=false, regTime=null, isOnline=1, maps=null, lists=null, address=null}
@ConfigurationProperties驗證:
@AssertTrue private boolean isAdmin;
junit測試,發現能夠支持
@Value驗證
@AssertTrue @Value("${isAdmin}") private boolean isAdmin;
驗證,校驗發現不起效
因此,本博客驗證一下@Value是否支持就能夠
@Value("${maps}") private Map<String,Object> maps;
junit測試,發現類型轉換錯誤
Caused by: org.springframework.beans.ConversionNotSupportedException: Failed to convert value of type 'java.lang.String' to required type 'java.util.Map';
綜上,能夠概括一下@Value和@ConfigurationProperties兩種屬性的區別
| | @ConfigurationProperties| @Value|
|--|--|--|
| 功能對比| 批量注入配置文件屬性| 一個一個屬性的注入|
| 鬆散綁定| 支持 | 不支持|
| SpEL| 不支持 |支持 |
| JSR303數據校驗| 支持| 不支持|
| 複雜類型封裝| 支持 |不支持 |
因此,@ConfigurationProperties適用與注入配置文件整個對應bean的所有屬性,而@Value正如其名稱同樣,適合注入配置文件單個值