Wednesday, 26 January 2011

Upgrading to Spring 3

"It’s really about time I learnt all about Spring 3.0" I thought today. I’ve actually been putting it off for some time, after all Spring 2.5.x has been working well, without any problems, so putting it off seemed a good idea – as the old saying goes "if it ain’t broke don’t fix it".

This is a quick blog that provides a few pointers about converting your existing projects and providing a few links to some of the more detailed information.


Useful Resources

Two useful resources I’ve found are:

The Spring Upgrade Guide and the Spring Reference Documentation.

Maven Things


There are a couple of points to note about the release of Spring 3. Firstly, they’ve provided a Spring release strategy that more closely fits into the Maven build philosophy. This means ditching the colossal spring.jar file and splitting the code down into a number of smaller modules. A list of these modules is available in both the upgrade guide and the reference manual.

When updating your Maven pom files, if you haven’t done so already, you’ll need to remove references to:
<groupId>org.springframework</groupId>
        <artifactId>spring</artifactId>        
 
You’ll then need to add in the new Spring Maven modules and set their version to: ‘3.0.5.RELEASE

You must also ensure that Spring 2 is not on the classpath; this can be difficult if you’re using a super-pom hierarchy over which you have little control. If Spring 2 is accidentally left on the classpath, then you’ll see on initialization MalformedParameterizedTypeException occurring. You can ensure that Spring 2 is not on the classpath by adding the following dependency exclusion:

<dependency>
              <groupId>my.project.group.id</groupId>
              <artifactId>my.artifact.id</artifactId>
              <type>jar</type>
              <version>3.1</version>
              <exclusions>
                     <exclusion>    <!-- we're using newer Spring 3 -->
                           <groupId>org.springframework</groupId>
                           <artifactId>spring</artifactId>        
                     </exclusion>
              </exclusions>       
        </dependency>        

Schemas

You can update your XML configuration files to use Spring 3 schemas (XSD); however, the old Spring 2 schemas and will still work.

Version 3 'Basic' Schema inclusion

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
                http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
... of course you need to add in further schema locations as necessary...

Generics

Most migration issues arise from Spring 2 not using Java Generics which gives rise to numerous compilation errors unless a Type is specified.

No comments: