Spring集成struts2
根据Spring官方文档介绍,使用Struts的Spring Plugin 插件进行对Spring 集成
http://struts.apache.org/docs/spring-plugin.html
struts.objectFactory = spring |
<struts> <constant name= "struts.objectFactory" value= "spring" /> ... </struts> |
struts.objectFactory.spring.autoWire = type |
- 配置spring监听器
< listener > < listener-class >org.springframework.web.context.ContextLoaderListener</ listener-class > </ listener > |
- 在spring配置文件中注册对象
<? 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" scope = "prototype" /> ... </ beans > |
<!-- 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
>
struts.xml
文件 Action 的class 属性需要做变更.
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" < 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 > |
当你有一个在applicationContext.xml
中定义的名叫"bar"的 Spring bean。 注意 com.acme.Foo
Action 不需要改变,因为它可以被自动装载。
下面是一个对应的spring配置文件,在这里bar这个bean可以被查找到。
<? 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.
2.3 重新装载类
Spring插件能够被配置成可以自动重新装载被修改的java类,这个功能主要被用来实现“热部署”机制,即可以不重启整个容器就能让java的修改生效(通常用于开发阶段)
- 设置常量"struts.devMode" 为"true" <constant name="struts.devMode" value="true"></constant>
- 设置"struts.class.reloading.watchList" 值为逗号分隔的目录,jar文件列表,绝对或相对路径都可以
-
web.xml中添加配置:
<context-param>
<param-name>contextClass</param-name>
<param-value>org.apache.struts2.spring.ClassReloadingXMLWebApplicationContext</param-value>
</context-param>
-
添加这个maven依赖(或直接增加jar包 commons-jci-fam-1.1.jar)
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-jci-fam</artifactId>
<version>
1.0
</version>
</dependency>
如果让所有类都使用重新加载机制,会可能导致ClassCastException异常,因为有多个类加载器装载相同的类无法互相分配,使用下面这个组件,来限制使用重新装载的类:例如只开action的重新装载
<constant name= "struts.class.reloading.acceptClasses" value= "com.myproject.example.actions..*" /> |
这个功能是实验性的,不要在生产环境使用
2.4其他相关配置
The following settings can be customized. See the developer guide.
Setting |
Description |
Default |
Possible Values |
---|---|---|---|
|
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, it uses an old approach to create a bean, switch this flag if you have problems with Spring beans and AOP |
|
|
2.5 安装
安装这个插件,只需直接把jar包复制到应用的 /WEB-INF/lib
目录下