Add system properties through spring XML configuration.

If you want to set system properties through XML configuration, here is a solution (inspired by this question):

Use two MethodInvokingFactorybeans, one to statically access System.getProperties() and one to call properties.putAll(map) on these properties. Here’s the configuration:

<bean
    class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
    <property
        name="targetObject">
        <!-- System.getProperties() -->
        <bean
            class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
            <property name="targetClass" value="java.lang.System" />
            <property name="targetMethod" value="getProperties" />
        </bean>
    </property>
    <property
        name="targetMethod"
        value="putAll" />
    <property
        name="arguments">
        <!-- The new Properties -->
        <util:properties>
            <prop
                key="my.key">myvalue</prop>
            <prop
                key="my.key2">myvalue2</prop>
            <prop
                key="my.key3">myvalue3</prop>

        </util:properties>
    </property>
</bean>

Of course I am not saying this is a best practice (far from it). But it’s probably the easiest way to add system properties using XML only.

About these ads
This entry was posted in Java, Spring. Bookmark the permalink.

2 Responses to Add system properties through spring XML configuration.

  1. springer says:

    Just pasted the code as-is ; getting an error “The prefix “util” for element “util:properties” is not bound.” . Is util a custom namespace? . I’m using Spring 3.0.6.

    Thanks,
    springer

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s