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> ...
随机推荐
- ls -F一种非常有用的ls格式
ls -F一种非常有用的ls格式 tz/y/yupeng > ls -F#q# News/ doc/ images/ mbox ...
- C语言 预处理二(宏定义--#define)
//#define 宏定义(宏定义一般大写) //知识点一-->#define的作用域:从#define开始,从上往下,如果遇到#undef就到#undef处结束,如果没有就是作用于当前整个文件 ...
- VMWare安装苹果操作系统OS X
项目要求做一些简单的苹果开发尝试,由于苹果的各种开发要求在Macintosh机上进行,可是项目不值得为一次简单的尝试付出过多的购机费,所以只能另辟蹊径,跟别人学学怎么在虚拟机里面搞: http://j ...
- select、poll、epoll程序实例
三个函数的基本用法如下: select 创建 fd_set rset , allset; FD_ZERO(&allset); FD_SET(listenfd, &allset); 监听 ...
- C#操作JSON
http://www.cnblogs.com/LiZhiW/p/3624729.html C#操作JSON 1. .NET对JSON的支持介绍............................. ...
- 基于React Native的Material Design风格的组件库 MRN
基于React Native的Material Design风格的组件库.(为了平台统一体验,目前只打算支持安卓) 官方网站 http://mrn.js.org/ Github https://git ...
- 缓存算法之belady现象
前言 在使用FIFO算法作为缺页置换算法时,分配的缺页增多,但缺页率反而提高,这样的异常现象称为belady Anomaly. 虽然这种现象说明的场景是缺页置换,但在运用FIFO算法作为缓存算法时,同 ...
- 记一次w3wp占用CPU过高的解决过程(Dictionary和线程安全)
项目上线以来一直存在一个比较揪心的问题,和一个没有信心处理的BUG,那就是在应用程序启动时有可能会导致cpu跑满99%或持续在一个值如50%左右,这样一来对服务器的压力是非常大的,经常出现服务器无法远 ...
- 学习笔记——Maven实战(八)常用Maven插件介绍(下)
我们都知道Maven本质上是一个插件框架,它的核心并不执行任何具体的构建任务,所有这些任务都交给插件来完成,例如编译源代码是由maven- compiler-plugin完成的.进一步说,每个任务对应 ...
- jsoup抓取借书记录
package tushuguan; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; ...