zoukankan      html  css  js  c++  java
  • web.xml中监听器如何顺序加载

    最近用到在Tomcat服务器启动时自动加载数据到缓存,这就需要创建一个自定义的缓存监听器并实现ServletContextListener接口,

    并且在此自定义监听器中需要用到Spring的依赖注入功能.

    在web.xml文件中监听器配置如下:

    Xml代码  
    1. <listener>  
    2.         <listener-class>  
    3.             org.springframework.web.context.ContextLoaderListener  
    4.         </listener-class>  
    5. </listener
    6.  
    7. <listener>  
    8.     <listener-class>  
    9.         com.wsjiang.test.listener.CacheListener  
    10.     </listener-class>  
    11. </listener>  

          上面的配置大意为,先配置spring监听器,启动spring,再配置一个缓存监听器,

          需求:我希望他们是顺序执行

                   因为在缓存监听器中需要 spring注入其他对象的实例,我期望在服务器加载缓存监听器前加载Spring的监听器,将其优先实例化。

                   但是实际运行发现他们并不是按照配置的顺序 加载的。


          对上面的问题我查询了很多资料,找到了一种解决方案,希望能帮助遇到同类问题的朋友。
          思路就是,既然listener的顺序是不固定的,那么我们可以整合两个listener到一个类中,这样就可以让初始化的顺序固定了。我就重写了 org.springframework.web.context.ContextLoaderListener这个类的 contextInitialized方法.大致代码如下:

    Java代码  
    1. public class ContextLoaderListenerOverWrite extends ContextLoaderListener {  
    2.     private IStationService stationService;  
    3.     private IOSCache osCache;  
    4.     @Override  
    5.     /** 
    6.      * @description 重写ContextLoaderListener的contextInitialized方法 
    7.      */  
    8.     public void contextInitialized(ServletContextEvent event) {  
    9.         super.contextInitialized(event);  
    10.         ApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(event.getServletContext());  
    11.         //获取bean  
    12.         stationService = (IStationService) applicationContext.getBean("stationService");   
    13.         osCache = (IOSCache) applicationContext.getBean("osCache");  
    14.         /* 
    15.          具体地业务代码 
    16.          */  
    17.     }  
    18. }  

          web.xml的配置就由两个listener变为一个:

    Xml代码  
    1. <listener>  
    2.     <listener-class>  
    3.                  com.wsjiang.test.listener.ContextLoaderListenerOverWrite  
    4.         </listener-class>  
    5. </listener>  

           这样就能保证Spring的IOC容器先于自定义的缓存监听器初始化,在缓存监听器加载的时候就能使用依赖注入的实例.

  • 相关阅读:
    iOS js交互
    崩溃!UIAlertController 引起的崩溃
    跳转到AppStore下载app
    installshield basic msi 更新时覆盖不了上一个版本文件解决方案1
    用ILMerge.exe合并dll
    获取某几个分类下的前N条数据 mssql语句
    计算网络文件的大小
    英语 2018-12-04 2法国燃料涨价抗议活动
    英语2018-12-04
    从一个故事说起,谈谈企业应用架构的演变史
  • 原文地址:https://www.cnblogs.com/moxiaotao/p/9353776.html
Copyright © 2011-2022 走看看