通过 Struts提供的Spring插件集成Spring
2012-04-29 07:08
537 查看
Edit Page
Browse
Space
Add
Page
Add
News
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 theclass 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'sbean
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.
Struts-created objects can be autowired by Spring after creation
Provides two interceptors that autowire actions, if not using the Spring ObjectFactory
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 instruts.properties
or in one of several XML files via
Constant Configuration:
struts.properties
struts.xml
Wiring Mode
The autowire property can be set to several options.
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.
Configure the Spring listener
web.xml
Register your objects via the Spring configuration
applicationContext.xml
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 thestruts.xml to use the bean name defined in Spring instead of the class name.
Your struts.xml file would then have the Action class attributes changed.
struts.xml
Where you have a Spring bean defined in your applicationContext.xml named "bar". Note that thecom.acme.Foo Action did not need to be changed, because it can be autowired.
A typical spring configuration for bar could look as following.
applicationConext.xml
To use session-scoped components with Spring and Struts, see the
Spring Session Components Workarounds analysis.
Set "struts.devMode" to "true"
Set "struts.class.reloading.watchList" to a comma separated list of directories, or jar files (absolute or relative paths)
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 thatstruts.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:
developer guide.
Children
Show Children
Generated by Atlassian Confluence (Version: 3.4.9 Build: 2042 Feb 14, 2011)Auto Export Plugin (Version: 1.0.0-dkulp)
Browse
Space
Add
Page
Add
News
Spring is a lightweight container, providing centralized, automated configuration and wiring of your application objects, using a technique called "Dependency Injection" |
ObjectFactory to enhance the creation of core framework objects. When an object is to be created, it uses theclass 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'sbean
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.
Spring Actions are Optional! 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 SpringStruts-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 instruts.properties
or in one of several XML files via
Constant Configuration:
struts.properties
struts.objectFactory = org.apache.struts2.spring.StrutsSpringObjectFactory
struts.xml
<struts> <constant name="struts.objectFactory" value="org.apache.struts2.spring.StrutsSpringObjectFactory" /> ... </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 thespring.autowire property.Wiring Mode
struts.objectFactory.spring.autoWire = type
The autowire property can be set to several options.
name | Auto-wire by matching the name of the bean in Spring with the name of the property in your action.This is the default |
---|---|
type | 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 |
auto | Spring will attempt to auto-detect the best method for auto-wiring your action |
constructor | Spring will auto-wire the parameters of the bean's constructor |
Enabling Spring integration for other application objects is a two-step process.
Configure the Spring listener
web.xml
<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
Register your objects via the Spring configuration
applicationContext.xml
<?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="personManager" class="com.acme.PersonManager"/> ... </beans>
More applicationContext configuration files needed? Since the Spring integration uses a standard Listener, it can be configured to support configuration files other thanapplicationContext.xml. Adding the following to your web.xml will cause Spring's ApplicationContext to be inititalized from all files matching the given pattern: <!-- Context Configuration locations for Spring XML files --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext-*.xml,classpath*:applicationContext-*.xml</param-value> </context-param> 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.We strongly recommend that you find declarative ways of letting Spring know what to provide for your actions. This includes making your beans able to be autowired by either naming your dependent properties on your action the same as the bean defined in Spring which should be provided (to allow for name-based autowiring), or using autowire-by-type and only having one of the required type registered with Spring. It also can include using JDK5 annotations to declare transactional and security requirements rather than having to explicitly set up proxies in your Spring configuration. If you can find ways to let Spring know what it needs to do for your action without needing any explicit configuration in the SpringapplicationContext.xml, then you won't have to maintain this configuration in both places. |
bean in your Spring applicationContext.xml and then change the class attribute from your Action in thestruts.xml to use the bean name defined in Spring instead of the class name.
Your struts.xml file would then have the Action class attributes changed.
struts.xml
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <include file="struts-default.xml"/> <package name="default" extends="struts-default"> <action name="foo" class="com.acme.Foo"> <result>foo.ftl</result> </action> </package> <package name="secure" namespace="/secure" extends="default"> <action name="bar" class="bar"> <result>bar.ftl</result> </action> </package> </struts>
Where you have a Spring bean defined in your applicationContext.xml named "bar". Note that thecom.acme.Foo Action did not need to be changed, because it can be autowired.
A typical spring configuration for bar could look as following.
applicationConext.xml
<?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>
To use session-scoped components with Spring and Struts, see the
Spring Session Components Workarounds analysis.
Class Reloading
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:Set "struts.devMode" to "true"
Set "struts.class.reloading.watchList" to a comma separated list of directories, or jar files (absolute or relative paths)
Add this to web.xml:
<context-param> <param-name>contextClass</param-name> <param-value>org.apache.struts2.spring.ClassReloadingXMLWebApplicationContext</param-value> </context-param>
Add Apache Commons JCI FAM to the classpath. If you are using maven, add this to pom.xml
<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-jci-fam</artifactId> <version>1.0</version> </dependency>
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 thatstruts.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:
<constant name="struts.class.reloading.acceptClasses" value="com\.myproject\.example\.actions\..*" />
This feature is experimental, and should never be used in production systems. |
Settings
The following settings can be customized. See thedeveloper guide.
Setting | Description | Default | Possible Values |
---|---|---|---|
struts.objectFactory.spring.autoWire | The autowire strategy | name | name,type,auto, or constructor |
struts.objectFactory.spring.autoWire.alwaysRespect | Whether the autowire strategy should always be used, or if the framework should try to guess the best strategy based on the situation | false for backwards-compatibility | true or false |
struts.objectFactory.spring.useClassCache | Whether to have Spring use its class cache or not | true | true or false |
struts.class.reloading.watchList | List of jar files or directories to watch for changes | null | Comma separated list of absolute or relative paths to jars or directories |
struts.class.reloading.acceptClasses | List of regular expressions of accepted class names | null | 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) |
struts.class.reloading.reloadConfig | Reload the runtime configuration (action mappings, results etc) when a change is detected in one of the watched directories | false | true or false |
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.Children
Show Children
Generated by Atlassian Confluence (Version: 3.4.9 Build: 2042 Feb 14, 2011)Auto Export Plugin (Version: 1.0.0-dkulp)
相关文章推荐
- jqueryvalidate(猫冬插件)+spring3.0+struts2.18(集成开发)
- 使用eclipse开发struts+spring+hibernate免费插件套装
- Spring集成struts (1)
- 温习java:Struts与Spring集成
- Spring_36_集成 Struts2
- netbeans6.1环境下struts2+spring2.5+hibernate3.2集成
- spring+struts的集成(第二种集成方案,用得最多的方案)
- Mybatis3.2.1使用例二:Mapper方式集成Spring、注解提供SQL
- 深入探讨 Spring 与 Struts 的集成方案
- struts1.2 + hibernate3.2 + Spring2.5集成配置
- 深入探讨Spring与Struts的集成方案(转)
- Spring 4与Struts 2项目基于XML的集成实战
- SpringBoot集成MyBatis的分页插件PageHelper
- (转)Spring4.2.5+Hibernate4.3.11+Struts1.3.8集成方案一
- spring3.1集成struts2.1.6配置文件集成
- Struts与Spring集成方式(一)
- spring、struts、mybatis集成WARN [com.opensymphony.xwork2.util.FileManager] (MSC service thread 1-1) Cou
- Spring集成Swagger2,提供RestFul API
- osgi与流行的框架(spring,struts,hibernate等)的集成
- spring boot集成开源分页插件pagehelper