|Home > Guides > Plugin Developers Guide > Spring 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.
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:
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
autowire property can be set to several options.
Auto-wire by matching the name of the bean in Spring with the name of the property in your action. This is the default
Auto-wire by looking for a bean registered with Spring of the same type as the property in your action. This requires you to have only one bean of this type registered with Spring
Spring will attempt to auto-detect the best method for auto-wiring your action
Spring will auto-wire the parameters of the bean's constructor
Turn off externally defined autowiring. Annotation-driven injection and injection based on Springs *Aware-interfaces still applies
By default, the framework will at least try to use Spring to create all its objects. If the object cannot be created by Spring, then the framework will create the object itself.
Enabling Spring integration for other application objects is a two-step process.
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.
However, sometimes you might want the bean to be completely managed by Spring. This is useful, for example, if you wish to apply more complex AOP or Spring-enabled technologies, such as Acegi, to your beans. To do this, all you have to do is configure the bean in your Spring
applicationContext.xml and then change the class attribute from your Action in the
struts.xml to use the bean name defined in Spring instead of the class name.
struts.xml file would then have the Action class attributes changed.
Where you have a Spring bean defined in your
applicationContext.xml named "bar". Note that the
com.acme.Foo Action did not need to be changed, because it can be autowired.
A typical spring configuration for bar could look as following.
To use session-scoped components with Spring and Struts, see the Spring Session Components Workarounds analysis.
The Spring plugin can be configured to automatically reload classes that change in the file system. This feature will enable code changes to be "hot deployed" without having to restart the web container. To enable this feature follow these steps:
Add this to web.xml:
Add Apache Commons JCI FAM to the classpath. If you are using maven, add this to pom.xml
Letting the reloading class loader handle all the classes can lead to ClassCastException(s) because instances of the same classes loaded by different class loaders can not be assigned to each other. To prevent this problem we suggest that
struts.class.reloading.acceptClasses is used to limit the classes loaded by the reloading class loader, so only actions are handled by it. This constant supports a comma separated list of regular expressions:
The following settings can be customized. See the developer guide.
The autowire strategy
Whether the autowire strategy should always be used, or if the framework should try to guess the best strategy based on the situation
Whether to have Spring use its class cache or not
List of jar files or directories to watch for changes
Comma separated list of absolute or relative paths to jars or directories
List of regular expressions of accepted class names
Comma separated list of regular expressions of classes that will be loaded by the reloading class loader(we suggest to add regular expressions so only action classes are handled by the reloading class loader)
Reload the runtime configuration (action mappings, results etc) when a change is detected in one of the watched directories
Uses different logic to construct beans to allow support AOP
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.