前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Spring mvc ContextLoaderListener 原理解析

Spring mvc ContextLoaderListener 原理解析

作者头像
java404
发布2018-05-18 14:31:37
4100
发布2018-05-18 14:31:37
举报

对于熟悉Spring MVC功能,首先应从web.xml 开始,在web.xml 文件中我们需要配置一个监听器 ContextLoaderListener,如下。

<!-- 加载spring上下文信息,最主要的功能是解析applicationContext.xml -->
<listener>  
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
</listener>  

<!-- 配置applicationContext.xml的位置路径,让ContextLoaderListener进行加载该配置文件,并解析 -->
<context-param>
    <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml
    </param-value>
</context-param>

ContextLoaderListener有什么用?提供什么功能呢?我们下面通过源码分析下。

ContextLoaderListener 源码分析
public class ContextLoaderListener extends ContextLoader implements ServletContextListener {

    public ContextLoaderListener() {
    }

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

    //web 容器启动时执行
    @Override
    public void contextInitialized(ServletContextEvent event) {
        //初始化WebApplicationContext对象
        initWebApplicationContext(event.getServletContext());
    }
    //web容器销毁时执行
    @Override
    public void contextDestroyed(ServletContextEvent event) {
        closeWebApplicationContext(event.getServletContext());
        ContextCleanupListener.cleanupAttributes(event.getServletContext());
    }
}

ContextLoaderListener 实现了ServletContextListener 接口。 ServletContextListener 接口我们知道是web容器创建的时候开始执行contextInitialized() 方法,web容器销毁时执行contextDestroyed() 方法。那我们就从contextInitialized() 方法开始分析。

contextInitialized() 方法
public void contextInitialized(ServletContextEvent event) {
    //初始化WebApplicationContext对象
    initWebApplicationContext(event.getServletContext());
}
initWebApplicationContext() 方法
  1. 首先判断servletContext中是否存在WebApplicationContext实例,如果存在说明ServletContextListener在web.xml中多次声明,并抛出异常。
  2. 调用 createWebApplicationContext() 方法创建WebApplicationContext实例
  3. 调用configureAndRefreshWebApplicationContext() 方法通过WebApplicationContext进行解析web.xml中配置的applicationContext.xml。
  4. 把WebApplicationContext实例添加到ServletContext中
createWebApplicationContext()
  1. 调用 determineContextClass() 方法获取WebApplicationContext的Class实例
  2. 通过反射创建WebApplicationContext对象,并返回。
determineContextClass()
  1. 首先判断 servletContext中是否存在contextClass,如果有则直接返回该Class 实例(默认是没有配置该值的)
  2. 从defaultStrategies中通过WebApplicationContext全类名(包名和类名)获取要实现WebApplicationContext接口的类。
defaultStrategies

从当前类的路径下查找 ContextLoader.properties 并加载文件中的键值存储到 defaultStrategies中。

ContextLoader.properties
# Default WebApplicationContext implementation class for ContextLoader.
# Used as fallback when no explicit context implementation has been specified as context-param.
# Not meant to be customized by application developers.

org.springframework.web.context.WebApplicationContext=org.springframework.web.context.support.XmlWebApplicationContext

通过WebApplicationContext全类名获取它的实现类是XmlWebApplicationContext。

configureAndRefreshWebApplicationContext()
  1. 获取web.xml中配置的applicationContext.xml路径
  2. 调用refresh()进行加载xml,解析bean。
总结

通过代码分析ContextLoaderListener的作用就是启动Web容器的时候,初始化WebApplicationContext实例,并存放到ServletContext中。

ContextLoaderListener实现了ServletContextListener接口,在Web容器启动的时会默认执行它的contextInitialized() 方法,然后获取WebApplicationContext的实现类 XmlWebApplicationContext,并实例化后存放到ServletContext中,通过XmlWebApplicationContext类进行加载、解析applicationContext.xml 配置文件。ServletContext在整个Web容器范围内都有效,都可以获取得到。

XmlWebApplicationContext类的作用

通过代码可以大概了解到就是读取web.xml中配置的applicationContext.xml ,然后加载解析Bean信息。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017.09.27 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • ContextLoaderListener 源码分析
  • contextInitialized() 方法
  • initWebApplicationContext() 方法
  • createWebApplicationContext()
  • determineContextClass()
  • defaultStrategies
  • ContextLoader.properties
  • configureAndRefreshWebApplicationContext()
  • 总结
  • XmlWebApplicationContext类的作用
相关产品与服务
容器服务
腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档