Info | ||
---|---|---|
| ||
Spring is a lightweight container, providing centralized, automated configuration and wiring of your application objects, using a technique called "Dependency Injection" |
Spring integration is provided by a Struts Plugin.
...
The Spring Plugin works by overriding the Struts ObjectFactory to enhance the creation of core framework objects. When an object is to be created, it uses the class
attribute in the Struts configuration to correspond to the id
attribute in the Spring configuration. If not found, the class will try to be created as usual, then be autowired by Spring. In the case of Actions, Spring 2's bean scope feature can be used to scope an Action instance to the session, application, or a custom scope, providing advanced customization above the default per-request scoping.
Note | ||
---|---|---|
| ||
Remember: registering Actions with Spring is not required. The Spring alternative is there if you need it, but the framework will automatically create Actions objects from the action mappings. But, if you want to use Spring to inject your Actions, the option is there. |
Features
- Allow Actions, Interceptors, and Results to be created by Spring
- Struts-created objects can be autowired by Spring after creation
- Provides two interceptors that autowire actions, if not using the Spring ObjectFactory
Usage
To enable Spring integration, simply include struts2-spring-plugin-x-x-x.jar in your application.
If you are using more than one object factory, (for example, by including both the Spring and Plexus plugins in your application,) you will need to set the struts.objectFactory property in struts.properties or in one of several XML files via Constant Configuration:
Code Block | ||
---|---|---|
| ||
struts.objectFactory = spring |
Code Block | ||
---|---|---|
| ||
<struts>
<constant name="struts.objectFactory" value="spring" />
...
</struts>
|
Autowiring
The framework enables "autowiring" by default. (Autowiring means to look for objects defined in Spring with the same name as your object property). To change the wiring mode, modify the spring.autowire
property.
...
Tip | |||||||
---|---|---|---|---|---|---|---|
| |||||||
Since the Spring integration uses a standard Listener, it can be configured to support configuration files other than
See the Spring documentation for a full description of this parameter. |
Initializing Actions from Spring
Normally, in struts.xml
you specify the class for each Action. When using the default SpringObjectFactory, the framework will ask Spring to create the Action and wire up dependencies as specified by the default auto-wire behavior.
...
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans default-autowire="autodetect"> <bean id="bar" class="com.my.BarClass" singleton="false"/> ... </beans> |
How the code works
- The
id
attribute in the Spring configuration corresponds to the class attribute in the action configuration. - The
singleton
attribute is set tofalse
, meaning that Spring will create a new Action class upon each request, as Struts 1 would do.
...
title | Spring Actions are Optional! |
---|
...
To use session-scoped components with Spring and Struts, see the Spring Session Components Workarounds analysis.
Settings
The following settings can be customized. See the developer guide.
Setting | Description | Default | Possible Values |
---|---|---|---|
| The autowire strategy |
|
|
| Whether to have Spring use its class cache or not |
|
|
Installation
This plugin can be installed by copying the plugin jar into your application's /WEB-INF/lib
directory. No other files need to be copied or created.