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