spring技术内幕10-Web环境中Spring启动过程

1、Spring不但可以在JavaSE环境中应用,在Web环境中也可以广泛应用,Spring在Web环境中应用时,需要在应用的web.xml文件中添加如下配置:

....

<context-param>

         <param-name>contextConfigLocation</param-name>

         <!-- Spring配置文件位置-->

         <param-value>/WEB-INF/applicationContext.xml</param-value>

</context-param>

<listener>

     <listener-class>

           org.springframework.web.context.ContextLoaderListener

     </listener-class>

</listener>

......

通过web描述文件我们可以看到,Spring在web环境中通过ContextLoaderListener监听器类启动,通过加载"/WEB-INF/"目录下的Spring配置文件,WEB服务器启动Spring的初始化过程。

ContextLoaderListener是一个监听器,和Web服务器的生命周期事件紧密关联,即当Web服务器启动时,web服务器声明周期事件将会触发ContextLoaderListener监听器加载Spring配置文件,开始Spring在web服务器中的初始化工作。

2、ContextLoaderListener启动Spring:

ContextLoaderListener继承Spring的ContextLoader上下文加载器类,同时实现了ServletContextListener接口(Servlet上下文监听器),监听Web服务器上下文的启动和停止事件,管理web环境中Spring的启动和销毁过程,其源码如下:

public class ContextLoadListener extends ContextLoader implements ServletContextListener {

      //Spring上下文加载器

       private ContextLoader contextLoader;

     //初始化Spring根上下文,event参数是当前web应用上下文事件

      public void contextInitialized(ServletContextEvent event) {

         //创建Spring上下文加载器,ContextLoaderListener继承ContextLoader,所以ContextLoaderListener本身也是Spring的上下文加载器

          this.contextLoader = createContextLoader();

          if(this.contextLoader == null){

             this.contextLoader = this;

           }

          //根据给定的ServletContext上下文,初始化Spring Web应用上下文

            this.contextLoader.initWebApplicationContext(event.getServletContext());

      }

      //创建Spring上下文加载器

        protected ContextLoader createContextLoader(){

              return null;

         }

       //获取Spring上下文加载器

         public ContextLoader getContextLoader(){

             return this.contextLoader;

         }

      //销毁Spring根上下文

        public void contextDestroyed(ServletContextEvent event){

              if(this.contextLoader != null){

                 //关闭指定Servlet的spring web根上下文

                   this.contextLoader.closeWebApplicationContext(event.getServletContext());

               }

                //清除Servlet上下文中被配置的属性

                  ContextCleanupListener.cleanupAttributes(event.getServletContext());

             }

}

通过对ContextLoaderListener的源码分析,我们看到ContextLoaderListener继承ContextLoader,所以ContextLoaderListener本身也是Spring的上下文加载器。

ContextLoaderListener实现了ServletContextListener接口,当web应用在Web服务器中被启动和停止时,web服务器启动和停止事件会分别触发ContextLoaderListener的contextInitialized和contextDestroyed方法来初始化和销毁Spring上下文。我们通过上述对ContextLoaderListener的源码分析看到真正实现Spring上下文的初始化和销毁功能的是ContextLoader类,接下来我们分析ContextLoader初始化和销毁Spring web上下文的过程。

3、ContextLoader初始化和销毁Spring Web上下文:

(1)ContextLoader初始化Spring web上下文的主要源码如下:

//初始化Spring根上下文

public WebApplicationContext initWebApplicationContext(ServletContext servletContext){

       //ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE = WebApplicationContext.class.getName() + ".ROOT";

       //判断在Servlet上下文中Spring Web应用根上下文是否已经存在

         if(servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE) != null){

               throw new IllegalStateException("Cannot initialize context because there is already a root application context present - " +

                 "check whether you have multiple ContextLoader* definitions in your web.xml!");

           }

          //记录日志

          Log logge = LogFactory.getLog(ContextLoader.class);

          servletContext.log("Initializing Spring root WebApplicationContext");

           if(logger.isInfoEnabled()){

                 logger.info("Root WebApplicationContext: initialization started");

            }

           //获取当前系统时间

             long startTime = System.currentTimeMillis();

             try{

                //如果当前Web根容器有父容器,则获取父容器

                  ApplicationContext parent = loadParentContext(servletContext);

                  //根据给定Servlet容器和父容器创建Web应用容器,并且所创建的web应用容器实例对象存储在本地变量中,以确保当servlet容器关闭时该容器可用

                     this.context = createWebApplicationContext(servletContext,parent);

                     //将创建的web应用上下文设置到Servlet上下文的应用根容器属性中

                       servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,this.context);

                     //获取当前线程的容器类加载器

                       ClassLoader cc1 = Thread.currentThread().getContextClassLoader();

                       //如果当前线程的容器类加载器是ContextLoader类,则当前上下文就设置为创建的web应用上下文

                       if(cc1 == ContextLoader.class.getClassLoader()){

                             currentContext = this.context;

                        }

                       //如果当前线程的容器类加载器不为null,则将创建的web应用上下文和其类加载器缓存在容器类加载器->Web应用上下文Map集合中

                        else if(cc1!= null){

                             currentContextPerThread.put(cc1,this.context);

                        }

                        if(logger.isDebugEnabled()){

                            logger.debug("Published root WebApplicationContext as ServletContext attribute with name [" +

                                  WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE + "]");

                         }

                        if(logger.isInfoEnabled()){

                           long elapsedTime = System.currentTimeMillis() - startTime;

                            logger.info("Root WebApplicationContext: initialization completed in " + elapsedTime + " ms");

                   }

                   //返回创建的web应用上下文

                     return this.context;

            }

            catch(RuntimeException ex){

                    logger.error("Context initialization failed",ex);

                    servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,ex);

                     throw ex;

             }

             catch(Error err){

                  logger.error("Context initialization failed",err);

                 servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,err);

                  throw err;

             }

    }

   //创建Web应用上下文

protected WebApplicationContext createWebApplicationContext(ServletContext sc,ApplicationContext parent){

      //获取当前Servlet上下文接口的实现类

        Class<?> contextClass = determineContextClass(sc);

      //判断使用什么样的类在web容器中作为Ioc容器

       if(!ConfigurableWebApplicationContext.class.isAssignableFrom(contextClass)){

           throw new ApplicationContextException("Custom context class [" + contextClass.getName() +

                  "] is not of type [" + ConfgurableWebApplcationContext.class.getName() + "]");

         }

   //Servlet上下文实现类是ConfigurableWebApplicationContext类型,使用JDK反射机制,调用Servlet上下文实现类的无参构造方法创建实例对象

      ConfigurableWebApplicationContext wac = (ConfigurableWebApplicationContext) BeanUtils.instantiateClass(contextClass);

      //如果Servlet大版本是2,并且小版本号小于5,既是Servlet2.4以下标准

        if(sc.getMajorVersion() == 2 && sc.getMinorVersion()<5){

          //获取Servlet上下文名称

            String servletContextName = sc.getServletContextName();

          //为创建的Web应用上下文设置唯一的Id标识

             wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX + ObjectUtils.getDisplayString(servletContextName));

          }

          //Servlet2.5标准

          else{

              try {

                    //调用Servlet上下文对象中getContextPath()方法

                      String contextPath = (String)ServletContex.class.getMethod("getContextPath").invoke(sc);

                    //为web上下文设置唯一Id标识

                       wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX+ ObjectUtils.getDisplayString(contextPath));

                 }

                 catch(Exception ex){

                      throw new IllegalStateException("Failed to invoke Servlet 2.5 getContextPath method" ,ex);

                   }

              }

             //设置Web上下文的父级上下文

               wac.setParent(parent);

            //设置Web上下文的Servlet上下文

                wac.setServletContext(sc);

             //Servlet上下文从web.xml文件中获取配置的contextConfigLocation参数,并将其作为Spring web上下文配置资源的值

               wac.setConfigLocation(sc.getInitParameter(CONFIG_LOCATION_PARAM));

               //调用用户自定义的设置应用上下文方法

                  customizeContext(sc,wac);

                 //刷新Spring web容器,启动载入spring配置资源的过程

                   wac.refresh();

                    return wac;

          }

 //根据给定的servlet上下文,获取Spring web上下文的实现类:XmlWebApplicationContext或者用户自定义的Spring web应用上下文

   protected Class<?> determineContextClass(ServletContext servletContext){

        //Servlet上下文从web.xml中获取配置的contextClass参数值

          String contextClassName = servletContext.getInitParameter(CONTEXT_CLASS_PARAM);

          //如果web.xml中额外配置了contextClass参数值

         if(contextClassName != null){

              try{

                   //使用JDK反射机制,实例化指定名称的contextClass类对象

                     return CassUtils.forName(contextClassName,ClassUtils.getDefaulClassLoader());

               }

              catch(ClassNotFoundException ex){

                    throw new ApplicationContextException("Failed to load custom context class [" + contextClassName + "]" ,ex);

               }

          }

          //如果web.xml中没有配置contextClass参数值

          else{

            //获取Spring中默认的Web应用上下文策略,即XmlWebApplicationContext

               contextClassName = defaultStrategies.getProperty(WebApplicationContext.class.getName());

                try{

                      //使用JDK反射机制,创建Spring web应用上下文默认策略类对象

                         return ClassUtils.forName(contextClassName,ContextLoader.class.getClassLoader());

                  }

                  catch(ClassNotFoundException ex){

                       throw new ApplicationContextException("Failed to load default context class [" + contextClassName + "]",ex);

                   }

             }

  }

通过上面ContextLoader初始化Spring Web上下文的主要源码分析,我们看到当Web应用在Web服务器启动时,Servlet上下文通过web.xml文件获取所配置的contextConfigLocation、contextClass等参数,定位spring配置文件资源,调用Spring Web容器的涮洗的refresh()方法启动web环境中Spring IoC容器的初始化过程,refresh()方法启动Spring IoC容器的初始化过程在IoC容器源码分析中已经分析过。

(2)ContextLoader关闭Spring Web上下文的主要源码:

//关闭指定Servlet上下文中的Spring Web应用上下文

public void closeWebApplicationContext(ServletContext servletContext){

      servletContext.log("Closing Spring root WebApplicationContext");

      try{

          //Spring web应用上下文的类型都是ConfigurableWebApplicationContext

            if(this.context instanceof ConfigurableWebApplicationContext){

                 //关闭Spring web应用上下文,释放资源,销毁所有缓存的单态模式Bean

                   ((ConfigurableWebApplicationContext)this.context).close();

               }

            }

            finally{

                //获取当前线程的上下文类加载器

                  ClassLoader cc1 = Thread.currentThread().getContextClassLoader();

                //将当前上下文对象设置为null

                  if(cc1 == ContextLoader.class.getClassLoader()){

                        currentContext = null;

                   }

                   //移除容器类加载器->Web应用上下文Map集合中key为指定类加载器的项

                   else if(cc1 != null){

                        currentContextPerThread.remove(cc1);

                    }

                  //移除Servlet上下文中Spring Web根上下文属性

                   servletContext.removeAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);

                  //释放父容器引用

                  if(this.parentContextRef != null) {

                     this.parentContextRef.release();

                  }

             }

       }

4、WebApplicationContext接口:

WebApplicationContext是定义了Spring Web应用上下文的规范的接口,Web服务器在启动时通过该接口来启动Spring,源码如下:

public interface WebApplicationContext extends ApplicationContext {

     //用于定义在Servlet上下文中存储Spring Web根上下文

     String ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE = WebApplicationContext.class.getName() + ".ROOT";

     //请求范围

      String SCOPE_REQUEST = "request";

     //会话范围

       String SCOPE_SESSION = "session";

     //全局会话范围

       String SCOPE_GLOBAL_SESSION = "globalSession";

     //应用程序范围

        String SCOPE_APPLICATION = "application";

     //容器中存储的Servlet上下文环境的Bean名称

        String SERVLET_CONTEXT_BEAN_NAME = "servletContext";

     //web.xml文件中的配置Spring初始化参数的属性

        String CONTEXT_PARAMETERS_BEAN_NAME = "contextParameters";

     //Servlet上下文属性

         String CONTEXT_ATTRIBUTE_BEANS_NAME = "contextAttributes";

     //获取当前应用程序所有Web容器的标准Servlet上下文

       ServletContext.getServletContext();

}

5、XmlWebApplicationContext源码:

在3.(1)ContextLoader初始化Spring Web上下文的determineContextClass方法中,我们知道Spring首先通过Servlet上下文从web.xml文件中获取用户自定义配置的contextClass参数值,如果没有获取到,则默认使用Spring的XmlWebApplicationContext作为Spring Web应用的IoC容器,XmlWebApplicationContext是WebApplicationContext的实现类ConfigurableWebApplicationContext的子类,其源码如下:

public class XmlWebApplicationContext extends AbstractRefreshableWebApplicatioContext {

  //Web应用中Spring配置文件的默认位置和名称,如果没有特别指定,则Spring会根据此位置定义Spring Bean定义资源

  public static final String DEFAULT_CONFIG_LOCATION = "/WEB-INF/applicationContext.xml";

  //Spring Bean定义资源默认前缀

   public static final String DEFAULT_CONFIG_LOCATION_PREFIX = "/WEB-INF/";

  //Spring Bean定义资源默认后缀

   public static final String DEFAULT_CONFIG_LOCATION_SUFFIX = ".xml";

  //在分析Spring IoC初始过程中我们已经分析过,加载Spring Bean定义资源的方法,通过Spring容器刷新的refresh()方法触发

   protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws BeansException,IOException {

       //为Spring容器创建XML Bean定义读取器,加载Spring Bean定义资源

          XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);

          //设置Bean定义读取器,因为XmlWebApplicationContext是DefaultResourceLoader的子类,所以使用默认资源加载器来定义Bean定义资源

           beanDefinitonReader.setResoureceLoader(this);

          //为Bean定义读取器设置SAX实体解析器

            beanDefinitionReader.setEntityResolver(new ResourceEntityResource(this));

         //在加载Bean定义之前,调用子类提供的一些用户自定义初始化Bean定义读取器的方法

           initBeanDefinitionReader(beanDefinitionReader);

        //使用Bean定义读取器加载Bean定义资源

           loadBeanDefinitions(beanDefinitionReader);

   }

   //用户自定义初始化Bean定义读取器的方法

    protected void initBeanDefinitionReader(XmlBeanDefinitionReader beanDefinitionReader){

     }

    //加载Bean定义资源

     protected void loadBeanDefinitions(XmlBeanDefinitionReader reader) throws IOException {

         //获取定位的Bean定义资源路径

          String[] configLocations = getConfigLocations();

           if(configLocations != null){

              //遍历加载所有定义的Bean定义资源

               for(String configLocation : configLocations){

                       reader.loadBeanDefinitions(configLocation);

                }

           }

       }

      //获取默认Bean定义资源

       protected String[] getDefaultConfigLocations(){

         //获取web.xml中的命名空间,如命名空间不为null,则返回"/WEB-INF/命名空间.xml"

       if(getNamespace() != null){

            return new String[] {DEFAULT_CONFIG_LOCATION_PREFIX + getNamespace() + DEFAULT_CONFIG_LOCATION_SUFFIX};

       }

       //如果命名空间为null,则返回"/WEB-INF/applicationContext.xml"

       else{

                 return new String[] {DEFAULT_CONFIG_LOCATION};

        }

     }

}

在3.(1)ContextLoader初始化Spring Web上下文createWebApplicationContext方法创建Web应用上下文时,应用上下文对象通过setConfigLocation(sc.getInitParameter(CONFIG_LOCATION_PARAM));方法将web.xml中配置的contextConfigLocation参数(Spring配置文件位置)设置到Spring Web应用上下文中,在XmlWebApplicationContext的loadBeanDefinitions方法加载Spring Bean定义资源文件时,会逐个加载web.xml中配置的contextConfigLocation参数的Spring Bean定义资源文件。

XmlWebApplicationContext将Web应用中配置的Spring Bean定义资源文件载入到Spring IoC容器中后,接下来的Spring IoC容器初始化和依赖注入的过程我们已经在Spring IoC容器源码分析中具体分析过,至此Web环境中Spring的工作流程分析完毕。

            

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值