How to initialize a set in spring bean using value from .properties file

I have a spring bean (let's call it MagicBean) that has HashSet<String>as one of its properties.

I know how to initialize such a set:

<bean id="mySet" class="org.springframework.beans.factory.config.SetFactoryBean">
    <property name="targetSetClass" value="java.util.HashSet"/>
    <property name="sourceSet">
        <set>
            <value>Value 1</value>
            <value>Value 2</value>
            <value>Value 3</value>
        </set>
    </property>
</bean>

<bean id="magicBean" class="MagicBean">
    <property name="mySet" ref="mySet"/>
</bean>

Is there a way to set values ​​in a set using values ​​from .properties, instead of hard-coding those values ​​in xml?

Update 1: Since I may have a different number of values ​​for this set in different environments, using a hard-coded set in xml will not work. Therefore, I need to somehow extract these values ​​from the properties file.

2: , .properties, MagicBean. Java- . ?

+3
2

:

<value>${my.set.value1}/value>

:

my.set.value1=Value1
+1

-

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:util="http://www.springframework.org/schema/util" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/util
                           http://www.springframework.org/schema/util/spring-util.xsd
                           ">
    <bean class="B1">
        <property name="Props">
            <util:properties location="classpath:test.properties" />
        </property>
    </bean>
</beans>

B1

class B1 {
    public void setProps(Properties props) {
        ...
    }
}
+1

All Articles