Web容器之Listener初始化顺序
2016-07-12 15:54
232 查看
在Web容器启动时,web.xml的加载顺序一般是这样的,挑选几个常用属性来说,顺序如下
context-param ->listener -> filter -> servlet
而当我们想要在Listener初始化一些数据的时候,势必要把初始化的逻辑代码写在listener里面。
在同一个web.xml里面写两个listener,它们的初始化顺序是不根据你写在web.xml里面的顺序而定的,就相当于说你要想在自定义Listener使用spring管理的bean那就会得到一个null值。
那这个时候就要手动的去控制好listener的顺序。那如果不要使用spring管理的bean那也就没有这个必要去控制顺序。
方法肯定是有的,可以采用自定义的listener但这个listener不要去去实现ServletContextListener,而是extendsorg.springframework.web.context.ContextLoaderListener。OverridecontextInitialized(ServletContextEventevent)方法,在这个Override方法里面控制好执行顺序就可以了。
最近用到在Tomcat服务器启动时自动加载数据到缓存,这就需要创建一个自定义的缓存监听器并实现ServletContextListener接口,并且在此自定义监听器中需要用到Spring的依赖注入功能.在web.xml文件中监听器配置如下:
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<listener>
<listener-class>
com.wsjiang.test.listener.CacheListener
</listener-class>
</listener>
上面的配置细细大意为,先配置spring监听器,启动spring,再配置一个缓存监听器,我希望他们是顺序执行的,因为在缓存监听器中需要 spring注入其他对象的实例,我期望在服务器加载缓存监听器前加载Spring的监听器,将其优先实例化。但是实际运行发现他们并不是按照配置的顺序加载的。
对上面的问题我查询了很多资料,找到了一种解决方案,希望能帮助遇到同类问题的朋友。
思路就是,既然listener的顺序是不固定的,那么我们可以整合两个listener到一个类中,这样就可以让初始化的顺序固定了。我就重写了org.springframework.web.context.ContextLoaderListener这个类的contextInitialized方法.大致代码如下:
web.xml的配置就由两个listener变为一个:
<listener>
<listener-class>
com.wsjiang.test.listener.ContextLoaderListenerOverWrite
</listener-class>
</listener>
这样就能保证Spring的IOC容器先于自定义的缓存监听器初始化,在缓存监听器加载的时候就能使用依赖注入的实例.
我还是一新手,如果哪位大侠有其他方案,望指点,谢谢!
参考链接:
1:http://blog.sina.com.cn/s/blog_67d771b80101buxq.html
2:http://wsjiang.iteye.com/blog/1127304
3:http://blog.csdn.net/dfdsggdgg/article/details/48713481#
context-param ->listener -> filter -> servlet
而当我们想要在Listener初始化一些数据的时候,势必要把初始化的逻辑代码写在listener里面。
在同一个web.xml里面写两个listener,它们的初始化顺序是不根据你写在web.xml里面的顺序而定的,就相当于说你要想在自定义Listener使用spring管理的bean那就会得到一个null值。
那这个时候就要手动的去控制好listener的顺序。那如果不要使用spring管理的bean那也就没有这个必要去控制顺序。
方法肯定是有的,可以采用自定义的listener但这个listener不要去去实现ServletContextListener,而是extendsorg.springframework.web.context.ContextLoaderListener。OverridecontextInitialized(ServletContextEventevent)方法,在这个Override方法里面控制好执行顺序就可以了。
最近用到在Tomcat服务器启动时自动加载数据到缓存,这就需要创建一个自定义的缓存监听器并实现ServletContextListener接口,并且在此自定义监听器中需要用到Spring的依赖注入功能.在web.xml文件中监听器配置如下:
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<listener>
<listener-class>
com.wsjiang.test.listener.CacheListener
</listener-class>
</listener>
上面的配置细细大意为,先配置spring监听器,启动spring,再配置一个缓存监听器,我希望他们是顺序执行的,因为在缓存监听器中需要 spring注入其他对象的实例,我期望在服务器加载缓存监听器前加载Spring的监听器,将其优先实例化。但是实际运行发现他们并不是按照配置的顺序加载的。
对上面的问题我查询了很多资料,找到了一种解决方案,希望能帮助遇到同类问题的朋友。
思路就是,既然listener的顺序是不固定的,那么我们可以整合两个listener到一个类中,这样就可以让初始化的顺序固定了。我就重写了org.springframework.web.context.ContextLoaderListener这个类的contextInitialized方法.大致代码如下:
public class ContextLoaderListenerOverWrite extends ContextLoaderListener { private IStationService stationService; private IOSCache osCache; @Override /** * @description 重写ContextLoaderListener的contextInitialized方法 */ public void contextInitialized(ServletContextEvent event) { super.contextInitialized(event); ApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(event.getServletContext()); //获取bean stationService = (IStationService) applicationContext.getBean("stationService"); osCache = (IOSCache) applicationContext.getBean("osCache"); /* 具体地业务代码 */ } }
web.xml的配置就由两个listener变为一个:
<listener>
<listener-class>
com.wsjiang.test.listener.ContextLoaderListenerOverWrite
</listener-class>
</listener>
这样就能保证Spring的IOC容器先于自定义的缓存监听器初始化,在缓存监听器加载的时候就能使用依赖注入的实例.
我还是一新手,如果哪位大侠有其他方案,望指点,谢谢!
参考链接:
1:http://blog.sina.com.cn/s/blog_67d771b80101buxq.html
2:http://wsjiang.iteye.com/blog/1127304
3:http://blog.csdn.net/dfdsggdgg/article/details/48713481#
相关文章推荐
- Android 组件动画
- LeetCode-233.Number of Digit One
- HDU 1007 Quoit Design(最近点对问题:分治)
- 设计模式之策略模式
- 新手入门:史上最全Web端即时通讯技术原理详解
- 数据库复制 Nacicate Premium
- 脚本语言
- oralce 角色和概要文件
- php知识点3-HTTP 头部-Accept与Content-Type的区别
- DLX专题
- 使用JVisualVM远程监控Tomcat
- ASP.NET 第一课后小结
- android -- 蓝牙 bluetooth (二) 打开蓝牙
- eclipse配置hadoop开发环境并运行WordCount小程序
- 禁止ViewPager的左右滑动事件
- 质数/素数 和筛选
- 构造函数
- 什么是 RegExp?
- 新手入门:史上最全Web端即时通讯技术原理详解
- php知识点3-HTTP 头部-Connection