3

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

I know how to init a set like this:

<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 the values in the set using values from .properties file instead of hard-coding those value in the xml?

Update 1: Since I might have different numbers of values for this set in different environments, using the hard-coded set in the xml won't work. That's why I need to somehow fetch these values from a properties file.

Update 2: I came up with a quick-and-dirty way to do this which is list all values as ONE single string in the .properties file and then set this value to the MagicBean. Then in Java code, parse this string. Any better idea?

0x56794E
  • 20,883
  • 13
  • 42
  • 58

2 Answers2

1

You can use:

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

And set the value in property file:

my.set.value1=Value1
BobTheBuilder
  • 18,858
  • 6
  • 40
  • 61
  • But I don't know how many values I will have. I might have different numbers of values for this set in different environments. – 0x56794E Feb 20 '14 at 06:24
  • So how can you declare it in your xml? – BobTheBuilder Feb 20 '14 at 06:25
  • Ah, what I meant from my previous comment was that what I'm getting at. I might have different numbers of values for this set in different environments, thus using the hard-coded set in the xml won't work. That's why I need to get these values from a properties file – 0x56794E Feb 20 '14 at 06:26
  • Ok I see. Take a look at these (not set specific but conditional sprin config) http://stackoverflow.com/questions/3035630/how-to-achieve-conditional-resource-import-in-a-spring-xml-context and http://stackoverflow.com/questions/6906863/conditional-statement-inside-spring-config – BobTheBuilder Feb 20 '14 at 06:31
1

try something like this

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

this is B1

class B1 {
    public void setProps(Properties props) {
        ...
    }
}
Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275