Spring 實(shí)現(xiàn)自定義監(jiān)聽(tīng)器案例
在一般的javaWeb項(xiàng)目中經(jīng)常有一些緩存是需要再項(xiàng)目啟動(dòng)的時(shí)候加載到內(nèi)存中,這樣就可以使用自定義的監(jiān)聽(tīng)器來(lái)實(shí)現(xiàn)。
1、在web.xml中聲明
<!-- 自定義監(jiān)聽(tīng) 啟動(dòng)加載系統(tǒng)參數(shù) --> <listener> <listener-class>com.cn.framework.constant.OmsConfigLoader</listener-class></listener>
2、創(chuàng)建類(lèi)OmsConfigLoader 實(shí)現(xiàn)接口 ServletContextListener,項(xiàng)目啟動(dòng)的時(shí)候service還沒(méi)有注入,此時(shí)調(diào)用service的方法會(huì)報(bào)錯(cuò),因?yàn)樵趙eb容器中無(wú)論是servlet還是Filter都不是Spring容器來(lái)管理的。
listener的生命周期是web容器維護(hù)的,bean的生命周期是由Spring容器來(lái)維護(hù)的,所以在listener中使用@Resource,listener不認(rèn)識(shí),
可以溝通過(guò)如下方法來(lái)解決:使用WebApplicationContextUtils工具類(lèi),該工具類(lèi)的作用是獲取到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)聽(tīng)器
一、自定義事件Spring中使用自定義事件類(lèi)型:
第一步:自定義事件類(lèi)型:自定義類(lèi)需要繼承Spring中org.springframework.context.ApplicationEvent類(lèi)
第二步:設(shè)置事件監(jiān)聽(tīng)器,實(shí)現(xiàn)org.springframework.context.ApplicationListener<自定義事件類(lèi)型>接口,重寫(xiě)onApplicationEvent方法監(jiān)聽(tīng)事件源
第三步:將事件監(jiān)聽(tīng)器配置到Spring中,通過(guò)xml配置文件將事件監(jiān)聽(tīng)器配置到bean容器中
第四步:Spring容器(container容器發(fā)布事件)發(fā)布事件
自定義事件類(lèi)型
public class RainEvent extends ApplicationEvent { private static final long serialVersionUID = 1L; public RainEvent(Object source) { super(source); } }
監(jiān)聽(tīng)器:可以創(chuàng)建多個(gè)監(jiān)聽(tīng)器
public class RainEventListener1 implements ApplicationListener<RainEvent> { //監(jiān)聽(tīng)rainevent事件,調(diào)用當(dāng)前方法 @Override public void onApplicationEvent(RainEvent event) { Object source = event.getSource(); System.out.println('監(jiān)聽(tīng)器1:'+source); }}public class RainEventListener2 implements ApplicationListener<RainEvent> { //監(jiān)聽(tīng)rainevent事件,調(diào)用當(dāng)前方法 @Override public void onApplicationEvent(RainEvent event) { Object source = event.getSource(); System.out.println('監(jiān)聽(tīng)器2:'+source); }}
xml配置文件將監(jiān)聽(tīng)器配置到bean容器中
<!-- 配置監(jiān)聽(tīng)器,向spring容器發(fā)布事件,自動(dòng)觸發(fā)監(jiān)聽(tīng)器的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 xml xsl取值及數(shù)據(jù)修改第1/2頁(yè)2. 利用CSS制作3D動(dòng)畫(huà)3. Properties 持久的屬性集的實(shí)例詳解4. jsp文件下載功能實(shí)現(xiàn)代碼5. XML入門(mén)的常見(jiàn)問(wèn)題(四)6. WML語(yǔ)言的基本情況7. php網(wǎng)絡(luò)安全中命令執(zhí)行漏洞的產(chǎn)生及本質(zhì)探究8. ASP基礎(chǔ)入門(mén)第八篇(ASP內(nèi)建對(duì)象Application和Session)9. HTML5 Canvas繪制圖形從入門(mén)到精通10. CSS代碼檢查工具stylelint的使用方法詳解
