Struts 2 Spring Plugin - Excluding Its Transitive Dependencies To Use A Newer Version Of Spring

Introduction

When using the Struts 2 Spring plugin in your application and using Maven to manage your dependencies, Maven will include the plugin's transitive dependencies. The Struts 2 Spring plugin (version 2.2.1) has transitive dependencies on several Spring jar files, version 2.5.6 (this is as of October 2010). If you want to use the current version of those Spring jar files, then you should exclude the Struts 2 Spring plugin's transitive dependencies and explicitly include dependencies on the current Spring jar files. This article demonstrates how to use the current Spring jar files and the Struts 2 Spring plugin when using Maven to manage your artifacts.

Struts 2 Spring Plugin Transitive Dependencies

If you examine the Struts 2 Spring plugin's (version 2.2.1) pom.xml you'll find these transitive dependencies.


<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${struts2.springPlatformVersion}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${struts2.springPlatformVersion}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${struts2.springPlatformVersion}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${struts2.springPlatformVersion}</version>
</dependency>

...rest of dependencies omitted...



</dependencies>

Exclude Struts 2 Spring Plugin Transitive Dependencies

In one of the parent pom.xml files for Struts 2, the property ${struts2.springPlatformVersion} is defined as 2.5.6. So Maven will include in your application those Spring jar files named above version 2.5.6. If you want to use a newer version of the Spring jar files, you should exclude the Struts 2 Spring plugin's transitive dependencies in your application's pom.xml.



<dependency>
    <groupId>org.apache.struts</groupId>
    <artifactId>struts2-spring-plugin</artifactId>
    <version>2.2.1</version>
    <exclusions>
        <exclusion>
            <artifactId>spring-web</artifactId>
            <groupId>org.springframework</groupId>
        </exclusion>
        <exclusion>
            <artifactId>spring-beans</artifactId>
            <groupId>org.springframework</groupId>
        </exclusion>
        <exclusion>
            <artifactId>spring-context</artifactId>
            <groupId>org.springframework</groupId>
        </exclusion>
        <exclusion>
            <artifactId>spring-core</artifactId>
            <groupId>org.springframework</groupId>
        </exclusion>
    </exclusions>
</dependency>

Explicitly State Spring Dependency

Next you need to specify dependency nodes for the version of the Spring jars you want to use in your application instead of the above Spring artifacts.



<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>3.0.4.RELEASE</version>
</dependency>

All you need to do is include a dependency for the Spring web jar. The pom.xml for the Spring web jar includes transitive dependencies on the other Spring jar files needed to work with the Struts 2 Spring plugin.

Summary

The Struts 2 framework provides a Spring plugin that enables Spring and Struts 2 to work together to manage dependencies between ActionSupport classes and other classes (e.g. Service, DAO). If you use Maven to manage your application's artifacts, then Maven will by default include the Struts 2 Spring plugin's transitive dependencies on an older version of Spring. If you want to use a newer version of Spring then you must exclude the plugin's transitive dependencies and explicitly include a dependency on the version of Spring you want to use.

Related Blog Entries

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
This article solved the problem I've been having for days now. I kept getting a MalformedParameterizedTypeException whenever Spring attempting to instantiate a Hibernate sessionFactory. Turns out it was in fact due to this precise problem - the struts2-spring plugin was causing Maven to suck in all the Spring2 stuff, which hosed the webapp.

Point being, thanks for the good article. I'd take stuff like this over a million worthless roseindia's.
# Posted By Ragnar | 4/13/11 11:25 AM
BlogCFC was created by Raymond Camden. This blog is running version 5.9.1.002. Contact Blog Owner