欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

跟我学Spring3(10.1):集成其它Web框架之概述,spring310.1

来源: javaer 分享于  点击 29254 次 点评:208

跟我学Spring3(10.1):集成其它Web框架之概述,spring310.1


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目录结构:

图10-1 web目录结构

1.2、右击spring项目选择【Propeties】,然后选择【Java Build Path】中的【Source】选项卡,将类输出路径修改为“spring/webapp/WEB-INF/classes”,如图10-2所示:

图10-2 修改类输出路径

1.3、web.xml初始内容如下:

java代码:

<?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>

<web-app version=”2.4″>表示采用Servlet 2.4规范的Web程序部署描述格式

2、 指定Web应用上下文实现:在Web环境中,Spring提供WebApplicationContext(继承ApplicationContext)接口用于配置Web应用,该接口应该被实现为在Web应用程序运行时只读,即在初始化完毕后不能修改Spring Web容器(WebApplicationContext),但可能支持重载。

Spring提供XmlWebApplicationContext实现,并在Web应用程序中默认使用该实现,可以通过在web.xml配置文件中使用如下方式指定:

java代码:

<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中定义要加载自定义的配置文件,多个配置文件用“,”分割:

java代码:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        classpath:chapter10/applicationContext-message.xml
    </param-value>
</context-param>

通用Spring配置文件(resources/chapter10/applicationContext-message.xml)内容如下所示:

java代码:

<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中指定:

java代码:

<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):

java代码:

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包:

到<a href="http://dist.codehaus.org/jetty/%E7%BD%91%E7%AB%99%E4%B8%8B%E8%BD%BDjetty-6.1.24">http://dist.codehaus.org/jetty/网站下载jetty-6.1.24</a>,在下载的<a href="http://dist.codehaus.org/jetty/jetty-6.1.24/jetty-6.1.24.zip">jetty-6.1.24.zip</a>包中拷贝如下jar包到项目的lib/jetty目录下,并添加到类路径中:

7.2、在单元测试中启动Web服务器:

java代码:

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所示:

图10-3 点击红色按钮关闭Web服务器

本系列:

  • 跟我学 Spring 3(1): Spring 概述
  • 跟我学 Spring 3(2.1):IoC 基础 
  • 跟我学Spring3(2.2):IoC容器基本原理
  • 跟我学Spring3(2.3):IoC容器基本原理
  • 跟我学Spring3(3.1):DI的配置使用
  • 跟我学Spring3(3.2):DI之循环依赖
  • 跟我学Spring3(3.3):更多的DI知识
  • 跟我学Spring3(3.4):DI之Bean的作用域
  • 跟我学Spring3(4.1):资源之基础知识
  • 跟我学Spring3(4.2):内置Resources实现
  • 跟我学Spring3(4.3):访问Resource
  • 跟我学Spring3(4.4):Resource通配符路径
  • 跟我学Spring3(5.1 & 5.2):Spring表达式语言之概述和SpEL基础
  • 跟我学Spring3(5.3):Spring 表达式语言之 SpEL 语法
  • 跟我学Spring3(5.4):在Bean定义中使用EL—跟我学spring3
  • 跟我学Spring3(6.1):AOP的HelloWorld
  • 跟我学Spring3(6.3):基于Schema的AOP
  • 跟我学Spring3(6.5):AspectJ切入点语法详解
  • 跟我学Spring3(6.6): 通知参数 
  • 跟我学Spring3(6.7): 通知顺序
  • 跟我学Spring3(6.8):切面实例化模型
  • 跟我学Spring3(6.9):代理机制
  • 跟我学Spring3( 7.1 ):对JDBC的支持之概述
  • 跟我学Spring3( 7.2 ):对JDBC的支持之JDBC模板类
  • 跟我学Spring3(7.3):对JDBC的支持之关系数据库操作对象化
  • 跟我学Spring3(7.4):对JDBC的支持之Spring提供的其它帮助
  • 跟我学Spring3(7.5):对JDBC的支持之集成Spring JDBC及最佳实践
  • 跟我学Spring3(8.1):对ORM的支持之概述
  • 跟我学Spring3(8.2):对ORM的支持之集成Hibernate3
  • 跟我学Spring3(9.1):Spring的事务之数据库事务概述
  • 跟我学Spring3(9.2):Spring的事务之事务管理器
  • 跟我学Spring3(9.3):Spring的事务之编程式事务
  • 跟我学Spring3(9.4):Spring的事务之声明式事务
  • 跟我学Spring3(10.1):集成其它Web框架之概述
相关栏目:

用户点评