Spring 實(shí)現(xiàn)自定義監(jiān)聽器案例
在一般的javaWeb項(xiàng)目中經(jīng)常有一些緩存是需要再項(xiàng)目啟動(dòng)的時(shí)候加載到內(nèi)存中,這樣就可以使用自定義的監(jiān)聽器來實(shí)現(xiàn)。
1、在web.xml中聲明
<!-- 自定義監(jiān)聽 啟動(dòng)加載系統(tǒng)參數(shù) --> <listener> <listener-class>com.cn.framework.constant.OmsConfigLoader</listener-class></listener>
2、創(chuàng)建類OmsConfigLoader 實(shí)現(xiàn)接口 ServletContextListener,項(xiàng)目啟動(dòng)的時(shí)候service還沒有注入,此時(shí)調(diào)用service的方法會(huì)報(bào)錯(cuò),因?yàn)樵趙eb容器中無論是servlet還是Filter都不是Spring容器來管理的。
listener的生命周期是web容器維護(hù)的,bean的生命周期是由Spring容器來維護(hù)的,所以在listener中使用@Resource,listener不認(rèn)識(shí),
可以溝通過如下方法來解決:使用WebApplicationContextUtils工具類,該工具類的作用是獲取到spring容器的引用,進(jìn)而獲取到我們需要的bean實(shí)例。
package com.cn.framework.constant;import javax.servlet.ServletContextEvent;import javax.servlet.ServletContextListener;import org.apache.log4j.Logger;import org.springframework.web.context.support.WebApplicationContextUtils;import com.kxs.service.systemService.ISystemService;public class OmsConfigLoader implements ServletContextListener {private static Logger LOG = Logger.getLogger(OmsConfigLoader.class);@Overridepublic void contextDestroyed(ServletContextEvent arg0) {// TODO Auto-generated method stub}@Overridepublic void contextInitialized(ServletContextEvent arg0) {LOG.info('==> 加載OMS系統(tǒng)配置信息 Start ==');try {ISystemService iSystemService = WebApplicationContextUtils.getWebApplicationContext(arg0.getServletContext()).getBean(ISystemService.class);iSystemService.refreshCache();} catch (Exception e) {e.printStackTrace();LOG.info(e.toString());}LOG.info('==> 加載OMS系統(tǒng)配置信息 End ==');}}
補(bǔ)充:Spring-xml配置自定義事件監(jiān)聽器
一、自定義事件Spring中使用自定義事件類型:
第一步:自定義事件類型:自定義類需要繼承Spring中org.springframework.context.ApplicationEvent類
第二步:設(shè)置事件監(jiān)聽器,實(shí)現(xiàn)org.springframework.context.ApplicationListener<自定義事件類型>接口,重寫onApplicationEvent方法監(jiān)聽事件源
第三步:將事件監(jiān)聽器配置到Spring中,通過xml配置文件將事件監(jiān)聽器配置到bean容器中
第四步:Spring容器(container容器發(fā)布事件)發(fā)布事件
自定義事件類型
public class RainEvent extends ApplicationEvent { private static final long serialVersionUID = 1L; public RainEvent(Object source) { super(source); } }
監(jiān)聽器:可以創(chuàng)建多個(gè)監(jiān)聽器
public class RainEventListener1 implements ApplicationListener<RainEvent> { //監(jiān)聽rainevent事件,調(diào)用當(dāng)前方法 @Override public void onApplicationEvent(RainEvent event) { Object source = event.getSource(); System.out.println('監(jiān)聽器1:'+source); }}public class RainEventListener2 implements ApplicationListener<RainEvent> { //監(jiān)聽rainevent事件,調(diào)用當(dāng)前方法 @Override public void onApplicationEvent(RainEvent event) { Object source = event.getSource(); System.out.println('監(jiān)聽器2:'+source); }}
xml配置文件將監(jiān)聽器配置到bean容器中
<!-- 配置監(jiān)聽器,向spring容器發(fā)布事件,自動(dòng)觸發(fā)監(jiān)聽器的onApplicationEvent方法 --><bean class='com.briup.ioc.event.RainEventListener1'></bean><bean class='com.briup.ioc.event.RainEventListener2'></bean>
bean容器發(fā)布事件
public void ioc_event() { try { String path = 'com/briup/ioc/event/event.xml'; ApplicationContext container = new ClassPathXmlApplicationContext(path); container.publishEvent(new RainEvent('打雷了,下雨了!')); } catch (Exception e) { e.printStackTrace(); }}
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章:
1. 基于javascript處理二進(jìn)制圖片流過程詳解2. ajax請(qǐng)求添加自定義header參數(shù)代碼3. Idea 快速生成方法返回值的操作4. Docker 部署 Prometheus的安裝詳細(xì)教程5. Gitlab CI-CD自動(dòng)化部署SpringBoot項(xiàng)目的方法步驟6. 使用Python和百度語音識(shí)別生成視頻字幕的實(shí)現(xiàn)7. idea開啟代碼提示功能的方法步驟8. idea刪除項(xiàng)目的操作方法9. JS sort方法基于數(shù)組對(duì)象屬性值排序10. JAVA上加密算法的實(shí)現(xiàn)用例
