Adding Spring Dependancy Injection to Struts2 Project

Last modified by Ken McWilliams on 2012/03/11 06:21

This will add Spring for the purpose of dependancy injection(DI) to our project. We will create a simple example showing DI and we will bring spring up to the latest version (as of this writing 3.1.1 however this method should apply for the near future). 

This assumes that we are starting with a basic strusts2 application.

1) Add the struts2-spring-plugin

The maven coordinates are:

Artifact Id: struts2-spring-plugin

Group Id: org.apache.struts

Version: (or whatever version of struts2 you are using)

2) Configure web.xml and add applicationContext.xml to our project.

web.xml will need to look like the following:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="" xmlns:xsi="" xsi:schemaLocation="">

Add applicationContext.xml to src/main/resources (same place log4j.xml)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""
    <context:component-scan base-package="com.yourProjectBase" />


Note the above contains more xml name spaces and schemas than nessasary for simple DI however it sets us up nicely for adding Java Persistene API in the next integration tutorial. 

Beyond the name spaces and schemas there are three things happening:

1) context:annotation-config is used to activate annotations in beans already registered in the application context. To better understand this and to learn how to avoid using xml to register beans altogether please read this very nice answer on stack overflow: Difference between <context:annotation-config/> vs <context:component-scan/>

2) Was also explained in the previous link.  It allows us to define beans outside of the applicationContext via annotations, for that we supply the package(s) which it will scan. Again the above link is very useful in understanding this.

Note: We will add our beans on the blank line preceding </beans>

3) [optional] Upgrade Spring to Latest Version

After adding the struts2-spring-plugin you'll see that the following jars were added to support it:







In netbeans 7.1, it is as simple as right clicking the dependancy node and adding the most current dependancy for each of the above a list of availible versions should be provided and you simply select the most current one.

You should be able to determine the coordinates:

Groupd Id for all the above is: org.springframework

Artifact Id for all the above is the first two words including the hypen, for example spring-aop-3.0.5.RELEASE.jar is spring-aop, then as previously stated pick the most recent version. 

4) [optional] Basic DI example

The following assumes the use of the conventions plugin.  For explanation of this plugin see: using the convention plugin.

Our example will require the following things: 1) A service, 2) an implementation of the service, 3) configure spring to inject our service where we need it, 4) an action to use the service and 5) a view.

Create Service

package com.example.project.service;

public interface Test {
    String doTest();

Create Service Implementation

package com.example.project.impl;
import com.example.project.service.Test;

public class TestImpl implements Test{
    public String doTest() {
        return "From test.";

Configure Spring to Inject the Service

In applicationContext.xml add the following on the line before </beans>:

<bean id="test" class="com.example.project.impl.TestImpl"/>

Create an Action to use the Bean

package com.example.project.action.test;

import com.example.project.service.Test;
import com.opensymphony.xwork2.ActionSupport;
import org.springframework.beans.factory.annotation.Autowired;

public class SpringTest extends ActionSupport{
    @Autowired Test test;
    public String output;
    public String execute(){
        output = test.doTest();
        return SUCCESS;

The View

Create the JSP /WEB-INF/content/test/spring-test.jsp

<%@taglib prefix="s" uri="/struts-tags"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Spring Test</title>
        <h1>Spring Test</h1>
        Value from spring bean: <s:property value="output"/>

Run the example... and append test/spring-test to the end of your application path and you should see the result.

Upgrade Spring to the Latest Version 3.1.1