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> ...
随机推荐
- Maven 小结
Maven 的各项功能通过插件实现,有需要的时候学习那些插件的配置即可 一般一个大型项目会有 A:父管理工程,定义了所有的依赖和插件 B:工具工程 C:web 项目的父工程,同时也是一个聚合工程 D: ...
- rsyslog 日志统一搜集&message格式
日志格式修改: http://jiechao2012.blog.51cto.com/3251753/1143762 http://yulei7633.blog.51cto.com/149275 ...
- C语言 百炼成钢11
//题目31:请输入星期几的第一个字母来判断一下是星期几,如果第一个字母一样,则继续 //判断第二个字母. #define _CRT_SECURE_NO_WARNINGS #include<st ...
- 如何解决机器重启后MySql服务跑不起来了1067,附解决过程
今个一位同事的mysql跑不起来了,之前还好好的,问我怎么恢复,我没在她出问题的机器上弄,在自己机器上试的,最终在CTO的指导下解决了,现记录于此. 1,首先把她的mysql安装文件夹.数据结构文件. ...
- python 线性回归示例
说明:此文的第一部分参考了这里 用python进行线性回归分析非常方便,有现成的库可以使用比如:numpy.linalog.lstsq例子.scipy.stats.linregress例子.panda ...
- ios 指南针
指南针图片 IBOutlet UIImageView *compassView 1 #import "ViewController.h" #import <CoreLoca ...
- LeetCode Question Difficulty Distribution
参考链接:https://docs.google.com/spreadsheet/pub?key=0Aqt--%20wSNYfuxdGxQWVFsOGdVVWxQRlNUVXZTdEpOeEE& ...
- iOS中UIMenuController的使用
不知你有没有发现,在你的微信朋友中,长按一段文字(正文或者评论),会弹出这样子的玩意: 要想在你的view或者viewController中实现长按弹出菜单栏你必须要调用becomeFirstResp ...
- Jenkins进阶之自动发送邮件的Default Content设置模板
分享一个简洁实用的Jenkins项目邮件管理系统的"Default Content"设置模板 配置如下: <h1><center><font colo ...
- 教你写一个Android可快速复用的小键盘输入控件
引子 在Android项目开发中特别是一些稍大型的项目,面对需求文档的时候你经常会发现很多地方用到了同样的组件,但是又略有不同.比如这个: 右边是一个小键盘输入板,左边当焦点不同的时候分别用右边的小键 ...