Sunday, March 15, 2009

Adding Spring to the project

Spring is generally highly rated for it's IoC capabilities. It gained it's popularity while developers had hard times with EJB 2.0 Specs.

Since Spring can be needed in more sub-project or modules, it is only reasonable to create a central place in your project to implement your Spring code. Create a Maven module as described in the revious post. It should look similar to this:


The Spring configuration files should be placed inside of the src/main/resources folder.

The main Spring configuration file is named applicationContext.xml by default. How ever this is not mandatory. You can name it hwat ever you like. I called it spring-context.xml.

If you are using the Sprng for web app or portlets, you can tell Spring where to find your custom configuration file with the following entry in your web.xml descriptor:


Now that you have the basic structure for your Spring, you should add the Module in to the main Project pom.xml :


and add it as dependency to be available in other projects:
Now that the Spring module is configured and available as dependency for other modules, you can bind it in other modules by adding the dependency in their pom.xml as follows:

As you can notice, the dependency in other modules pom.xml is added without the version tag. In this way, the versioning of the project dependency's is handled central in the main project pom.xml.