SpringBoot 的@Value注解太強(qiáng)大了,用了都說爽!
閱讀本文大概需要 4 分鐘。
來自:https://jitwxs.cn/d6d760c4.html
List 或是 Map 這種類型的數(shù)據(jù)。List 類型為例,對(duì)于 .yml 文件配置如下:test:
list:
- aaa
- bbb
- ccc
.properties 文件配置如下所示:test.list[0]=aaa
test.list[1]=bbb
test.list[2]=ccc
@Value 注解去讀取這個(gè)值,就像下面這種寫法一樣:@Value("${test.list}")
private List<String> testList;
java.lang.IllegalArgumentException: Could not resolve placeholder 'test.list' in value "${test.list}"
test.list 為例,新建一個(gè) test 的配置類,將 list 作為該配置類的一個(gè)屬性:@Configuration
@ConfigurationProperties("test")
public class TestListConfig {
private List<String> list;
public List<String> getList() {
return list;
}
public void setList(List<String> list) {
this.list = list;
}
}
@Autowired
private TestListConfig testListConfig;
// testListConfig.getList();
二、數(shù)組怎么樣
test:
array1: aaa,bbb,ccc
array2: 111,222,333
array3: 11.1,22.2,33.3
@Value("${test.array1}")
private String[] testArray1;
@Value("${test.array2}")
private int[] testArray2;
@Value("${test.array3}")
private double[] testArray3;
@Value("${test.array1:}")
private String[] testArray1;
@Value("${test.array2:}")
private int[] testArray2;
@Value("${test.array3:}")
private double[] testArray3;
: 號(hào),冒號(hào)后的值表示當(dāng) key 不存在時(shí)候使用的默認(rèn)值,使用默認(rèn)值時(shí)數(shù)組的 length = 0。不需要寫配置類 使用逗號(hào)分割,一行配置,即可完成多個(gè)數(shù)值的注入,配置文件更加精簡(jiǎn)
業(yè)務(wù)代碼中數(shù)組使用很少,基本需要將其轉(zhuǎn)換為 List,去做 contains、foreach 等操作。
三、替代方法
EL 表達(dá)式。3.1 解析 List
.yml 文件為例,我們只需要在配置文件中,跟配置數(shù)組一樣去配置:test:
list: aaa,bbb,ccc
EL 表達(dá)式的 split() 函數(shù)進(jìn)行切分即可。@Value("#{'${test.list}'.split(',')}")
private List<String> testList;
@Value("#{'${test.list:}'.split(',')}")
private List<String> testList;

split() 之前判斷下是否為空即可。@Value("#{'${test.list:}'.empty ? null : '${test.list:}'.split(',')}")
private List<String> testList;
3.2 解析 Set
test:
set: 111,222,333,111
`@Value("#{'${test.set:}'.empty ? null : '${test.set:}'.split(',')}")
private Set<Integer> testSet;
// output: [111, 222, 333]
3.3 解析 Map
test:
map1: '{"name": "zhangsan", "sex": "male"}'
map2: '{"math": "90", "english": "85"}'
@Value("#{${test.map1}}")
private Map<String,String> map1;
@Value("#{${test.map2}}")
private Map<String,Integer> map2;
public class MapDecoder {
public static Map<String, String> decodeMap(String value) {
try {
return JSONObject.parseObject(value, new TypeReference<Map<String, String>>(){});
} catch (Exception e) {
return null;
}
}
}
@Value("#{T(com.github.jitwxs.demo.MapDecoder).decodeMap('${test.map1:}')}")
private Map<String, String> map1;
@Value("#{T(com.github.jitwxs.demo.MapDecoder).decodeMap('${test.map2:}')}")
private Map<String, String> map2;
四、后續(xù)
@Value 注解不能和 @AllArgsConstructor 注解同時(shí)使用,否則會(huì)報(bào)錯(cuò)Consider defining a bean of type 'java.lang.String' in your configuration
@Value 的內(nèi)容都很長(zhǎng),既不美觀,也不容易閱讀。推薦閱讀:
是時(shí)候裝逼了,試試 IDEA 解決 Maven 依賴沖突的高能神器!
最近面試BAT,整理一份面試資料《Java面試BATJ通關(guān)手冊(cè)》,覆蓋了Java核心技術(shù)、JVM、Java并發(fā)、SSM、微服務(wù)、數(shù)據(jù)庫(kù)、數(shù)據(jù)結(jié)構(gòu)等等。
朕已閱 

