Spring与SpringMVC父子容器的关系与初始化

原文地址

Spring和SpringMVC的容器具有父子关系,Spring容器为父容器,SpringMVC为子容器,子容器可以引用父容器中的Bean,而父容器不可以引用子容器中的Bean。


了解了Spring与SpringMVC父子容器的关系,接下来让我们看看Spring与SpringMVC容器的初始化过程。


以下讲解使用的web.xml文件如下:


    
    
  1. <context-param>
  2. <param-name>contextConfigLocation</param-name> //指定spring ioc配置文件的位置
  3. <param-value>classpath*:spring /*.xml</param-value>
  4. </context-param>
  5. <!-- Creates the Spring Container shared by all Servlets and Filters -->
  6. <listener>
  7. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  8. </listener>

    
    
  1. <!-- 配置DisaptcherServlet -->
  2. <servlet>
  3. <servlet-name>springMVC</servlet-name>
  4. <servlet- class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  5. <!-- 初始化参数,配置springmvc配置文件 -->
  6. <init-param>
  7. <param-name>contextConfigLocation</param-name>
  8. <param-value>springMVC配置文件的路径</param-value>
  9. </init-param>
  10. <!-- web容器启动时加载该Servlet -->
  11. <load-on-startup>1</load-on-startup>
  12. </servlet>
  13. <servlet-mapping>
  14. <servlet-name>springMVC</servlet-name>
  15. <url-pattern>/</url-pattern>
  16. </servlet-mapping>

spring ioc容器初始化的过程

1、web应用程序启动时,tomcat会读取web.xml文件中的context-parm(含有配置文件的路径)和listener节点,接着会为应用程序创建一个ServletContext,为全局共享,Spring ioc容器就是存储在这里


2、tomcat将context-param节点转换为键值对,写入到ServletContext中


3、创建listener节点中的ContextLoaderListener实例,调用该实例,初始化webapplicationContext,这是一个接口,其实现类为XmlWebApplicationContext(即spring的IOC容器),其通过ServletContext.getinitialParameter("contextConfigLoaction")从ServletContext中获取context-param中的值(即spring ioc容器配置文件的路径),这就是为什么要有第二步的原因。接着根据配置文件的路径加载配置文件信息(其中含有Bean的配置信息)到WebApplicationContext(即spring ioc容器)中,将WebApplicationContext以WebApplicationContext.ROOTWEBAPPLICATIONCONTEXTATTRIBUTE为属性Key,将其存储到ServletContext中,便于获取。至此,spring ioc容器初始化完毕


4、容器初始化web.xml中配置的servlet,为其初始化自己的上下文信息servletContext,并加载其设置的配置信息到该上下文中。将WebApplicationContext(即spring ioc容器)设置为它的父容器。其中便有SpringMVC(假设配置了SpringMVC),这就是为什么spring ioc是springmvc ioc的父容器的原因


SpringMVC初始化过程


SpringMVC通过web.xml文件中servlet标签下的DispatcherServlet类完成自身的初始化

DispatcherServlet类的继承体系如下:


请注意每个长方形中第三行的方法,其为完成SpringMVC ioc容器初始化的关键。

我们知道,每个servlet在初始化时,会先调用servlte的构造函数(为默认构造函数),接着调用init函数,而DispatcherServlet的init方法在其父类HttpServlet中。


HttpServlet中的init方法

    
    
  1. /DispatcherServlet第一次加载时调用init方法
  2. @Override
  3.     public final void init() throws ServletException {
  4.         if (logger.isDebugEnabled()) {
  5.             logger.debug( "Initializing servlet '" + getServletName() + "'");
  6.         }
  7.         // Set bean properties from init parameters.
  8.         try {
  9. /*加载web.xml文件中的servlet标签中的init-param,其中含有springMVC的配置文件的名字和路径
  10.  *若没有,则默认为(servlet-name)-servlet.xml,
  11.  *默认路径为WEF—INF下
  12.  */
  13.             PropertyValues pvs = new ServletConfigPropertyValues(getServletConfig(), this.requiredProperties);
  14.           //创建BeanWrapper实例,为DispatcherServlet设置属性
  15.             BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess( this);
  16.             ResourceLoader resourceLoader = new ServletContextResourceLoader(getServletContext());
  17.             bw.registerCustomEditor(Resource.class, new ResourceEditor(resourceLoader, getEnvironment()));
  18.             initBeanWrapper(bw);
  19.           //把init-param中的参数设置到DispatcherServlet里面去
  20.             bw.setPropertyValues(pvs, true);
  21.         }
  22.         catch (BeansException ex) {
  23.             logger.error( "Failed to set bean properties on servlet '" + getServletName() + "'", ex);
  24.             throw ex;
  25.         }
  26.         // Let subclasses do whatever initialization they like.
  27.         //该方法在FrameworkServlet中
  28.         initServletBean();
  29.         if (logger.isDebugEnabled()) {
  30.             logger.debug( "Servlet '" + getServletName() + "' configured successfully");
  31.         }
  32.     }

FrameworkServlet中的initServletBean方法

    
    
  1. @Override
  2. protected final void initServletBean() throws ServletException {
  3. getServletContext().log( "Initializing Spring FrameworkServlet '" + getServletName() + "'");
  4. if ( this.logger.isInfoEnabled()) {
  5. this.logger.info( "FrameworkServlet '" + getServletName() + "': initialization started");
  6. }
  7. long startTime = System.currentTimeMillis();
  8. try {
  9. //创建springmvc的ioc容器实例
  10. this.webApplicationContext = initWebApplicationContext();
  11. initFrameworkServlet();
  12. }
  13. catch (ServletException ex) {
  14. this.logger.error( "Context initialization failed", ex);
  15. throw ex;
  16. }
  17. catch (RuntimeException ex) {
  18. this.logger.error( "Context initialization failed", ex);
  19. throw ex;
  20. }
  21. if ( this.logger.isInfoEnabled()) {
  22. long elapsedTime = System.currentTimeMillis() - startTime;
  23. this.logger.info( "FrameworkServlet '" + getServletName() + "': initialization completed in " +
  24. elapsedTime + " ms");
  25. }
  26. }

FrameworkServlet中的initWebapplicationContext方法

    
    
  1. protected WebApplicationContext initWebApplicationContext() {
  2.         //首先通过ServletContext获得spring容器,因为子容器springMVC要和父容器spring容器进行关联
  3.         //这就是为什么要在ServletContext中注册spring ioc容器的原因
  4.         WebApplicationContext rootContext =
  5.                 WebApplicationContextUtils.getWebApplicationContext(getServletContext());
  6.         //定义springMVC容器wac
  7.         WebApplicationContext wac = null;
  8.         //判断容器是否由编程式传入(即是否已经存在了容器实例),存在的话直接赋值给wac,给springMVC容器设置父容器
  9.         //最后调用刷新函数configureAndRefreshWebApplicationContext(wac),作用是把springMVC的配置信息加载到容器中去(之前已经将配置信息的路径设置到了bw中)
  10.         if ( this.webApplicationContext != null) {
  11.             // A context instance was injected at construction time -> use it
  12.             wac = this.webApplicationContext;
  13.             if (wac instanceof ConfigurableWebApplicationContext) {
  14.                 ConfigurableWebApplicationContext cwac = (ConfigurableWebApplicationContext) wac;
  15.                 if (!cwac.isActive()) {
  16.                     if (cwac.getParent() == null) {
  17.                         // The context instance was injected without an explicit parent -> set
  18.                         // the root application context (if any; may be null) as the parent
  19.                         //将spring ioc设置为springMVC ioc的父容器
  20.                         cwac.setParent(rootContext);
  21.                     }
  22.                     configureAndRefreshWebApplicationContext(cwac);
  23.                 }
  24.             }
  25.         }
  26.         if (wac == null) {
  27.             // 在ServletContext中寻找是否有springMVC容器,初次运行是没有的,springMVC初始化完毕ServletContext就有了springMVC容器
  28.             wac = findWebApplicationContext();
  29.         }
  30.         //当wac既没有没被编程式注册到容器中的,也没在ServletContext找得到,此时就要新建一个springMVC容器
  31.         if (wac == null) {
  32.             // 创建springMVC容器
  33.             wac = createWebApplicationContext(rootContext);
  34.         }
  35.         if (! this.refreshEventReceived) {
  36.             //到这里mvc的容器已经创建完毕,接着才是真正调用DispatcherServlet的初始化方法onRefresh(wac)
  37.             onRefresh(wac);
  38.         }
  39.         if ( this.publishContext) {
  40.             //将springMVC容器存放到ServletContext中去,方便下次取出来
  41.             String attrName = getServletContextAttributeName();
  42.             getServletContext().setAttribute(attrName, wac);
  43.             if ( this.logger.isDebugEnabled()) {
  44.                 this.logger.debug( "Published WebApplicationContext of servlet '" + getServletName() +
  45.                         "' as ServletContext attribute with name [" + attrName + "]");
  46.             }
  47.         }
  48.         return wac;
  49.     }


FrameworkServlet中的createWebApplicationContext(WebApplicationContext parent)方法

    
    
  1. protected WebApplicationContext createWebApplicationContext(ApplicationContext parent) {
  2. Class<?> contextClass = getContextClass();
  3. if ( this.logger.isDebugEnabled()) {
  4. this.logger.debug( "Servlet with name '" + getServletName() +
  5. "' will try to create custom WebApplicationContext context of class '" +
  6. contextClass.getName() + "'" + ", using parent context [" + parent + "]");
  7. }
  8. if (!ConfigurableWebApplicationContext.class.isAssignableFrom(contextClass)) {
  9. throw new ApplicationContextException(
  10. "Fatal initialization error in servlet with name '" + getServletName() +
  11. "': custom WebApplicationContext class [" + contextClass.getName() +
  12. "] is not of type ConfigurableWebApplicationContext");
  13. }
  14. //实例化空白的ioc容器
  15. ConfigurableWebApplicationContext wac =
  16. (ConfigurableWebApplicationContext) BeanUtils.instantiateClass(contextClass);
  17. //给容器设置环境
  18. wac.setEnvironment(getEnvironment());
  19. //给容器设置父容器(就是spring容器),两个ioc容器关联在一起了
  20. wac.setParent(parent);
  21. //给容器加载springMVC的配置信息,之前已经通过bw将配置文件路径写入到了DispatcherServlet中
  22. wac.setConfigLocation(getContextConfigLocation());
  23. //上面提到过这方法,刷新容器,根据springMVC配置文件完成初始化操作,此时springMVC容器创建完成
  24. configureAndRefreshWebApplicationContext(wac);
  25. return wac;
  26. }

DispatcherServlet的onRefresh(ApplicationContext context)方法

    
    
  1. @Override
  2. protected void onRefresh(ApplicationContext context) {
  3. initStrategies(context);
  4. }

DispatcherServlet的initStrategies(ApplicationContext context)方法


    
    
  1. protected void initStrategies(ApplicationContext context) {
  2. initMultipartResolver(context); //文件上传解析
  3. initLocaleResolver(context); //本地解析
  4. initThemeResolver(context); //主题解析
  5. initHandlerMappings(context); //url请求映射
  6. initHandlerAdapters(context); //初始化真正调用controloler方法的类
  7. initHandlerExceptionResolvers(context); //异常解析
  8. initRequestToViewNameTranslator(context);
  9. initViewResolvers(context); //视图解析
  10. initFlashMapManager(context);
  11. }


总结以下DispatcherServlet及各个父类(接口)的功能:

HttpServlet:实现了init方法,完成web,xml中与DispatcherServlet有关的参数的读入,初始化DispatcherServlet。

FrameworkServlet:完成了springMVC ioc 容器的创建,并且将spring ioc容器设置为springMVC ioc容器的父容器,将springMVC ioc容器注册到ServletContext中

DispatcherServlet:完成策略组件的初始化

至此,SpringMVC容器初始化完成


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值