zoukankan      html  css  js  c++  java
  • Spring :监听器ContextLoaderListener的作用

    Spring :监听器ContextLoaderListener的作用

    开始使用Spring读取配置文件ApplicationContext.xml的时候没有配置监听器,在web.ml中配置如下:


    <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
    </context-param>

    但是在Tomcat启动的时候日志中显示如下:
    信息: No Spring WebApplicationInitializer types detected on classpath​​

    也就是Spring没有配置文件,导致后面访问的时候发生错误。

    后来查找之下是发现没有在web.xml中使用ContextLoaderListener监听器。设置监听器


    <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    启动tomcat之后日志如下:
    2014-1-9 16:42:24 org.apache.catalina.core.ApplicationContext log
    信息: Initializing Spring root WebApplicationContext

    Spring提供ServletContentListener的一个实现类ContextLoaderListener监听器,该类可以作为Listener使用,在启动Tomcat

    容器的时候,该类的作用就是自动装载ApplicationContext的配置信息,如果没有设置contextConfigLocation的初始参数则会

    使用默认参数WEB-INF路径下的application.xml文件。如果需要自定义读取多个配置文件或者修改默认路径,则可以在web.xml

    中设置:


    <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
    </context-param>

    ContextLoaderListener会读取这些XML文件并产生 WebApplicationContext对象,然后将这个对象放置在ServletContext的属性

    里,这样我们只要可以得到Servlet就可 以得到WebApplicationContext对象,并利用这个对象访问spring 容器管理的bean。

    初始成功之后显示如下

    信息: Initializing Spring root WebApplicationContext
    ---------------------
    作者:java_wliang
    来源:CSDN
    原文:https://blog.csdn.net/java_wliang/article/details/18044507
    版权声明:本文为博主原创文章,转载请附上博文链接!

  • 相关阅读:
    新的知识点来了-ES6 Proxy代理 和 去银行存款有什么关系?
    JavaScript中一种全新的数据类型-symbol
    箭头函数的this指向问题-一看就懂
    ES6中对函数的扩展
    ES6中对象的扩展
    ES6中对数组的扩展
    ES6中对数值的扩展
    UWP --- Display Content 显示基础内容
    ViewBag & ViewData
    Bubble Sort冒泡排序
  • 原文地址:https://www.cnblogs.com/handsome1013/p/10422467.html
Copyright © 2011-2022 走看看