Search

Dark theme | Light theme

March 8, 2009

Use Spring Configurator to support different dev, test and production environments with Spring configuration files (Part 7)

The Spring Configurator makes it very easy to configure beans in the Spring configuration files. We can read in the previous parts (part 1, part 2, part 3, part 4, part 5, part 6) the configurator by defaults assumes we put property files in directories named cocoon. That is because the Spring Configurator was first developed as part of Cocoon 2.2. But we can also use our own directory names so we don't see any cocoon in our definitions.

For example we rename the src/main/resources/META-INF/cocoon directory of our example to src/main/resources/META-INF/app-config. Next we must add extra elements to the configurator:settings in our applicationContext.xml, see lines 13 and 14. The configurator:include-properties element contains a Spring resource path. In our case all properties from META-INF/app-config/properties and META-INF/app-config/properties/[runningMode] in the classpath are loaded, where [runningMode] is replaced with the current running mode. So if we run the application in prod running mode the path will be META-INF/app-config/properties/prod. The configurator:include-beans elements loads Spring configuration files from the META-INF/app-config/spring and META-INF/app-config/spring/[runningMode] directories in the classpath. The runningMode is replaced by the current running mode, just as with the properties.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:configurator="http://cocoon.apache.org/schema/configurator"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://cocoon.apache.org/schema/configurator http://cocoon.apache.org/schema/configurator/cocoon-configurator-1.0.1.xsd">

    <context:component-scan base-package="com.mrhaki.netbeans.mavenapp"/>
    <configurator:settings>
        <configurator:include-properties dir="classpath*:META-INF/app-config/properties"/> 
        <configurator:include-beans dir="classpath*:META-INF/app-config/spring"/> 
    </configurator:settings>

    <bean name="sample" class="com.mrhaki.netbeans.mavenapp.Sample">
        <property name="text" value="${sample.text}"/>
    </bean>

</beans>

Now we can run our application (mvn test) and all properties and Spring configuration files are read from META-INF/app-config/properties, META-INF/app-config/properties/prod, META-INF/app-config/spring and META-INF/app-config/spring/prod.

We can put as many configurator:include-properties and configurator:include-beans elements in the configuration file as we want.