这篇文章主要讲解了“在Spring-Boot中怎么使用@Value注解注入集合类”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“在Spring-Boot中怎么使用@Value注解注入集合类”吧!
我们在使用spring框架进行开发时,有时候需要在properties文件中配置集合内容并注入到代码中使用。本篇文章的目的就是给出一种可行的方式。
通常来说,我们都使用@Value注解来注入properties文件中的内容,注入集合类时,我们也使用@Value来注入。
my.set=foo,bar
my.list=foo,bar
my.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;
我们写一个单测类来验证上面的注入是否可行。
@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项目的文件夹结构)。
在我们使用的@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注解方式的单元测试中给出。
我们用@Value注解把properties文件中的内容注入了集合类,注解中以#开头,其实就是使用了SpEL。
Spring-Boot的版本是2.2.1.RELEASE,之所以要说这个,是因为一开始使用1.x版本时无法注入Set和List。
感谢各位的阅读,以上就是“在Spring-Boot中怎么使用@Value注解注入集合类”的内容了,经过本文的学习后,相信大家对在Spring-Boot中怎么使用@Value注解注入集合类这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。