Spring源代码-ContextLoaderListener的学习

本文介绍了Spring在web应用程序中的启用,特别是在web.xml中配置ContextLoaderListener的角色。ContextLoaderListener的官方注释强调了配置顺序的重要性,该监听器在应用启动时加载Spring服务。此外,文章还探讨了ContextLoaderListener的结构,包括初始化和关闭web上下文的方法。最后,总结了web.xml中listener、servlet、filter的加载顺序,并指出Log4jConfigListener应先于ContextLoaderListener加载。
摘要由CSDN通过智能技术生成

web.xml中启用Spring

在一般的web应用程序,我们倘若用到Spring的话,需要在web.xml中配置以下的信息来使一些容器,例如TomcatJetty等来加载Spring

    <!-- Spring配置文件信息 -->
    <context-param>
    	<!-- 这个contextConfigLocation参数是用来加载spring相关配置的标签 -->
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:com/conf/applicationContext.xml</param-value>
    </context-param>
    <!-- ContextLoaderListener监听器 -->
    <!-- ContextLoaderListener的作用就是启动Web容器时,自动装配
		ApplicationContext.xml的配置信息 
	-->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

Spring主要通过ContextLoaderListener类在应用启动时加载其服务

 

ContextLoaderListener的官方注释

查看某个类的重要功能最好是观察其的注释,ContextLoaderLinstener官方注释如下:

//父级启动类,可用ContextLoader启动和ContextCleanupListener来关闭Spring的根web应用上下文
  Bootstrap listener to start up and shut down Spring's root {@link WebApplicationContext}.
  Simply delegates to {@link ContextLoader} as well as to {@link ContextCleanupListener}.
 
  //这里给出了提示,如果需要用到自定义log4j的配置的话,则ContextListener需要在Log4jConfigListener之后
  <p>This listener should be registered after {@link org.springframework.web.util.Log4jConfigListener}
  in {@code web.xml}, if the latter is used.
 //从Spring3.1之后,注入根web应用上下文可通过 WebApplicationInitializer,容器在启动会加载此接口,但是有个要求是容器的Servlet版本必须是3.0+,对Tomcat来说必须是7.0.15版本以上
  <p>As of Spring 3.1, {@code ContextLoaderListener} supports injecting the root web
  application context via the {@link #ContextLoaderListener(WebApplicationContext)}
  constructor, allowing for programmatic configuration in Servlet 3.0+ environments.
  See {@link org.springframework.web.WebApplicationInitializer} for usage examples.
 
  @author Juergen Hoeller
  @author Chris Beams
  @since 17.02.2003
  @see #setContextInitializers
  @see org.springframework.web.WebApplicationInitializer
  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值