Spring学习6-Spring整合Struts2
一、Spring为什么要整合Struts2
Struts2与Spring进行整合的根本目的就是要让
Spring为Struts2的Action注入所需的资源对象,它们整合的原理则是只要导入了struts2的spring插件包,不需要进行
任何额外的配置,Struts2的Action实例对象将由struts2的spring插件创建,该插件会按照Action的属性名称自动从
spring容器中查找相同名称的bean对象对Action进行装配.
显然,Struts2的spring插件需要获得spring容器对象,这个spring容器对象是谁创建出来?Struts2的Spring插件又是如何获得Spring容器的呢?在web开发方面,Spring除了提供自己的spring
mvc这个web框架之外,它也为web开发提供了最基本的支持,以便在不使用spring
mvc的web环境下使用spring,这就是通过
org.springframework.web.context.ContextLoaderListener创建出
WebApplicationContext容器对象,
并将创建出来的WebApplicationContext对象存储进了Web应用程序的application作用域中,存储时的key为
WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE这个常量表示的字符串,以后在Web应用程序中就可以使用
application.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE)
语句, 或Spring提供的工具方法
WebApplicationContextUtils.getWebApplicationContext(application)
来获得
spring容器对象。ContextLoaderListener使用contextConfigLocation这个ServletContext初
始化参数来指定WebApplicationContext容器对象的配置文件,如果没有配置contextConfigLocation这个
ServletContext的初始化参数,ContextLoaderListener则默认使用/WEB-INF
/applicationContext.xml作为配置文件。
为了简便,我们这里采用默认的配置文件,所以,不需要配置contextConfigLocation这个ServletContext的初始化参数,只
需要保证spring配置文件的路径和名称为/WEB-INF/applicationContext.xml即可。
二、整合步骤
1、导入struts2的8大核心jar包
struts2-core-2.1.6.jar
xwork-2.1.2.jar
ognl-2.6.11.jar
freemarker-2.3.13.jar
commons-logging-1.1.jar
commons-fileupload-1.2.1.jar commons-io-2.0.1.jar
javassist-3.11.0.GA.jar
说明:strut2的8个核心jar包是使用strut2必须导入的最少jar包
2、将struts.xml文件拷贝到src目录下,并配置web.xml
3、导入Spring的核心jar包
spring-core-3.2.0.M2.jar
spring的核心jar包
commons-logging.jar
4、初始化Spring容器,有如下两种方式:
①利用ContextLoaderListener类,在Web.xml文件中添加:
<!– Spring配置信息加载监听器 Context
Listener,
必要-->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<!-- Spring
防止内存溢出,原因详见:博文底-->
<listener>
<listener-class>
org.springframework.web.util.IntrospectorCleanupListener
</listener-class>
</listener>
该类会在Web启动时自动查找WEB-INF下的applicationContext.xml配置,并且根据该文件来创建Spring容器。当然我们也可以自定义applicationContext.xml的位置,需在web.xml子节点<web-app
>的顶端添加:
<context-param>
<param-name>contextConfigLocation</param-name>
<!-- 此位置相对于:部署后的项目的类路径
。多个文件逗号分隔(因这里用到了Spring-Security)-->
<param-value>
classpath:applicationContext.xml,classpath:spring\applicationContext-security.xml
</param-value>
<!-- 此位置相对于:部署后的项目的根路径
。多个文件逗号分隔(因这里用到了quartz定时任务框架)-->
<param-value>/WEB-INF/applicationContext.xml,/WEB-INF/quartz.xml</param-value>
</context-param>
②如果Web服务器不支持Servlet2.3以上的规范,即不支持Listener利用load-on-startup
Servlet创建ApplicationContext,在Web.xml文件中添加:
<servlet>
<servlet-name>context</servlet-name>
<servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
设置该Servlet随Web服务器的启动而启动,load-on-startup的值越小越早调用。但不管该值多小,Listener都比该Servlet更早启动。
因为ContextLoaderServlet与COntextLoaderListener底层都依赖于ContextLoader,因此二者的效果几
乎没有区别。Listener总比所有的Servlet更优先加载,因此,采用ContextLoaderListener创建
ApplicationContext的时机更早。
5、添加spring与struts2整合的插件
struts2-spring-plugin-2.1.6.jar(它在strut2框架的jar包中)
千万注意:而不是导入spring框架中的spring-struts-3.2.0.M2.jar,否则在strut.xml中无法使用Spring中Bean工厂的Bean了!!!
.jar
的Struts插件,该插件会把Struts自带的ObjectFactory替换为com/opensymphony/xwork2/spring
/SpringObjectFactory,这样所有Action的实例化工作就交给你spring框架,所有的Action既是struts的
action,同时又是spring的bean。在struts的配置中,action的type就不再设置为类型路径,而是bean名称。
至此整合步骤已完成!!!现在让我们来比较一下整合前后的区别:
整合之前的struts:
<package name="test" extends="strutsTest"
namespace="/">
<action name="ActionTest" class="com.test.action.ActionTestAction">
<result
name="input">/ActionTest.jsp</result>
<result name="success"
type="dispatcher">/ActionTest.jsp</result>
</action>
</package>
整合之后的struts:
<package name="test" extends="strutsTest"
namespace="/">
<action name="ActionTest" class="actionTestAction">
<result
name="input">/ActionTest.jsp</result>
<result name="success"
type="dispatcher">/ActionTest.jsp</result>
</action>
</package>
这里的class属性actionTestAction就是bean的名称:
<bean id="actionTestAction"
class="com.test.action.ActionTestAction"></bean>
#########################################################
为什么要在web.xml添加如下代码来防止内存泄露呢:
<listener>
<listener-class>
org.springframework.web.util.IntrospectorCleanupListener
</listener-class>
</listener>
spring的说明如下:
Listener that flushes the JDK's JavaBeans Introspector cache on web
app shutdown. Register this listener in your web.xml to guarantee
proper release of the web application class loader and its loaded
classes.
If the JavaBeans Introspector has been used to analyze application
classes, the system-level Introspector cache will hold a hard
reference to those classes. Consequently, those classes and the web
application class loader will not be garbage-collected on web app
shutdown! This listener performs proper cleanup, to allow for
garbage collection to take effect.
Unfortunately, the only way to clean up the Introspector is to
flush the entire cache, as there is no way to specifically
determine the application's classes referenced there. This will
remove cached introspection results for all other applications in
the server too.
Note that this listener is not necessary when using Spring's beans
infrastructure within the application, as Spring's own
introspection results cache will immediately flush an analyzed
class from the JavaBeans Introspector cache and only hold a cache
within the application's own ClassLoader. Although Spring itself
does not create JDK Introspector leaks, note that this listener
should nevertheless be used in scenarios where the Spring framework
classes themselves reside in a 'common' ClassLoader (such as the
system ClassLoader). In such a scenario, this listener will
properly clean up Spring's introspection cache.
Application classes hardly ever need to use the JavaBeans
Introspector directly, so are normally not the cause of
Introspector resource leaks. Rather, many libraries and frameworks
do not clean up the Introspector: e.g. Struts and Quartz.
Note that a single such Introspector leak will cause the entire web
app class loader to not get garbage collected! This has the
consequence that you will see all the application's static class
resources (like singletons) around after web app shutdown, which is
not the fault of those classes!
This listener should be registered as the first one in web.xml,
before any application listeners such as Spring's
ContextLoaderListener. This allows the listener to take full effect
at the right time of the lifecycle.
其中JavaBeans
Introspector是一个类,位置在Java.bean.Introspector,这个类的用途是发现java类是否符合javaBean规范,也就是这个类是不是javabean。具体用法可以参照jdk文档;
上面的意思就是,如果有的框架或者程序用到了JavaBeans
Introspector了,那么就启用了一个系统级别的缓存,这个缓存会存放一些曾加载并分析过的javabean的引用,当web服务器关闭的时候,
由于这个缓存中存放着这些javabean的引用,所以垃圾回收器不能对web容器中的javaBean对象进行回收,导致内存越来越大。
spring提供的org.springframework.web.util.IntrospectorCleanupListener就解决了这个问题,他会在web服务器停止的时候,清理一下这个Introspector缓存。使那些javabean能被垃圾回收器正确回收。
spring不会出现这种问题,因为spring在加载并分析完一个类之后会马上刷新JavaBeans
Introspector缓存,这样就保证了spring不会出现这种内存泄漏的问题。
但是有很多程序和框架在使用了JavaBeans
Introspector之后,都没有进行清理工作,比如quartz、struts;解决办法很简单,就是上面的那个配置。这是
因为:对于Web容器来说,最忌讳应用程序私自启动线程,自行进行线程调度,像Quartz这种在web容器内部默认就自己启动了10线程进行异步job
调度的框架本身就是很危险的事情,很容易造成servlet线程资源回收不掉,所以我一向排斥使用quartz。quartz还有一个问题就是不支持
cluster。导致使用quartz的应用都没有办法做群集。
Spring学习6-Spring整合Struts2的更多相关文章
- Spring学习(十一)-----Spring使用@Required注解依赖检查
Spring学习(九)-----Spring依赖检查 bean 配置文件用于确定的特定类型(基本,集合或对象)的所有属性被设置.在大多数情况下,你只需要确保特定属性已经设置但不是所有属性.. 对于这种 ...
- Spring学习(六)-----Spring使用@Autowired注解自动装配
Spring使用@Autowired注解自动装配 在上一篇 Spring学习(三)-----Spring自动装配Beans示例中,它会匹配当前Spring容器任何bean的属性自动装配.在大多数情况下 ...
- Spring学习笔记四 整合SSH
三大框架架构(整合原理) 步骤1:导包 Hibernate包 1.Hibernate包,hibernate/lib/required 2.hibernate/lib/jpa | java persis ...
- spring学习 六 spring与mybatis整合
在mybatis学习中有两种配置文件 :全局配置文件,映射配置文件.mybatis和spring整合,其实就是把mybatis中的全局配置文件的配置内容都变成一个spring容器的一个bean,让sp ...
- Spring学习之Spring与Mybatis的两种整合方式
本机使用IDEA 2020.1.MySql 8.0.19,通过Maven进行构建 环境准备 导入maven依赖包 <dependencies> <dependency> < ...
- spring学习07(整合MyBatis)
10.整合MyBatis 10.1 相关jar包 junit <dependency> <groupId>junit</groupId> <artifactI ...
- spring学习(三) ———— spring事务操作
前面一篇博文讲解了什么是AOP.学会了写AOP的实现,但是并没有实际运用起来,这一篇博文就算是对AOP技术应用的进阶把,重点是事务的处理. --wh 一.jdbcTemplate 什么是JdbcTem ...
- spring学习12 -Spring 框架模块以及面试常见问题注解等
以下为spring常见面试问题: 1.Spring 框架中都用到了哪些设计模式? Spring框架中使用到了大量的设计模式,下面列举了比较有代表性的: 代理模式—在AOP和remoting中被用的比较 ...
- Spring学习【Spring概述】
从本文開始,我们就要一起学习Spring框架,首先不得不说Spring框架是一个优秀的开源框架. 当中採用IoC原理实现的基于Java Beans的配置管理和AOP的思想都是非常值得学习与使用的.以下 ...
- Spring学习笔记—Spring之旅
1.Spring简介 Spring是一个开源框架,最早由Rod Johnson创建,并在<Expert One-on-One:J2EE Design and Development> ...
随机推荐
- 08Spring_Spring和junit测试集成
第一步: 在项目导入 spring-test-3.2.0.RELEASE.jar 第二步: 编写测试类
- JAVA 根据数据库表内容生产树结构JSON数据
1.利用场景 组织机构树,通常会有组织机构表,其中有code(代码),pcode(上级代码),name(组织名称)等字段 2.构造数据(以下数据并不是组织机构数据,而纯属本人胡编乱造的数据) List ...
- 数字签名的定义及在K2 BPM业务流程管理中的应用
背景介绍 ARX(算法研究)公司是一家数字签名和数据安全解决方案的领先供应商.该公司专门从事结合软件和硬件产品为大中小型计算机环境设计和实现数字签名解决方案. 很多流程都需要和数字签名进行集成,用于审 ...
- [IIS][ASP.NET]“拒绝访问临时目录”的解决方法
开始以为是“C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files”文件夹权限的问题,但怎么设置这个权限也解决不 ...
- PHP基础16:多维数组
<?php //1.PHP-两维数组 $cars=array ( array("Volvo",22,18), array("BMW",15,13), ar ...
- listview的头布局把我的ACTION_DOWN事件给吃了.....
因为头布局的viewpager自己处理点击事件 public boolean dispatchTouchEvent(MotionEvent ev) { switch (ev.getAction()) ...
- WireShark系列: 使用WireShark过滤条件抓取特定数据流(zz)
应用抓包过滤,选择Capture | Options,扩展窗口查看到Capture Filter栏.双击选定的接口,如下图所示,弹出Edit Interface Settints窗口. 下图显示了Ed ...
- survival analysis 生存分析与R 语言示例 入门篇
原创博客,未经允许,不得转载. 生存分析,survival analysis,顾名思义是用来研究个体的存活概率与时间的关系.例如研究病人感染了病毒后,多长时间会死亡:工作的机器多长时间会发生崩溃等. ...
- Jenkins进阶系列之——03parameterized-trigger插件
说明:这个插件可以根据已经完成构建的结果,触发新Job或者传递参数. 官方说明:Parameterized Trigger Plugin 安装步骤: 系统管理→管理插件→可选插件→Build Trig ...
- java <? super Fruit>与<? extends Fruit>
package Test2016; import java.util.ArrayList; import java.util.List; public class Test2016 { public ...