在Spring中指定包含值的列表的最简单方法是什么? [英] What is the easiest way to specify a list with values in Spring?

查看:92
本文介绍了在Spring中指定包含值的列表的最简单方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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等。

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.,

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

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?

推荐答案

传递给列表的值是否来自属性文件?
如果是这样,您可以使用以下内容:

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> 

以下.properties文件

with the following .properties file

list.value=aa,bb,cc,dd   

如果没有,你显然可以直接传递:

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

<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> 

这篇关于在Spring中指定包含值的列表的最简单方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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