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

StackOverflow https://stackoverflow.com/questions/19677179

  •  01-07-2022
  •  | 
  •  

Вопрос

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>
Это было полезно?

Решение 2

I've already found the result.

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

Другие советы

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}"  />
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top