Tuesday, January 03, 2012

Spring configuration: applicationContext

When I started working with Spring framework, I immediately recognized the value and simplicity of using such framework to develop and maintain J2EE applications. It is collection of best practices for coding, great design patterns implementation (some of them), and fantastic guideline for putting enterprise level applications together. However, starting with Spring can sometime be overwhelming experience, so I would like to create a few posts that would make this job easier. This is just one of the templates that works for me, but with Spring you have infinite number of possibilities on how to integrate and develop you code.

More about Spring framework can be found at Spring documentation. Of course, Springsource (and community) have created many other additions and standalone products that fit well with Spring. Please feel free to explore this product.

File included in this post would be the first applicationContext.xml file that will be loaded from the web.xml.

<?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:aop="http://www.springframework.org/schema/aop"
    xmlns:ctx="http://www.springframework.org/schema/context" xmlns:sws="http://www.springframework.org/schema/web-services"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/web-services 
    http://www.springframework.org/schema/web-services/web-services-2.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <!-- enable configuration with annotations -->
    <context:annotation-config />

    <!-- we want to use AspectJ proxy (Spring proxy is limited) -->
    <aop:aspectj-autoproxy />

    <!-- scan for all annotations in this package -->
    <ctx:component-scan base-package="my.package.structure" />

    <!-- load log4j configuration -->
    <!-- custom properties to get Properties when crating them -->
    <!-- should have private Properties customProperties, and should be set when loading log4j --> 
    <bean id="log4jPropertyConfigurer"
        class="my.package.structure.properties.CustomPropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:log4j.properties</value>
            </list>
        </property>
        <property name="order" value="0"></property>
        <property name="ignoreUnresolvablePlaceholders" value="true" />
    </bean>

    <!--configure log4j -->
    <!-- call this in your constructor to init log4j: org.apache.log4j.PropertyConfigurator.configure(properties.getCustomPropeties()) -->
    <bean id="applicationLog4j" class="my.package.structure.properties.log4j.Log4jPropertyConfigurer">
        <constructor-arg index="0" ref="log4jPropertyConfigurer" />
    </bean>        

    <!-- load additional properties -->
    <bean id="propertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:application.properties</value>
                <value>classpath:jdbc.properties</value>
                <value>classpath:messages.properties</value>
                <!-- This should be loaded last -->
                <value>file:override.properties</value>
            </list>
        </property>
        <property name="ignoreUnresolvablePlaceholders" value="true" />
    </bean> 

    <bean id="messageSource"
        class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basenames">
            <list>
                <value>messages</value>
                <value>messages_en_US</value>
            </list>
        </property>
    </bean>

    <!-- import hibernate or any other Spring xml -->
    <import resource="applicationContext-hibernate.xml" />  

</beans>

No comments:

Post a Comment