Spring系列-Spring MVC初始化流程

什么是Spring MVC?
Spring MVC是Spring提供的的web框架,类似于Struts。不过在说Spring MVC之前,先说下MVC和三层架构吧,我们在web开发中,经常用到MVC模式,这个MVC指的是:
Model(数据模型) + View(视图) + Controller(控制器),MVC模式在UI设计中使用非常普遍,主要特点是分离了模型,视图,控制器三种角色,将业务处理从UI设计中独立出来,使得相互之间解耦,可以独立扩展而不需要彼此依赖。
三层架构:
Presentation tier(展现层) + Application tier(应用层) + Data tier(数据访问层)。

MVC和三层架构并不是同一回事,三层架构是整个应用角度的架构,应用层对应我们Service类,执行业务流程,数据访问层对应我们的Dao层,进行数据库的操作。展现层,相当于Spring MVC,也就是说,Spring MVC中的Model,View,Controller都是属于三层架构中的展现层。

做Web开发的人,应该都知道SSH架构,就是Struts + Spring + Hibernate,这是Web应用中最常用的技术架构之一,Struts作为Web框架,帮助构建UI,Spring作为应用平台,Hibernate作为数据持久化实现。
不过其实Spring也有Spring MVC,作为Web框架为用户开发Web应用提供支持,而且现在Struts的使用也越来越少了,使用Spring MVC的用户越来越多。

既然知道了Spring MVC是Web框架,那就需要知道,Spring MVC是如何起作用的,和Spring本身有什么关系,请求发送到Spring MVC之后,经过了什么流程,最后返回了Response。这篇文章就先分析下Spring MVC是怎么建立起来的,下一篇再来分析Spring MVC处理Http请求的流程。

Spring MVC是建立在Spring IoC基础上的,Spring IoC的启动和Web容器的启动关联起来,Web容器启动的时候,就把Spring IoC载入到Web容器,并完成初始化。这样,Web容器就有了Spring MVC运行的基础。

Spring IoC容器的初始化
web.xml文件是Java web项目中的一个配置文件,主要用于配置欢迎页、Filter、Listener、Servlet等,但并不是必须的,一个java web项目没有web.xml文件照样能跑起来。Tomcat容器/conf目录下也有作用于全局web应用web.xml文件,当一个web项目要启动时,Tomcat会首先加载项目中的web.xml文件,然后通过其中的配置来启动项目,只有配置的各项没有错误时,项目才能正常启动。
我们在配置Tomcat的web.xml时,一般会这么配置:

<servlet>
        <servlet-name>DispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
     <servlet-name>DispatcherServlet</servlet-name>
     <url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring/applicationContext.xml</param-value>
</context-param>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

在web.xml中,会看到,最先配置了一个DispatcherServlet,使用Spring MVC,配置DispatcherServlet是第一步,DispatcherServlet是前置控制器,拦截匹配的请求。这里还能看到配置了ContextLoaderListener,这个是一个监听器。这个监听器是和Web服务器的生命周期相关的,通过监听Web服务器的生命周期,来完成Spring IoC容器的启动和销毁。Spring IoC容器启动之后,就会把DispatchServlet作为Spring MVC处理Web请求的转发器,完成Http请求的转发和响应。

接下来,就看下ContextLoaderListener的实现:

public class ContextLoaderListener extends ContextLoader implements ServletContextListener {

	public ContextLoaderListener() {
	}
	
	public ContextLoaderListener(WebApplicationContext context) {
		super(context);
	}

	/**
	 * Initialize the root web application context.
	 */
	@Override
	public void contextInitialized(ServletContextEvent event) {
		initWebApplicationContext(event.getServletContext());
	}

	/**
	 * Close the root web application context.
	 */
	@Override
	public void contextDestroyed(ServletContextEvent event) {
		closeWebApplicationContext(event.getServletContext());
		ContextCleanupListener.cleanupAttributes(event.getServletContext());
	}
}

ContextLoaderListener是Spring提供的类,它实现了ServletContextListener接口:

public interface ServletContextListener extends EventListener {

    /**
     ** Notification that the web application initialization process is starting.
     * All ServletContextListeners are notified of context initialization before
     * any filter or servlet in the web application is initialized.
     * @param sce Information about the ServletContext that was initialized
     */
    public void contextInitialized(ServletContextEvent sce);

    /**
     ** Notification that the servlet context is about to be shut down. All
     * servlets and filters have been destroy()ed before any
     * ServletContextListeners are notified of context destruction.
     * @param sce Information about the ServletContext that was destroyed
     */
    public void contextDestroyed(ServletContextEvent sce);
}

ServletContextListener接口是在Servlet API中定义的,是ServletContext的监听器,提供了与Servlet生命周期结合的回调,contextInitialized和contextDestroyed方法。从方法名就可以看出,contextInitialized是进行ServletContext初始化时候的回调,contextDestroyed是ServletContext关闭时的回调。
接下来,就看下contextInitialized方法的逻辑,contextInitialized调用了initWebApplicationCon

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值