java 在 Spring 中指定带有值的列表的最简单方法是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11577547/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 05:39:08  来源:igfitidea点击:

What is the easiest way to specify a list with values in Spring?

javaspring

提问by Kathir

Assume I'm having a large list of values aa bb cc dd ee ff gg etc., which I need to pass as a constructor in spring

假设我有一个很大的值列表 aa bb cc dd ee ff gg 等等,我需要在 spring 中将它们作为构造函数传递

If I need to configure as string array it is easy in spring as we can just specify the values as comma separated as aa, bb, cc etc.,

如果我需要配置为字符串数组,那么在 spring 中很容易,因为我们可以将值指定为逗号分隔的 aa、bb、cc 等,

If I need to configure as list I need to do like below

如果我需要配置为列表,我需要做如下

<bean name="myBean" class="MyClass">
    <constructor-arg>
        <list>
            <value>aa</value>
            <value>bb</value>
            <value>cc</value>
            <value>dd</value>
        </list>
    </constructor-arg>
</bean>

When the number of values increased it occupies a huge lines and it looks ugly.

当值的数量增加时,它占据了一条巨大的线,看起来很难看。

Could some one please help me how we can pass large values as list in string as constructor?

有人可以帮助我如何将大值作为字符串中的列表作为构造函数传递吗?

回答by AxxA Osiris

Are the values being passed to the list comming from a properties file? If so, you can use the something like this:

传递给列表的值是否来自属性文件?如果是这样,你可以使用这样的东西:

<bean name="myBean" class="MyClass">
   <constructor-arg>
      <bean class="org.springframework.util.StringUtils" factory-method="commaDelimitedListToSet">
          <constructor-arg type="java.lang.String" value="${list.value}"/>
      </bean>
    </constructor-arg>
</bean> 

with the following .properties file

使用以下 .properties 文件

list.value=aa,bb,cc,dd   

And if not, you can apparently just pass then directly :

如果没有,您显然可以直接通过 then :

<bean name="myBean" class="MyClass">
   <constructor-arg>
      <bean class="org.springframework.util.StringUtils" factory-method="commaDelimitedListToSet">
          <constructor-arg type="java.lang.String" value="aa,bb,cc,dd"/>
      </bean>
    </constructor-arg>
</bean> 

回答by Matt

Spring can automatically convert any comma separated string into a list or array for you:

Spring 可以为您自动将任何逗号分隔的字符串转换为列表或数组:

public class Foo {
   public void setValueList(String[] values) { ... }
}

<bean class="Foo"
      p:valueList="a,b,c,d" />
<bean class="Foo"
      c:_0="a,b,c,d" />
<bean class="Foo">
     <constructor-arg><value>a,b,c,d</value></constructor-arg>
</bean>

In fact, even if there's only 1 value, and no commas in the string, it will still work.

事实上,即使只有 1 个值,并且字符串中没有逗号,它仍然可以工作。

There's no need for the call to org.springframework.util.StringUtils that someone mentioned in another answer.

不需要有人在另一个答案中提到的对 org.springframework.util.StringUtils 的调用。

This words for constructor args as well (c:_0 is shorthand for <constructor-arg index="0"> using the c namespace.

这个词也用于构造函数 args(c:_0 是 <constructor-arg index="0"> 使用 c 命名空间的简写。

回答by Jacek Obarymski

You could try something even simpler and use Spring expression language to convert the values to a list:

您可以尝试更简单的方法并使用 Spring 表达式语言将值转换为列表:

<bean name="myBean" class="MyClass">
    <constructor-arg value="#{T(java.util.Arrays).asList('${list.values}')}"/>
</bean>

回答by wonhee

If you are using latest Spring framework version(Spring 3.1+ I believe), you don't need to those string split stuff in SpringEL,

如果您使用的是最新的 Spring 框架版本(我相信是 Spring 3.1+),则不需要在 SpringEL 中进行字符串拆分,

Simply add PropertySourcesPlaceholderConfigurer and DefaultConversionService in your Spring's Configuration class ( the one with annotated with Configuration ) e.g,

只需在 Spring 的 Configuration 类(带有 Configuration 注释的类)中添加 PropertySourcesPlaceholderConfigurer 和 DefaultConversionService 例如,

@Configuration
public class AppConfiguration {

    @Bean
    public static PropertySourcesPlaceholderConfigurer propertyPlaceholderConfigurer() {
        return new PropertySourcesPlaceholderConfigurer();
    }

    @Bean public ConversionService conversionService() {
        return new DefaultConversionService();
    }
}

and in your class

在你的课堂上

@Value("${list}")
private List<String> list;

Or use @Value annotation on your constructor's parameter if that's what you want to do.

或者,如果这是您想要做的,则在构造函数的参数上使用 @Value 注释。

Finally in the properties file

最后在属性文件中

list=A,B,C,D,E

Without DefaultConversionService, you can only take comma separated String into String array when you inject the value into your field, but DefaultConversionService does a few convenient magic for you and will add those into Collection, Array, etc. ( check the implementation if you'd like to know more about it )

如果没有 DefaultConversionService,当您将值注入字段时,您只能将逗号分隔的 String 放入 String 数组中,但 DefaultConversionService 为您做了一些方便的魔术,并将它们添加到 Collection、Array 等中(如果您愿意,请检查实现想了解更多)

With these two, it even handles all the redundant whitespaces including newline, so you don't need to add additional logics to trim them.

有了这两个,它甚至可以处理包括换行符在内的所有冗余空格,因此您无需添加额外的逻辑来修剪它们。

回答by MattR

You could implement a constructor that takes a comma-delimited String as its argument (and split that string yourself into the values).

您可以实现一个以逗号分隔的 String 作为参数的构造函数(并自己将该字符串拆分为值)。

If you want a solution that works for any constructor/property you could look at implementing your own PropertyEditorand configure Spring to use that.

如果您想要一个适用于任何构造函数/属性的解决方案,您可以考虑实现自己的PropertyEditor并配置 Spring 以使用它。