Here’s a tutorial to show how to access beans declared in the Spring Ioc container in a web application developed with Apache Struts 1.x.

Download this Struts 1.x + Spring example –

Spring comes with “Struts-specific” solution for access beans declared in the Spring Ioc container.

  1. Register a Spring’s ready-make Struts plug-in in the Struts configuration file.
  2. Change your Struts action class to extend the Spring’s ActionSupport class, a subclass of the Struts Action class.
  3. The ActionSupport provide a convenient getWebApplicationContext() method for you to access beans declared in Spring Ioc container.

1. Struts + Spring dependencies

To integrate with Struts 1.x, Spring is required the “spring-web.jar” and “spring-struts.jar” libraries. You can download it from Spring web site or Maven.

        <!-- Spring framework --> 

2. Register Struts plug-in

In your Struts configuration file (struts-config.xml), register the Spring’s ready-make Struts plug-in – “ ContextLoaderPlugIn“.


    <!-- Spring Struts plugin -->
 	<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
		<set-property property="contextConfigLocation"
			value="/WEB-INF/classes/SpringBeans.xml" />

The “ContextLoaderPlugIn” will handle all the integration work between Struts and Spring. You can load your Spring’s bean xml file into the “contextConfigLocation” property.


<beans xmlns=""
	<!-- Beans Declaration -->
	<import resource="com/mkyong/customer/spring/CustomerBean.xml"/>

3. Spring’s ActionSupport

In Struts Action class, extends the Spring “ActionSupport” class, and get the Spring’s bean via “getWebApplicationContext()” method.


   	<bean id="customerBo" class="" >
   		<property name="customerDao" ref="customerDao" />

Struts Action

package com.mkyong.customer.action;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.web.struts.ActionSupport;

import com.mkyong.customer.model.Customer;
public class ListCustomerAction extends ActionSupport{
  public ActionForward execute(ActionMapping mapping,ActionForm form,
	HttpServletRequest request,HttpServletResponse response) 
        throws Exception {
	CustomerBo customerBo =
		(CustomerBo) getWebApplicationContext().getBean("customerBo");
	return mapping.findForward("success");