zoukankan      html  css  js  c++  java
  • Spring常用的Listener

    一.Spring中的WebAppRootListener

      这个listner的作用就是监听web.xml中的配置para-name为webAppRootKey的值,比如我的web应用为tsts,那么我配置

    这样一个

     

    1.<context-param>

    2.        <param-name>webAppRootKey</param-name>

    3.        <param-value>tsts.root</param-value>

    4. </context-param>

    5.,然后再配置这样一个监听器:

     

    1.<listener>

    2.        <listener-class> 
    3.            org.springframework.web.util.WebAppRootListener
    4.        </listener-class>

    5.  </listener>

    。这个监听器就会在web上下文初始化的时候,调用webUtil的对应方法,首先获取到param-name对应的param-value ,然后,根据传递进去的ServletContext对象得到web的物理路径:String root = servletContext.getRealPath("/");

     

    接着把这个param-value作为key,root作为value放到system中System.setProperty(key, root);

     

    然后再web中可以用 System.get.....就可以得到web的跟目录的物理路径了。

     

     

    之前我的做法是用一个filter,在filter的init中调用String root = servletContext.getRealPath("/");,然后再去设置对应一个常量类文件的属性。做法差不多,但是spring的做法更可以借鉴!

    ------------------------------------------

    二.spring中的Log4jConfigListener

       使用spring中的Log4jConfigListener有如如下好处:
       1. 动态的改变记录级别和策略,不需要重启Web应用,如《Effective Enterprise Java》所说。
       2. 把log文件定在 /WEB-INF/logs/ 而不需要写绝对路径。
    因为 系统把web目录的路径压入一个叫webapp.root的系统变量。这样写log文件路径时不用写绝对路径了.
    log4j.appender.logfile.File=${webapp.root}/WEB-INF/logs/myfuse.log
       3. 可以把log4j.properties和其他properties一起放在/WEB-INF/ ,而不是Class-Path。
       4.log4jRefreshInterval为60000表示 开一条watchdog线程每60秒扫描一下配置文件的变化;
       在web.xml 添加 
        <context-param>
            <param-name>log4jConfigLocation</param-name>
            <param-value>WEB-INF/log4j.properties</param-value>
        </context-param>

        <context-param>
            <param-name>log4jRefreshInterval</param-name>
            <param-value>60000</param-value>
        </context-param>

        <listener>
            <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
        </listener>

    -----------------------------------

    三.ContextLoaderListener

      * Bootstrap listener to start up Spring's root WebApplicationContext.
     * Simply delegates to ContextLoader.

     * <p>This listener should be registered after Log4jConfigListener in web.xml,
     * if the latter is used.

             与BeanFactory通常以编程的方式被创建不同的是,ApplicationContext能以声明的方式创建,如使用ContextLoader。当然你也可以使用ApplicationContext的实现之一来以编程的方式创建ApplicationContext实例。首先,让我们先分析ContextLoader接口及其实现。

        ContextLoader机制有两种方式,ContextLoaderListener 和ContextLoaderServlet,他们功能相同但是listener不能在Servlet2.3容器下使用。Servlet2.4规范中servlet context listeners需要在web应用启动并能处理初始请求时立即运行。(servlet context listener关闭的时候也是相同的)。servlet context listener是初始化SpringApplicationContext理想的方式。你可能愿意选择ContextLoaderListener,虽然是一样的,但决定权在于你。你可以查看ContextLoaderServlet的Javadoc来获得更详细的信息。

    可以象下面所示例的一样使用ContextLoaderListener注册一个ApplicationContext

     

    <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/daoContext.xml /WEB-INF/applicationContext.xml</param-value>
    </context-param>

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

    <!-- or use the ContextLoaderServlet instead of the above listener
    <servlet>
      <servlet-name>context</servlet-name>
      <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
      <load-on-startup>1</load-on-startup>
    </servlet>
    --
    >

    监听器首先检查contextConfigLocation参数,如果它不存在,它将使用/WEB-INF/applicationContext.xml作为默认值。如果已存在,它将使用分隔符(逗号、冒号或空格)将字符串分解成应用上下文件位置路径。可以支持ant-风格的路径模式,如/WEB-INF/*Context.xml(WEB-INF文件夹下所有以"Context.xml"结尾的文件)。或者/WEB-INF/**/*Context.xml(WEB-INF文件夹及子文件夹下的以"Context.xml"结尾的文件)。

    ContextLoaderServletContextLoaderListener一样使用'contextConfigLocation'参数。

    -----------------------------------

    四.ServletContextListener

    ------------------------------------

    五.IntrospectorCleanupListener

      org.springframework.web.util.IntrospectorCleanupListener监听器主要负责处理由JavaBean Introspector使用而引起的缓冲泄露, 
    它是一个在web应用关闭时清除JavaBean Introspector的监听器,在web.xml中注册这个listener可以保证在web应用关闭的时候释放掉与这个web应用相关的class loader和由它管理的类. 
      如果你使用了JavaBeans Introspector来分析应用中的类.Introspector缓冲中会保留这些类的引用.结果在你的应用关闭的时候,这些类以及web应用相关的class loader没有被垃圾回收. 
      不幸的是,清除Introsepctor的唯一方法是刷新整个缓冲,这是因为我们没有办法判断哪些是属于你的应用的引用,所以删除被缓冲的Introspection会导致把这台电脑上的所有应用的introspection都删掉. 
      spring托管的bean不需要使用这个监听器.因为spring它自己的introspection所使用的缓冲在分析完一个类之后会被马上从javabeans introspector缓冲中清除掉. 
    应用程序中的类从来不直接使用JavaBeans Introspector,所以他们一般不会导致内部查看资源泄露,但是一些类库和框架往往会产生这个问题.如:Struts和Quartz.单个的内部查看泄露会导致整个的web应用的类加载器不能进行垃圾回收,在web应用关闭之后,你会看到此应用的所在静态资源如单例,这个错误不是由该类自身引起的.

     

     其他参考资料:

    1.spring优秀工具类盘点 http://shijian0306.javaeye.com/blog/166036

  • 相关阅读:
    DB2 for Z/os Statement prepare
    Foreign key (referential) constraints on DB2 LUW v105
    复制Informational constraints on LUW DB2 v105
    DB2 SQL Mixed data in character strings
    DB2 create partitioned table
    MVC中使用EF的技巧集(一)
    Asp.Net MVC 开发技巧(二)
    Linq使用技巧及查询示例(一)
    Asp.Net MVC 开发技巧(一)
    Asp.Net MVC Identity 2.2.1 使用技巧(八)
  • 原文地址:https://www.cnblogs.com/chenying99/p/2671300.html
Copyright © 2011-2022 走看看