通过 Struts提供的Spring插件集成Spring
来源:互联网 发布:淘宝刷单怎么找客户 编辑:程序博客网 时间:2024/04/26 07:27
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 theid 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.
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 instruts.properties or in one of several XML files via Constant Configuration:
struts.objectFactory = org.apache.struts2.spring.StrutsSpringObjectFactory
<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.
struts.objectFactory.spring.autoWire = type
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
<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>
- Register your objects via the Spring configuration
<?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>
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.
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.
<!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.
<?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\..*" />
Settings
The following settings can be customized. See the developer guide.
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.
- 通过 Struts提供的Spring插件集成Spring
- spring+struts的集成
- spring+struts的集成
- spring和struts的集成
- spring和struts的集成
- Spring集成Struts的方法
- Struts与Spring的集成
- Spring集成Struts的方法
- Spring与Struts的集成
- 不错的struts插件:Struts Spring Plugin
- 不错的struts插件:Struts Spring Plugin
- 不错的struts插件:Struts Spring Plugin
- 【Spring】Struts和Spring集成
- Spring集成struts
- Spring集成struts (1)
- Spring集成struts(2)
- Spring集成struts(3)
- spring struts hibernate 集成
- 推荐不知道歌名的曲子,但知道是中岛爱的歌
- 关于MySQL的ODBC驱动兼容性问题
- js中showModalDialog和returnValue的用法
- 解析LINUX的passwd文件
- 根据分隔符将一个长字符串分割保存到动态数组中
- 通过 Struts提供的Spring插件集成Spring
- 开机动画(闪动的ANDROID字样的动画图片)
- ExtJS 操作DOM元素Ext.Element
- JSP中动态INCLUDE与静态INCLUDE,forward与sendredirect的区别
- 判断资源存在与否如判断一个drawable的图片存在及通过id加载图片
- 根据分隔符将一个长字符串分割保存到动态数组中(第二种方法)
- 数据流图懂不懂?
- 重温经典 ——《计算机网络 第五版》读书随想
- 通过PID获取进程名