<< Using Maven with IntelliJ IDEA 7.0 | Home | Java on the iPhone? >>

Modular Spring Configuration

Automatically loading Spring configuration snippets from JAR files

When assembling applications from a set of reusable compoenents that use the Spring Framework to wire their beans you will end up with a whole bunch of Spring configuration snippets spread around the JAR files containing your components.

Components should be self-contained so everything belonging to the component (including configuration) should be part of it. Ideally assembling the application would just mean dropping an additional component JAR file into the classpath (i.e. adding dependency to its Maven POM) and using the beans it exposes.

A simple convention makes this quite easy: Just put a spring.xml file into the META-INF directory of your components and declare the application context in the application's web.xml like this:

<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>
    classpath*:META-INF/spring.xml
    /WEB-INF/applicationContext.xml
  </param-value>
</context-param>

The result: For each component the contained spring.xml is added to the application context and any beans defined therein are automatically created on deployment.

To make sure no name clashes occur you should establish a simple naming conventions for the id attribute of your beans like component.beanname. Resources defined in the main application context that the components require like Hibernate sessions will be named core.beanname.
For example:

<bean id="pbx.pbxDao" class="com.reucon.astis.dao.hibernate.PbxHibernateDao">
  <property name="sessionFactory" ref="core.sessionFactory"/>
</bean>
Tags :


Re: Modular Spring Configuration

Nice idea! In my case modules build on each other. How can you refer one spring configuration from another one when they are packed in jars/wars and ears and deployed on an application server?

Re: Modular Spring Configuration

Dot (.) is prohibited in XML ID tag. Also dot character is overused.
I think _ here is better.

i.e.: component_beanName

Add a comment Send a TrackBack