亚洲免费在线视频-亚洲啊v-久久免费精品视频-国产精品va-看片地址-成人在线视频网

您的位置:首頁技術文章
文章詳情頁

在Spring-Boot中如何使用@Value注解注入集合類

瀏覽:45日期:2023-06-27 16:54:04

我們在使用spring框架進行開發時,有時候需要在properties文件中配置集合內容并注入到代碼中使用。本篇文章的目的就是給出一種可行的方式。

1.注入

通常來說,我們都使用@Value注解來注入properties文件中的內容,注入集合類時,我們也使用@Value來注入。

properties文件中的內容如下:

my.set=foo,barmy.list=foo,barmy.map={'foo': 'bar'}

分別是我們要注入的Set,List,Map中的內容。

注入方式如下:

@Value('#{${my.map}}')private Map<String, String> map;@Value('#{’${my.set}’}')private Set<String> set;@Value('#{’${my.list}’}')private List<String> list;2.驗證

我們寫一個單測類來驗證上面的注入是否可行。

@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中的內容已經在上面給出,位置在test文件夾下的resources文件夾下面(maven項目的文件夾結構)。

3.原理

在我們使用的@Value注解中,每一個開頭都有個#,這其實就是說明我們使用了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);我們也使用單元測試來驗證:

@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方法的代碼已經在驗證使用@Value注解方式的單元測試中給出。

4.總結

我們用@Value注解把properties文件中的內容注入了集合類,注解中以#開頭,其實就是使用了SpEL。

Spring-Boot的版本是2.2.1.RELEASE,之所以要說這個,是因為一開始使用1.x版本時無法注入Set和List。

以上為個人經驗,希望能給大家一個參考,也希望大家多多支持好吧啦網。

標簽: Spring
相關文章:
主站蜘蛛池模板: 欧美大片一级毛片 | 日韩精品免费视频 | 久艹在线观看 | 国产欧美日本在线观看 | 午夜不卡视频 | 国产在线一区在线视频 | 特黄特色大片免费播放路01 | 亚洲人成影院在线高清 | 亚洲免费观看在线视频 | 国产成人在线免费观看 | 亚洲成人在线视频网站 | 18视频免费网址在线观看 | 亚洲精品www久久久久久久软件 | 国产成人一区在线播放 | 国内精自线一二区 | 国产精品久久久久久一区二区三区 | 日本欧美精品 | 国产一区二区精品久 | 日本三级成人中文字幕乱码 | 真人一级毛片全部免 | aaa在线观看高清免费 | 亚洲欧洲无码一区二区三区 | 国产精品吹潮在线播放 | 富二代精品视频 | 99热在线观看 | 一区二区精品在线 | 九月婷婷亚洲综合在线 | 成人手机看片 | 日本在线免费播放 | 一本一本久久a久久精品综合麻豆 | 国产欧美一区二区三区视频 | 美女视频网站色 | 国产成人精品午夜 | 日本暖暖视频在线播放 | 亚洲精品视频久久久 | 国产呦系列呦 | 亚洲男人第一天堂 | 久久精品国产99精品最新 | 欧美激情自拍 | 久爱综合| 9191久久久久视频 |