将动态属性列表读入 spring 管理的 bean
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5274362/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
reading a dynamic property list into a spring managed bean
提问by bob
I've been searching but cannot find these steps. I hope I'm missing something obvious.
我一直在寻找,但找不到这些步骤。我希望我遗漏了一些明显的东西。
I have a properties file with the following contents:
我有一个包含以下内容的属性文件:
machines=A,B
I have another file like that but having a different number of members in the machines element like this:
我有另一个类似的文件,但在机器元素中有不同数量的成员,如下所示:
machines=B,C,D
My question is how do I load this variable-length machines variable into a bean in my spring config in a generic way?
我的问题是如何以通用方式将这个可变长度机器变量加载到我的 spring 配置中的 bean 中?
something like this:
像这样:
<property name="machines" value="${machines}"/>
where machines is an array or list in my java code. I can define it however I want if I can figure out how to do this.
其中machines是我的java代码中的数组或列表。如果我能弄清楚如何做到这一点,我可以随意定义它。
Basically I'd rather have spring do the parsing and stick each value into a list element instead of me having to write something that reads in the full machines string and do the parsing myself (with the comma delimiter) to put each value into an array or list. Is there an easy way to do this that I'm missing?
基本上,我宁愿让 spring 进行解析并将每个值粘贴到列表元素中,而不是我必须编写一些读取完整机器字符串的内容并自己进行解析(使用逗号分隔符)以将每个值放入一个数组中或列表。有没有一种简单的方法可以做到这一点,我错过了?
回答by Kris Babic
You may want to take a look at Spring's StringUtils class. It has a number of useful methods to convert a comma separated list to a Set or a String array. You can use any of these utility methods, using Spring's factory-method framework, to inject a parsed value into your bean. Here is an example:
您可能想看看 Spring 的 StringUtils 类。它有许多有用的方法可以将逗号分隔的列表转换为 Set 或 String 数组。您可以使用任何这些实用程序方法,使用 Spring 的工厂方法框架,将解析的值注入到您的 bean 中。下面是一个例子:
<property name="machines">
<bean class="org.springframework.util.StringUtils" factory-method="commaDelimitedListToSet">
<constructor-arg type="java.lang.String" value="${machines}"/>
</bean>
</property>
In this example, the value for 'machines' is loaded from the properties file.
在这个例子中,'machines' 的值是从属性文件中加载的。
If an existing utility method does not meet your needs, it is pretty straightforward to create your own. This technique allows you to execute any static utility method.
如果现有的实用方法不能满足您的需求,那么创建您自己的方法非常简单。此技术允许您执行任何静态实用程序方法。
回答by Jingyang Peng
Spring EL makes easier. Java:
Spring EL 更容易。爪哇:
List <String> machines;
Context:
语境:
<property name="machines" value="#{T(java.util.Arrays).asList('${machines}')}"/>
回答by Simon Gibbs
If you make the property "machines" a String array, then spring will do it automatically for you
如果您将属性“machines”设为字符串数组,则 spring 会自动为您执行此操作
machines=B,C,D
<property name="machines" value="${machines}"/>
public void setMachines(String[] test) {
回答by DAMungra
Since Spring 3.0, it is also possible to read in the list of values using the @Value annotation.
从 Spring 3.0 开始,还可以使用 @Value 注释读入值列表。
Property file:
属性文件:
machines=B,C,D
Java code:
爪哇代码:
import org.springframework.beans.factory.annotation.Value;
@Value("#{'${machines}'.split(',')}")
private List<String> machines;
回答by wonhee
You can inject the values to the list directly without boilerplate code.(Spring 3.1+)
您可以将值直接注入列表而无需样板代码。(Spring 3.1+)
@Value("${machines}")
private List<String> machines;
for the key "machines=B,C,D" in the properties file by creating following two instance in your configuration.
通过在您的配置中创建以下两个实例来获取属性文件中的键“machines=B,C,D”。
@Bean
public static PropertySourcesPlaceholderConfigurer propertyPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
@Bean
public ConversionService conversionService() {
return new DefaultConversionService();
}
Those will cover all the separate based split and whitespace trim as well.
这些也将涵盖所有基于单独的拆分和空白修剪。

