9

I have some spring config that uses a property, like so:

<bean id="foo" class="...">
    <constructor-arg value="${aProperty}"/>
</bean>

Obviously I know I can resolve this property by having a properties file (say example.properties):

aProperty=value

and importing this file in the Spring config:

<bean id="propertyConfiguration" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>example.properties</value>
        </list>
    </property>
</bean>

My question is, can I set this property directly in the XML file instead of having to create a separate properties file? Something like this would be ideal:

<set-property name="aProperty" value="value"/>

Maven has a similar feature for pom files:

<properties><aProperty>value</aProperty></properies>
tonicsoft
  • 1,736
  • 9
  • 22

1 Answers1

12

The goal of using a properties file is uncouple values from Spring configuration files, so it's a little weird define a property in the same configuration file. Nevertheless you always can add properties to your PropertyPlaceholderConfigurer:

<bean id="propertyConfiguration" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>example.properties</value>
        </list>
    </property>
    <property name="properties">
        <props>
            <prop key="aa">bb</prop>
            <prop key="cc">dd</prop>
        </props>
    </property>
</bean>

Hope it helps.

eltabo
  • 3,749
  • 1
  • 21
  • 33
  • 1
    Perfect. The reason for wanting to do this is the property value itself is the name of a properties file passed to a third party library. As this is the only property that is different between the 6+ instances of my app, I didn't want to create six new property files, all of which had one property which was the name of yet another property file! – tonicsoft Oct 07 '16 at 17:50