Properties based configuration

Overview

This document describes the steps required to get spring-config up and running in your application using the java.util.Properties class as the configuration agent. It's also probably worth reading the Customizing bean factories with BeanFactoryPostprocessors section of the Spring reference document to get a better idea of how spring-config does it's magic. It might even be worth not using spring-config if you are only going to use properties based configuration as Spring already provides the functionality.

Define the configuration agent

Firstly you'll need to register the configuration agent with spring's bean factory. spring-config provides a factory class that can be used to merge several properties files into one.

<bean id="configurationFactory"
            class="org.reactive.configuration.PropertiesConfigurationFactory"
            singleton="true">
    <property name="locations">
        <value>classpath:/configuration.properties</value>
        <value>classpath:/jdbc.properties</value>
    </property>
</bean>

Now you will need to add the bean definition for the configuration instance that is returned from the above factory.

<bean id="configuration"
            factory-bean="configurationFactory"
            factory-method="getConfiguration" />

Register the placeholder processor

The ConfigurationPlaceholderProcessor class performs the bulk of the work in spring-config. It coordinates between your configuration agent (defined above) and spring's bean factory to substitute placeholders with objects returned from the configuration agent.

The placeholder processor requires that a placeholder evaluator be registered in order to look up the configured values. In the below example we set the placeholderEvaluator property to the Properties Configuration implementation.

<bean id="configurationPlaceholderProcessor"
            class="org.reactive.beans.factory.config.ConfigurationPlaceholderProcessor">
    <property name="placeholderEvaluator">
        <bean class="org.reactive.beans.factory.config.properties.PropertiesPlaceholderEvaluator">
            <property name="properties">
                <ref bean="configuration"/>
            </property>
        </bean>
    </property>
</bean>

And the rest

Now that we have told Spring about the configuration placeholder processor we can start adding placeholders to your bean definition files. Take a look at the syntax page for details on spring-config's place holder syntax.