Struts 2 Spring Integration With the Spring Plugin Guide
Integrate Struts 2 with Spring using the struts2-spring-plugin: let Spring create actions, inject services by name or type, and handle scopes and AOP.

Struts 2 builds its own objects, and the Spring plugin swaps that factory for the Spring container, so actions can use injected services, transactions and configuration. This guide uses Struts 6 on Jakarta-compatible servlet containers where noted, and Spring Framework 6. The Struts basics are in the Struts 2 tutorial.
Dependencies
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-spring-plugin</artifactId>
<version>${struts.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>Use the plugin version that matches your Struts version, and a Spring version it supports. Mixing a Struts line on the javax namespace with a Spring line on jakarta is a classic cause of startup failures, similar to the one in javax to jakarta errors.
web.xml
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>The listener starts Spring before the Struts filter, so the context exists when the first action is created. In Struts 6.x the filter class lives in org.apache.struts2.dispatcher.filter, as above, and not in the old ng package.
Spring beans and the action
<!-- applicationContext.xml -->
<beans ...>
<context:component-scan base-package="dev.javahacks.shop.service" />
</beans>@Service("orderService")
public class OrderService {
public List<Order> findOpen() { ... }
}public class OrderListAction extends ActionSupport {
private OrderService orderService; // injected by name
private List<Order> orders;
public void setOrderService(OrderService orderService) { this.orderService = orderService; }
@Override
public String execute() {
orders = orderService.findOpen();
return SUCCESS;
}
public List<Order> getOrders() { return orders; }
}By default the plugin autowires by name: the setter setOrderService receives the bean named orderService. You can change the strategy with the constant struts.objectFactory.spring.autoWire (name, type, auto or constructor). Autowiring by type is convenient and breaks when two beans share a type, as in NoUniqueBeanDefinitionException.
struts.xml
<struts>
<constant name="struts.objectFactory" value="spring" />
<package name="shop" extends="struts-default">
<action name="orders" class="dev.javahacks.shop.web.OrderListAction">
<result>/WEB-INF/jsp/orders.jsp</result>
</action>
</package>
</struts>The class attribute can name a class, in which case Spring builds it with autowiring, or the id of a Spring bean, in which case you control the scope and dependencies in Spring configuration. For the second, declare it:
<bean id="orderListAction" class="dev.javahacks.shop.web.OrderListAction" scope="prototype" />Scope: prototype for actions
Struts creates a new action for each request and stores request data in its fields. A singleton action shared by all threads would mix up users' data, a thread-safety bug of the kind in bean scopes. Always use prototype scope, which the plugin does when it creates the class by name.
Transactions
Put @Transactional on services, not on actions, and enable annotation-driven transactions in Spring configuration. A service called from an action then runs in a transaction. Keep the boundaries away from the web layer, as discussed in self-called transactional methods.
How the plugin wires things
With the Spring plugin on the classpath and struts.objectFactory set to spring, Struts asks Spring for every object it needs, including action instances. The class attribute in struts.xml can name a Spring bean id, in which case Spring supplies a configured instance, or a class name, in which case Struts creates the action and Spring autowires its properties. Autowiring by name or by type is controlled by struts.objectFactory.spring.autoWire. Knowing which of these is in play explains the errors, because No bean named 'x' is defined means Struts looked up an id, and a null service field means the action was created without autowiring.
The singleton trap
A Spring bean is a singleton unless told otherwise, and a Struts action is created per request and holds the request's state in its fields. If you register an action as a singleton, every request shares the same fields, and two users see each other's data. Declare action beans with scope="prototype" in the Spring configuration, or let Struts create them and autowire. Services are the opposite, and should stay singletons. A symptom of the mistake is a form that shows another user's input under load and behaves perfectly in a single-user test.
Transactions and where they belong
Put @Transactional on the service layer, and not on the action. The action is a web adapter and should call one service method per user operation, so that the transaction covers the whole operation. A transaction on the action would stay open while the view renders, which holds a connection for the length of the page. Check that the service is a Spring bean called through its proxy, since a service created with new inside the action does not get a transaction.
Testing the wiring
Write one integration test that loads the real Spring context and the Struts configuration, and executes an action through the Struts test support, asserting on the result name and a model property. It catches a bad bean name, a missing property and a scope mistake, which are the failures that a unit test with a mock cannot see. Keep the test small, and run it on every build.
Upgrading Struts and Spring together
Both projects have had security releases that matter, and the plugin must match the Struts version. When you upgrade Spring, check the plugin's documentation for the supported range, and run the integration test first. Mismatched versions fail at startup with a class loading error, and the fix is to align them, not to add exclusions.
Diagnosing a null service
The first error most people see is a NullPointerException on a service field of the action. Walk through three checks. Is the Spring plugin jar on the classpath, and does it match the Struts version? Does the ContextLoaderListener start, which you can see in the startup log as the root context being initialized? And does the action have a setter, or is the field annotated, so that autowiring has something to call? A missing listener produces the null silently, because the plugin has no context to ask. Add a log line in a @PostConstruct method of the service to confirm that Spring created it at all.
Interceptors and the proxy
If a service is wrapped in a Spring proxy for transactions, the action receives the proxy, and calling a method through it works as expected. If the service class is final, or a method is private, the proxy cannot intercept it and the transaction silently does not start. The failure appears later, as data that is not saved or a lazy exception, so check for it whenever a service method does more than one write. Keep services as plain non-final classes with public methods for the entry points.
Where this does not apply
A new application should not start on Struts 2. For existing ones, the path out is in migrating to Spring MVC. Security hardening is covered in Struts 2 security. The reference is the Struts Spring plugin page, and Struts documentation covers the framework.


