ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
# 【第十章】集成其它Web框架 之 10.1 概述 ——跟我学spring3 ## 10.1  概述 ### 10.1.1  Spring和Web框架 Spring框架不仅提供了一套自己的Web框架实现,还支持集成第三方Web框架(如Struts1x、Struts2x)。 Spring实现的SpringMVC Web框架将在第十八章详细介绍。 由于现在有很大部分公司在使用第三方Web框架,对于并不熟悉SpringMVC Web框架的公司,为了充分利用开发人员已掌握的技术并相使用Spring的功能,想集成所使用的Web框架;由于Spring框架的高度可配置和可选择性,因此集成这些第三方Web框架是非常简单的。 之所以想把这些第三方Web框架集成到Spring中,最核心的价值是享受Spring的某些强大功能,如一致的数据访问,事务管理,IOC,AOP等等。 Spring为所有Web框架提供一致的通用配置,从而不管使用什么Web框架都使用该通用配置。 ### 10.1.2  通用配置 Spring对所有Web框架抽象出通用配置,以减少重复配置,其中主要有以下配置: **1、Web环境准备:** **1.1、在spring项目下创建如图10-1目录结构:** ![](https://box.kancloud.cn/2016-05-13_57354721c1e7b.JPG) 图10-1 web目录结构 **1.2、右击spring项目选择【Propeties】,然后选择【Java Build Path】中的【Source】选项卡,将类输出路径修改为“spring/webapp/WEB-INF/classes”,如图10-2所示:** ![](https://box.kancloud.cn/2016-05-13_57354721d498e.JPG)  图10-2 修改类输出路径 **1.3、web.xml初始内容如下:** ``` <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" 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"> </web-app> ``` &lt;web-app version="2.4"&gt;表示采用Servlet 2.4规范的Web程序部署描述格式 **2、 指定Web应用上下文实现:**在Web环境中,Spring提供WebApplicationContext(继承ApplicationContext)接口用于配置Web应用,该接口应该被实现为在Web应用程序运行时只读,即在初始化完毕后不能修改Spring Web容器(WebApplicationContext),但可能支持重载。 Spring提供XmlWebApplicationContext实现,并在Web应用程序中默认使用该实现,可以通过在web.xml配置文件中使用如下方式指定: ``` <context-param> <param-name>contextClass</param-name> <param-value> org.springframework.web.context.support.XmlWebApplicationContext </param-value> </context-param> ``` 如上指定是可选的,只有当使用其他实现时才需要显示指定。 **3、 指定加载文件位置:** 前边已经指定了Spring Web容器实现,那从什么地方加载配置文件呢? 默认情况下将加载/WEB-INF/applicationContext.xml配置文件,当然也可以使用如下形式在web.xml中定义要加载自定义的配置文件,多个配置文件用“,”分割: ``` <context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:chapter10/applicationContext-message.xml </param-value> </context-param> ``` 通用Spring配置文件(resources/chapter10/applicationContext-message.xml)内容如下所示: ``` <bean id="message" class="java.lang.String"> <constructor-arg index="0" value="Hello Spring"/> </bean> ``` **4、 加载和关闭Spring Web容器:** 我们已经指定了Spring Web容器实现和配置文件,那如何才能让Spring使用相应的Spring Web容器实现加载配置文件呢? Spring使用ContextLoaderListener监听器来加载和关闭Spring Web容器,即使用如下方式在web.xml中指定: ``` <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> ``` ContextLoaderListener监听器将在Web应用启动时使用指定的配置文件初始化Spring Web容器,在Web应用关闭时销毁Spring Web容器。 注:监听器是从Servlet 2.3才开始支持的,因此如果Web应用所运行的环境是Servlet 2.2版本则可以使用ContextLoaderServlet来完成,但从Spring3.x版本之后ContextLoaderServlet被移除了。 **5、 在Web环境中获取Spring Web容器:** 既然已经定义了Spring Web容器,那如何在Web中访问呢?Spring提供如下方式来支持获取Spring Web容器(WebApplicationContext): ``` WebApplicationContextUtils.getWebApplicationContext(servletContext); 或 WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext); ``` 如果当前Web应用中的ServletContext 中没有相应的Spring Web容器,对于getWebApplicationContext()方法将返回null,而getRequiredWebApplicationContext()方法将抛出异常,建议使用第二种方式,因为缺失Spring Web容器而又想获取它,很明显是错误的,应该抛出异常。 **6、 通用jar包,从下载的spring-framework-3.0.5.RELEASE-with-docs.zip中dist目录查找如下jar包:** + org.springframework.web-3.0.5.RELEASE.jar  此jar包为所有Web框架所共有,提供WebApplicationContext及实现等。 **7、Web服务器选择及测试:** 目前比较流行的支持Servlet规范的开源Web服务器包括Tomcat、Resin、Jetty等,Web服务器有独立运行和嵌入式运行之分,嵌入式Web服务器可以在测试用例中运行不依赖于外部环境,因此我们使用嵌入式Web服务器。 Jetty是一个非常轻量级的Web服务器,并且提供嵌入式运行支持,在此我们选用Jetty作为测试使用的Web服务器。 **7.1、准备Jetty嵌入式Web服务器运行需要的jar包:** ``` 到http://dist.codehaus.org/jetty/网站下载jetty-6.1.24,在下载的jetty-6.1.24.zip包中拷贝如下jar包到项目的lib/jetty目录下,并添加到类路径中:   ``` ![](https://box.kancloud.cn/2016-05-13_57354721e86c9.JPG) **7.2、在单元测试中启动Web服务器:** ``` package cn.javass.spring.chapter10; import org.junit.Test; import org.mortbay.jetty.Server; import org.mortbay.jetty.webapp.WebAppContext; public class WebFrameWorkIntegrateTest { @Test public void testWebFrameWork() throws Exception { Server server = new Server(8080); WebAppContext webapp = new WebAppContext(); webapp.setResourceBase("webapp"); //webapp.setDescriptor("webapp/WEB-INF/web.xml"); webapp.setContextPath("/"); webapp.setClassLoader(Thread.currentThread().getContextClassLoader()); server.setHandler(webapp); server.start(); server.join(); //server.stop(); } } ``` * **创建内嵌式Web服务器:**使用new Server(8080)新建一个Jetty服务器,监听端口为8080; * **创建一个Web应用:**使用new WebAppContext()新建一个Web应用对象,一个Web应用可以认为就是一个WebAppContext对象; * **指定Web应用的目录:**使用webapp.setResourceBase("webapp")指定Web应用位于项目根目录下的“webapp”目录下; * **指定部署描述符:**使用webapp.setDescriptor("webapp/WEB-INF/web.xml");此处指定部署描述符为项目根目录下的“webapp/WEB-INF/web.xml”,该步骤是可选的,如果web.xml位于Web应用的WEB-INF下。 * **指定Web应用请求上下文:**使用webapp.setContextPath("/")指定请求上下文为“/”,从而访问该Web应用可以使用如“http://localhost:8080/hello.do”形式访问; * 指定类装载器:因为Jetty自带的ClassLoader在内嵌环境中对中文路径处理有问题,因此我们使用Eclispe的ClassLoader,即通过“webapp.setClassLoader(Thread.currentThread().getContextClassLoader()) ”指定; * **启动Web服务器:**使用“server.start()”启动并使用“server.join()”保证Web服务器一直运行; * **关闭Web服务器:**可以通过某种方式执行“server.stop()”来关闭Web服务器;另一种方式是通过【Console】控制台面板的【Terminate】终止按钮关闭,如图10-3所示: ![](https://box.kancloud.cn/2016-05-13_5735472206ad5.JPG)  图10-3 点击红色按钮关闭Web服务器 原创内容,转载请注明出处【[http://sishuok.com/forum/blogPost/list/0/2510.html](http://sishuok.com/forum/blogPost/list/0/2510.html#7233)】