這篇文章將為大家詳細(xì)講解有關(guān)spring中怎么監(jiān)聽ApplicationEvent事件現(xiàn),文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。
創(chuàng)新互聯(lián)公司專注于昌吉網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠為您提供昌吉營銷型網(wǎng)站建設(shè),昌吉網(wǎng)站制作、昌吉網(wǎng)頁設(shè)計(jì)、昌吉網(wǎng)站官網(wǎng)定制、微信小程序開發(fā)服務(wù),打造昌吉網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供昌吉網(wǎng)站排名全網(wǎng)營銷落地服務(wù)。
package com.test.eventListener; import org.springframework.context.ApplicationEvent; /** * [@author](https://my.oschina.net/arthor) admin * [@date](https://my.oschina.net/u/2504391) 2018/5/17 17:37 * 新建StudentAddEvent類,實(shí)現(xiàn)抽象類org.springframework.context.ApplicationEvent * StudentAddEvent類中需要實(shí)現(xiàn)自己的構(gòu)造函數(shù) * 增加學(xué)生監(jiān)聽事件 */ public class StudentAddEvent extends ApplicationEvent { private static final long serialVersionUID = 20L; /** * 學(xué)生姓名 */ private String name; /** * [@param](https://my.oschina.net/u/2303379) source */ public StudentAddEvent(Object source, String name) { super(source); this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
package com.test.eventListener; import org.springframework.context.ApplicationEvent; import org.springframework.context.ApplicationListener; import org.springframework.stereotype.Component; /** * [@author](https://my.oschina.net/arthor) admin * 新建StudentAddListener類,實(shí)現(xiàn)接口org.springframework.context.ApplicationListener中的onApplicationEvent方法, * 在該方法中只處理StudentAddEvent類型的ApplicationEvent事件 * 定義StudentAddListener監(jiān)聽器 */ [@Component](https://my.oschina.net/u/3907912) public class StudentAddListener implements ApplicationListener { public void onApplicationEvent(ApplicationEvent event) { // 1.判斷是否是增加學(xué)生對(duì)象的事件 if (!(event instanceof StudentAddEvent)) { return; } // 2.是增加學(xué)生事件的對(duì)象,進(jìn)行邏輯處理,比如記日志、積分等 StudentAddEvent studentAddEvent = (StudentAddEvent) event; System.out.println("增加了學(xué)生:" + studentAddEvent.getName()); } }
package com.test.eventListener; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.stereotype.Component; /** * @author admin * 定義StudentAddBean觸發(fā)StudentAddEvent事件 * 新建StudentAddBean類,實(shí)現(xiàn)接口 org.springframework.context.ApplicationContextAware中的setApplicationContext方法, * 在構(gòu)造bean的時(shí)候注入Spring的上下文對(duì)象,以便通過Spring上下文對(duì)象的publishEvent方法來觸發(fā)StudentAddEvent事件 */ @Component public class StudentAddBean implements ApplicationContextAware { /** * 定義Spring上下文對(duì)象 */ private ApplicationContext applicationContext = null; /* * (non-Javadoc) * * @see * org.springframework.context.ApplicationContextAware#setApplicationContext * (org.springframework.context.ApplicationContext) */ public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { this.applicationContext = applicationContext; } /** * 增加一個(gè)學(xué)生 * * @param studentName */ public void addStudent(String studentName) { // 1.構(gòu)造一個(gè)增加學(xué)生的事件 StudentAddEvent aStudentEvent = new StudentAddEvent( applicationContext, studentName); // 2.觸發(fā)增加學(xué)生事件 applicationContext.publishEvent(aStudentEvent); } }
package com.test.eventListener; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * @author admin * ApplicationContext在運(yùn)行期會(huì)自動(dòng)檢測到所有實(shí)現(xiàn)了ApplicationListener的bean對(duì)象,并將其作為事件接收對(duì)象。 * 當(dāng)ApplicationContext的publishEvent方法被觸發(fā)時(shí),每個(gè)實(shí)現(xiàn)了ApplicationListener接口的bean都會(huì)收到ApplicationEvent對(duì)象, * 每個(gè)ApplicationListener可根據(jù)事件類型只接收處理自己感興趣的事件,比如上面的StudentAddListener只接收StudentAddEvent事件。 */ public class EventListenerTest { public static void main(String[] args) { String[] xmlConfig = new String[] { "spring/spring.xml" }; // 使用ApplicationContext來初始化系統(tǒng) ApplicationContext context = new ClassPathXmlApplicationContext(xmlConfig); StudentAddBean studentBean = (StudentAddBean) context.getBean("studentAddBean"); studentBean.addStudent("張三"); studentBean.addStudent("李四"); } }
關(guān)于spring中怎么監(jiān)聽ApplicationEvent事件現(xiàn)就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。
文章標(biāo)題:spring中怎么監(jiān)聽ApplicationEvent事件現(xiàn)
本文來源:http://muchs.cn/article34/jpeose.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供服務(wù)器托管、網(wǎng)頁設(shè)計(jì)公司、網(wǎng)站設(shè)計(jì)公司、域名注冊(cè)、響應(yīng)式網(wǎng)站、網(wǎng)站營銷
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)