从属性文件中读取列表并加载弹簧注释 @Value [英] Reading a List from properties file and load with spring annotation @Value

查看:58
本文介绍了从属性文件中读取列表并加载弹簧注释 @Value的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 .properties 文件中有一个值列表,即:

I want to have a list of values in a .properties file, ie:

my.list.of.strings=ABC,CDE,EFG

并直接在我的类中加载它,即:

And to load it in my class directly, ie:

@Value("${my.list.of.strings}")
private List<String> myList;

据我所知,另一种方法是将它放在 spring 配置文件中,并将其作为 bean 引用加载(如果我错了,请纠正我),即

As I understand, an alternative of doing this is to have it in the spring config file, and load it as a bean reference (correct me if I'm wrong), ie

<bean name="list">
 <list>
  <value>ABC</value>
  <value>CDE</value>
  <value>EFG</value>
 </list>
</bean>

但是有没有办法做到这一点?使用 .properties 文件?ps:如果可能,我想在不使用任何自定义代码的情况下执行此操作.

But is there any way of doing this? using a .properties file? ps: I would like to do this with out any custom code if possible.

推荐答案

使用 Spring EL:

Using Spring EL:

@Value("#{'${my.list.of.strings}'.split(',')}") 
private List<String> myList;

假设您的属性文件使用以下内容正确加载:

Assuming your properties file is loaded correctly with the following:

my.list.of.strings=ABC,CDE,EFG

这篇关于从属性文件中读取列表并加载弹簧注释 @Value的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆