您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)利用Spring怎么實(shí)現(xiàn)一個(gè)監(jiān)聽(tīng)器功能,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。
<!-- 自定義監(jiān)聽(tīng) 啟動(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還沒(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í),
使用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); @Override public void contextDestroyed(ServletContextEvent arg0) { // TODO Auto-generated method stub } @Override public 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中使用自定義事件類型:
第一步:自定義事件類型:自定義類需要繼承Spring中org.springframework.context.ApplicationEvent類
第二步:設(shè)置事件監(jiān)聽(tīng)器,實(shí)現(xiàn)org.springframework.context.ApplicationListener<自定義事件類型>接口,重寫onApplicationEvent方法監(jiān)聽(tīng)事件源
第三步:將事件監(jiān)聽(tīng)器配置到Spring中,通過(guò)xml配置文件將事件監(jiān)聽(tīng)器配置到bean容器中
第四步:Spring容器(container容器發(fā)布事件)發(fā)布事件
自定義事件類型
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(); } }
關(guān)于利用Spring怎么實(shí)現(xiàn)一個(gè)監(jiān)聽(tīng)器功能就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。