Spring mvc ContextLoaderListener 原理解析

对于熟悉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信息。


想了解更多精彩内容请关注我的公众号

本人简书blog地址:http://www.jianshu.com/u/1f0067e24ff8    
点击这里快速进入简书

GIT地址:http://git.oschina.net/brucekankan/
点击这里快速进入GIT

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值