繁体   English   中英

用构造器编写一个Spring bean,该构造器包含属性文件中的值列表

[英]Write a spring bean with a constructor that contains a list of values from property file

你能帮我用从.properties文件中获得的带有列表值的参数编写spring bean的正确方法是什么

  <bean id="property" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" value="classpath:dateFormat.properties" />
</bean>

<bean id="directoryMarshallerFolder1" class="threadService.DirectoryMarshalerFolder1">

    <constructor-arg>
        <list>
            ...
            <value = "${folder1.path}"/> ?????
            <value = "${folder2.path}"/> 
            ...
        </list>
    </constructor-arg>

</bean>

您需要告诉spring加载您的属性文件:

<bean name="propertyPlaceholder" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            <property name="locations">
                <value>classpath:application.properties</value>
            </property>
        </bean>

请注意,文件application.properties必须位于项目的类路径中(如果使用maven方式,则src/main/resources是一个不错的选择)

然后,您可以使用constructor-arg标记填充您的bean:

  <constructor-arg index="0" value="${property.key1}"/>
  <constructor-arg index="1" ref="${property.key2}"  />

我已经找到了结果。

 <constructor-arg> <list> <value>${folder1.path}</value> <value>${folder2.path}</value> </list> </constructor-arg> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM