- 浏览: 74687 次
文章分类
最新评论
-
易学生:
iteye用的 就是这个编辑软件吧。
FCKeditor在线编辑器的使用(jsp:html在线编辑器=FCKeditor 2.2+FCKeditor.java 2.3 ) -
易学生:
引用[*][/size][align=left][/align ...
FCKeditor在线编辑器的使用(jsp:html在线编辑器=FCKeditor 2.2+FCKeditor.java 2.3 ) -
i_lolo:
哈哈哈,笑到流泪了。
DOTA中国 外传 老婆,dota不是这么玩的……
转自:http://xm-king.javaeye.com/blog/540964
虽然Spring也提供了自己的MVC组件,但一来Spring的MVC组件过于繁琐,二来Struts的拥护者实在太多。因此,很多项目都会选择使用Spring整合Struts框架。而且Spring确实可以无缝整合Struts框架,二者结合成一个更实际的J2EE开发平台。利用Struts的PlugIn来启动Spring容器
使用Spring的Web应用时,不用手动创建Spring容器,而是通过配置文件声明式地创建Spring容器。因此,在Web应用中创建Spring容器有如下两个方式:
● 直接在web.xml文件中配置创建Spring容器。
● 利用第三方MVC框架的扩展点,创建Spring容器。
其实第一种创建Spring容器的方式更加常见。为了让Spring容器随Web应用的启动而自动启动,有如下两个方法:
● 利用ServletContextListener实现。
● 采用load-on-startup Servlet实现。
Spring提供ServletContextListener的一个实现ContextLoaderListener,该类可以作为Listener使用,会在创建时自动查找WEB-INF/下的applicationContext.xml文件,因此,如果只有一个配置文件,并且文件名为applicationContext.xml,只需在web.xml文件中增加如下配置片段即可:
<listener>
<listener-class>org.springframework.web.context.
ContextLoaderListener</listener-class>
</listener>
如果有多个配置文件需要载入,则考虑使用<context-param>元素来确定配置文件的文件名。ContextLoaderListener加载时,会查找名为contextConfigLocation的参数。因此,配置context-param时,参数名字应该是contextConfigLocation。带多个配置文件的web.xml文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<!-- 指定Web配置文件的根元素,以及相应的Schema信息 -->
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<!-- 采用listener创建ApplicationContext实例 -->
<listener>
<listener-class>org.springframework.web.context.
ContextLoaderListener</listener-class>
</listener>
<!-- 确定多个配置文件 -->
<context-param>
<!-- 参数名为contextConfigLocation -->
<param-name>contextConfigLocation</param-name>
<!-- 多个配置文件之间以“,”隔开 -->
<param-value>/WEB-INF/daoContext.xml,/WEB-INF/
applicationContext.xml</param-value>
</context-param>
</web-app>
如果没有通过contextConfigLocation指定配置文件,Spring会自动查找application- Context.xml配置文件;如果有contextConfigLocation,
则利用该参数确定的配置文件。如果无法找到合适的配置文件, Spring将无法正常初始化。
Spring 根据bean定义创建WebApplicationContext对象,并将其保存在web应用的ServletContext中。大部分情况下,应用中的Bean无须感受到ApplicationContext的存在,
只要利用ApplicationContext的IoC即可。如果需要在应用中获取ApplicationContext实例,可以通过如下代码获取:
//获取当前Web应用的Spring容器
WebApplicationContext ctx =
WebApplicationContextUtils.getWebApplicationContext(servletContext);
除此之外,Spring提供了一个特殊的Servlet类ContextLoaderServlet。该Servlet在启动时,会自动查找WEB-INF/下的applicationContext.xml文件。
当然,为了让ContextLoaderServlet随应用的启动而启动,应将此Servlet配置成load-on-startup的Servlet,load-on-startup的值小一点比较合适,
这样可以保证Application- Context更快的初始化。如果只有一个配置文件,并且文件名为applicationContext.xml,在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用于提供“后台”服务,主要用于创建Spring容器,无须响应客户请求,因此无须配置servlet-mapping。
如果有多个配置文件,一样使用<context-param>元素来确定多个配置文件。
事实上,不管是ContextLoaderServlet,还是ContextLoaderListener,都依赖于ContextLoader创建ApplicationContext实例。
在ContextLoader代码的第240行,有如下代码:
String configLocation = servletContext.getInitParameter
(CONFIG_LOCATION_PARAM);
if (configLocation != null) {
wac.setConfigLocations(StringUtils.tokenizeToStringArray(configLocation,
ConfigurableWebApplicationContext.CONFIG_LOCATION_DELIMITERS));
}
其中,CONFIG_LOCATION_PARAM是该类的常量,其值为contextConfigLocation。可以看出,ContextLoader首先检查servletContext中是否有contextConfigLocation的参数,如果有该参数,则加载该参数指定的配置文件。
ContextLoaderServlet与ContextLoaderListener底层都依赖于ContextLoader。因此,二者的效果几乎没有区别。之间的区别不是它们本身引起的,而是由于Servlet规范,Listener比Servlet优先加载。因此,采用ContextLoaderListener创建ApplicationContext的时机更早。
当然,也可以通过ServletContext的getAttribute方法获取ApplicationContext。但使用WebApplicationContextUtils类更便捷,因为无须记住ApplicationContext的属性名。即使ServletContext的WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRI-BUTE属性没有对应对象,WebApplicationContextUtils的getWebApplicationContext()方法将会返回空,而不会引起异常。
到底需要使用Listener,还是使用load-on-startupServlet来创建Spring容器呢?通常推荐使用Listener来创建Spring容器。但Listerner是Servlet2.3以上才支持的标准,因此,必须Web容器支持Listener才可使用Listerner。
注意:使用Listener创建Spring容器之前,应先评估Web容器是否支持Listener标准。
还有一种情况,利用第三方MVC框架的扩展点来创建Spring容器,比如Struts。在第2章介绍Strust框架时,知道Struts有一个扩展点PlugIn。
实际上,Spring正是利用了PlugIn这个扩展点,从而提供与Struts的整合。 Spring提供了PlugIn接口的实现类 org.springframework.web.struts.ContextLoaderPlugIn。这个实现类可作为Struts的PlugIn配置,Struts框架启动时,将自动创建Spring容器。
为了利用Struts的PlugIn创建Spring容器,只需在Struts配置文件中增加如下片段 即可:
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation"
value="/WEB-INF/action-servlet.xml,/WEB-INF/applicationContext.xml"/>
</plug-in>
其中,指定contextConfigLocation属性值时,即可以指定一个Spring配置文件的位置,可以指定多个Spring配置文件的位置。
虽然Spring也提供了自己的MVC组件,但一来Spring的MVC组件过于繁琐,二来Struts的拥护者实在太多。因此,很多项目都会选择使用Spring整合Struts框架。而且Spring确实可以无缝整合Struts框架,二者结合成一个更实际的J2EE开发平台。利用Struts的PlugIn来启动Spring容器
使用Spring的Web应用时,不用手动创建Spring容器,而是通过配置文件声明式地创建Spring容器。因此,在Web应用中创建Spring容器有如下两个方式:
● 直接在web.xml文件中配置创建Spring容器。
● 利用第三方MVC框架的扩展点,创建Spring容器。
其实第一种创建Spring容器的方式更加常见。为了让Spring容器随Web应用的启动而自动启动,有如下两个方法:
● 利用ServletContextListener实现。
● 采用load-on-startup Servlet实现。
Spring提供ServletContextListener的一个实现ContextLoaderListener,该类可以作为Listener使用,会在创建时自动查找WEB-INF/下的applicationContext.xml文件,因此,如果只有一个配置文件,并且文件名为applicationContext.xml,只需在web.xml文件中增加如下配置片段即可:
<listener>
<listener-class>org.springframework.web.context.
ContextLoaderListener</listener-class>
</listener>
如果有多个配置文件需要载入,则考虑使用<context-param>元素来确定配置文件的文件名。ContextLoaderListener加载时,会查找名为contextConfigLocation的参数。因此,配置context-param时,参数名字应该是contextConfigLocation。带多个配置文件的web.xml文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<!-- 指定Web配置文件的根元素,以及相应的Schema信息 -->
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<!-- 采用listener创建ApplicationContext实例 -->
<listener>
<listener-class>org.springframework.web.context.
ContextLoaderListener</listener-class>
</listener>
<!-- 确定多个配置文件 -->
<context-param>
<!-- 参数名为contextConfigLocation -->
<param-name>contextConfigLocation</param-name>
<!-- 多个配置文件之间以“,”隔开 -->
<param-value>/WEB-INF/daoContext.xml,/WEB-INF/
applicationContext.xml</param-value>
</context-param>
</web-app>
如果没有通过contextConfigLocation指定配置文件,Spring会自动查找application- Context.xml配置文件;如果有contextConfigLocation,
则利用该参数确定的配置文件。如果无法找到合适的配置文件, Spring将无法正常初始化。
Spring 根据bean定义创建WebApplicationContext对象,并将其保存在web应用的ServletContext中。大部分情况下,应用中的Bean无须感受到ApplicationContext的存在,
只要利用ApplicationContext的IoC即可。如果需要在应用中获取ApplicationContext实例,可以通过如下代码获取:
//获取当前Web应用的Spring容器
WebApplicationContext ctx =
WebApplicationContextUtils.getWebApplicationContext(servletContext);
除此之外,Spring提供了一个特殊的Servlet类ContextLoaderServlet。该Servlet在启动时,会自动查找WEB-INF/下的applicationContext.xml文件。
当然,为了让ContextLoaderServlet随应用的启动而启动,应将此Servlet配置成load-on-startup的Servlet,load-on-startup的值小一点比较合适,
这样可以保证Application- Context更快的初始化。如果只有一个配置文件,并且文件名为applicationContext.xml,在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用于提供“后台”服务,主要用于创建Spring容器,无须响应客户请求,因此无须配置servlet-mapping。
如果有多个配置文件,一样使用<context-param>元素来确定多个配置文件。
事实上,不管是ContextLoaderServlet,还是ContextLoaderListener,都依赖于ContextLoader创建ApplicationContext实例。
在ContextLoader代码的第240行,有如下代码:
String configLocation = servletContext.getInitParameter
(CONFIG_LOCATION_PARAM);
if (configLocation != null) {
wac.setConfigLocations(StringUtils.tokenizeToStringArray(configLocation,
ConfigurableWebApplicationContext.CONFIG_LOCATION_DELIMITERS));
}
其中,CONFIG_LOCATION_PARAM是该类的常量,其值为contextConfigLocation。可以看出,ContextLoader首先检查servletContext中是否有contextConfigLocation的参数,如果有该参数,则加载该参数指定的配置文件。
ContextLoaderServlet与ContextLoaderListener底层都依赖于ContextLoader。因此,二者的效果几乎没有区别。之间的区别不是它们本身引起的,而是由于Servlet规范,Listener比Servlet优先加载。因此,采用ContextLoaderListener创建ApplicationContext的时机更早。
当然,也可以通过ServletContext的getAttribute方法获取ApplicationContext。但使用WebApplicationContextUtils类更便捷,因为无须记住ApplicationContext的属性名。即使ServletContext的WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRI-BUTE属性没有对应对象,WebApplicationContextUtils的getWebApplicationContext()方法将会返回空,而不会引起异常。
到底需要使用Listener,还是使用load-on-startupServlet来创建Spring容器呢?通常推荐使用Listener来创建Spring容器。但Listerner是Servlet2.3以上才支持的标准,因此,必须Web容器支持Listener才可使用Listerner。
注意:使用Listener创建Spring容器之前,应先评估Web容器是否支持Listener标准。
还有一种情况,利用第三方MVC框架的扩展点来创建Spring容器,比如Struts。在第2章介绍Strust框架时,知道Struts有一个扩展点PlugIn。
实际上,Spring正是利用了PlugIn这个扩展点,从而提供与Struts的整合。 Spring提供了PlugIn接口的实现类 org.springframework.web.struts.ContextLoaderPlugIn。这个实现类可作为Struts的PlugIn配置,Struts框架启动时,将自动创建Spring容器。
为了利用Struts的PlugIn创建Spring容器,只需在Struts配置文件中增加如下片段 即可:
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation"
value="/WEB-INF/action-servlet.xml,/WEB-INF/applicationContext.xml"/>
</plug-in>
其中,指定contextConfigLocation属性值时,即可以指定一个Spring配置文件的位置,可以指定多个Spring配置文件的位置。
发表评论
-
Java数据类型和MySql数据类型对应一览
2017-04-12 16:00 1377类型名称 显示长度 数据库类型 JAVA类型 J ... -
mysql索引详解(转)
2017-04-06 14:58 422什么是索引 转自:htt ... -
MySQL性能优化的最佳20+条经验
2017-04-04 10:12 491转自:http://blog.csdn.net/hal ... -
java.util.Date、java.sql.Date、java.sql.Time、java.sql.Timestamp区别和联系
2017-03-28 13:53 705转自:http://swiftlet. ... -
SSH2整合--配置+详解
2017-03-24 16:41 0转自:http://blog.csdn.net ... -
Spring注解@Component、@Repository、@Service、@Controlle
2012-07-20 14:42 864转载:http://hi.baidu.com/javahot1 ... -
Spring3 MVC (一)----注解基本配置及@Controller和@RequestMapp
2012-07-18 14:30 868转自:http://ttaale.javaeye.com/bl ... -
Spring3 MVC (二)----注解基本配置及@Controller和@RequestMapp
2012-07-18 14:30 882转自:http://ttaale.javaeye.com/bl ... -
Spring3 MVC (三)----注解基本配置及@Controller和@RequestMapp
2012-07-18 14:30 1470Spring3 MVC (三)----注解基本配置及@Cont ... -
Spring整合struts(二)
2012-07-18 14:31 748转自:http://xm-king.javaeye.com/b ... -
java的定时器使用方法
2012-07-19 09:44 1847定时器类Timer在java.util包中。使用时,先实例化, ... -
浅谈Socket编程及Java实现
2012-07-20 14:42 691浅谈Socket编程及Java实现 ... -
Collection,List,Set和Map等集合类的用法
2012-07-20 14:41 1044线性表,链表,哈希表 ... -
java中assert使用
2012-07-17 16:49 926转自:http://jerrygao.iteye.com/bl ... -
使用Apache-commons-email发送邮件
2012-07-17 16:31 1436引用 使用Apache-commons-email发送邮件 ... -
java基础:java日期加减法
2012-07-17 16:27 3395转自:http://article.pchome.net/co ... -
【转】两个List合并的问题
2012-07-17 16:18 1295import java.util.ArrayList; im ... -
定时器的实现、java定时器介绍与Spring中定时器的配置 1定时器的作用
2012-07-17 15:35 8421定时器的作用 转自:http://blog.csdn.net ...
相关推荐
Spring 整合 Struts,很强大
使用Spring整合Struts编写多页面用户注册模块 使用Spring整合Struts编写多页面用户注册模块
spring框架整合struts框架时必需的类库文件
Spring整合集成Struts1.2最简单例子
只使用spring和struts进行整合
Spring与struts整合主要有三种方式,通过Spring的ActionSupport类,通过Spring的DelegatingRequestProcessor类、通过Spring的DelegatingActionProxy类。
spring整合struts.rar
Spring 整合 Struts2 详解
Spring整合Struts2 SSH 最少jar包
spring整合struts2与hibernate核心配置文件
JPA+Spring+Struts整合实例,JPA+Spring+Struts整合实例
此资源中包涵了spring整合struts和hibernate的所有资源,在做项目时只要在工程中引入该jar包就行,不用在eclipse或Myeclipse中加载strut与hibernate的资源了,挺有用的!
Spring+Struts+Hibernate比较详细的整合配置方案
整合Spring与Struts的几种方法
spring与struts2整合的小示例,适合初学者,具体说明在博客里
spring和struts整合的三种方案.
一个spring整合struts2的完整实例源码
深层次理解Spring整合Struts2开发原理,更有效地组织开发过程
struts和spring整合的2种方式
里面有spring+struts2+ibatis整合的jar包,