简体   繁体   中英

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

Could you help me what the proper way of writing spring bean with parameter of list values which I get from .properties file.

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

You need to tell spring to load your property file :

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

Please note that your file application.properties must be in the classpath of your project ( src/main/resources is a good pick if you use the maven way)

Then you can use the constructor-arg tag to populate your 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> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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