在Spring-Boot中如何使用@Value注解注入集合類
我們?cè)谑褂胹pring框架進(jìn)行開發(fā)時(shí),有時(shí)候需要在properties文件中配置集合內(nèi)容并注入到代碼中使用。本篇文章的目的就是給出一種可行的方式。
1.注入
通常來(lái)說(shuō),我們都使用@Value注解來(lái)注入properties文件中的內(nèi)容,注入集合類時(shí),我們也使用@Value來(lái)注入。
properties文件中的內(nèi)容如下:
my.set=foo,bar
my.list=foo,bar
my.map={"foo": "bar"}
分別是我們要注入的Set,List,Map中的內(nèi)容。
注入方式如下:
@Value("#{${my.map}}")
private Map<String, String> map;
@Value("#{'${my.set}'}")
private Set<String> set;
@Value("#{'${my.list}'}")
private List<String> list;
2.驗(yàn)證
我們寫一個(gè)單測(cè)類來(lái)驗(yàn)證上面的注入是否可行。
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE,
classes = PropertiesTest.ClassUsingProperties.class)
@TestPropertySource(locations = "classpath:test.properties")
public class PropertiesTest {
@Autowired
private ClassUsingProperties classUsingProperties;
@Test
public void testInjectCollectionFieldsUsingPropertiesFile() {
Map<String, String> map = classUsingProperties.getMap();
Set<String> set = classUsingProperties.getSet();
List<String> list = classUsingProperties.getList();
asserts(map, set, list);
}
private void asserts(Map<String, String> map, Set<String> set, List<String> list) {
Assert.assertEquals(map.get("foo"), "bar");
Assert.assertTrue(set.contains("foo"));
Assert.assertTrue(set.contains("bar"));
Assert.assertTrue(list.contains("foo"));
Assert.assertTrue(list.contains("bar"));
}
@Data
@Component
public static class ClassUsingProperties {
@Value("#{${my.map}}")
private Map<String, String> map;
@Value("#{'${my.set}'}")
private Set<String> set;
@Value("#{'${my.list}'}")
private List<String> list;
}
}
test.properties中的內(nèi)容已經(jīng)在上面給出,位置在test文件夾下的resources文件夾下面(maven項(xiàng)目的文件夾結(jié)構(gòu))。
3.原理
在我們使用的@Value注解中,每一個(gè)開頭都有個(gè)#,這其實(shí)就是說(shuō)明我們使用了SpEL,如果直接使用SpEL,
就是下面的代碼:
ExpressionParser parser = new SpelExpressionParser();
Map<String, String> map =
(Map<String, String>) parser
.parseExpression({'foo':'bar'}")
.getValue(Map.class);
Set<String> set =
(Set<String>) parser
.parseExpression("'foo,bar'")
.getValue(Set.class);
List<String> list =
(List<String>) parser
.parseExpression("'foo,bar'")
.getValue(List.class);
我們也使用單元測(cè)試來(lái)驗(yàn)證:
@Test
@SuppressWarnings("unchecked")
public void testInitCollectionUsingSpEL() {
ExpressionParser parser = new SpelExpressionParser();
Map<String, String> map =
(Map<String, String>) parser
.parseExpression("{'foo':'bar'}")
.getValue(Map.class);
Set<String> set =
(Set<String>) parser
.parseExpression("'foo,bar'")
.getValue(Set.class);
List<String> list =
(List<String>) parser
.parseExpression("'foo,bar'")
.getValue(List.class);
asserts(map, set, list);
}
asserts方法的代碼已經(jīng)在驗(yàn)證使用@Value注解方式的單元測(cè)試中給出。
4.總結(jié)
我們用@Value注解把properties文件中的內(nèi)容注入了集合類,注解中以#開頭,其實(shí)就是使用了SpEL。
Spring-Boot的版本是2.2.1.RELEASE,之所以要說(shuō)這個(gè),是因?yàn)橐婚_始使用1.x版本時(shí)無(wú)法注入Set和List。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java的微信開發(fā)中使用XML格式和JSON格式數(shù)據(jù)的示例
這篇文章主要介紹了Java微信開發(fā)中使用XML格式和JSON格式數(shù)據(jù)的示例,注意一下json-lib所需要的jar包,需要的朋友可以參考下2016-02-02
從0開始學(xué)習(xí)大數(shù)據(jù)之java spark編程入門與項(xiàng)目實(shí)踐
這篇文章主要介紹了從0開始學(xué)習(xí)大數(shù)據(jù)之java spark編程入門與項(xiàng)目實(shí)踐,結(jié)合具體入門項(xiàng)目分析了大數(shù)據(jù)java spark編程項(xiàng)目建立、調(diào)試、輸出等相關(guān)步驟及操作技巧,需要的朋友可以參考下2019-11-11
Java中args參數(shù)數(shù)組的用法說(shuō)明
這篇文章主要介紹了Java中args參數(shù)數(shù)組的用法說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2021-02-02
java并發(fā)編程StampedLock高性能讀寫鎖詳解
這篇文章主要為大家介紹了java并發(fā)編程StampedLock高性能讀寫鎖的示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05

