這篇文章主要介紹“Spring事件監(jiān)聽(tīng)機(jī)制主要涉及到的核心類(lèi)和接口有哪些”,在日常操作中,相信很多人在Spring事件監(jiān)聽(tīng)機(jī)制主要涉及到的核心類(lèi)和接口有哪些問(wèn)題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”Spring事件監(jiān)聽(tīng)機(jī)制主要涉及到的核心類(lèi)和接口有哪些”的疑惑有所幫助!接下來(lái),請(qǐng)跟著小編一起來(lái)學(xué)習(xí)吧!
為原州等地區(qū)用戶提供了全套網(wǎng)頁(yè)設(shè)計(jì)制作服務(wù),及原州網(wǎng)站建設(shè)行業(yè)解決方案。主營(yíng)業(yè)務(wù)為網(wǎng)站建設(shè)、網(wǎng)站制作、原州網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專(zhuān)業(yè)、用心的態(tài)度為用戶提供真誠(chéng)的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會(huì)得到認(rèn)可,從而選擇與我們長(zhǎng)期合作。這樣,我們也可以走得更遠(yuǎn)!
其實(shí)在 Spring/ Spring Boot 框架中有一套事件監(jiān)聽(tīng)機(jī)制,可以實(shí)現(xiàn)觀察者模式。
Spring/ Spring Boot 框架中也都內(nèi)置了許多事件,我們也可以自定義發(fā)布應(yīng)用程序事件,下面我們會(huì)介紹。
其主要涉及到的幾個(gè)核心類(lèi)和接口如下 :
ApplicationEvent(應(yīng)用程序事件)它是一個(gè)抽象類(lèi),相當(dāng)于觀察者模式中的觀察目標(biāo)。
ApplicationEvent 源碼如下:
public abstract class ApplicationEvent extends EventObject { /** use serialVersionUID from Spring 1.2 for interoperability. */ private static final long serialVersionUID = 7099057708183571937L; /** System time when the event happened. */ private final long timestamp; /** * Create a new {@code ApplicationEvent}. * @param source the object on which the event initially occurred or with * which the event is associated (never {@code null}) */ public ApplicationEvent(Object source) { super(source); this.timestamp = System.currentTimeMillis(); } /** * Return the system time in milliseconds when the event occurred. */ public final long getTimestamp() { return this.timestamp; } }
ApplicationEvent 繼承自 Java 中的 EventObject 事件對(duì)象類(lèi),Spring 框架中的所有事件都繼承自 ApplicationEvent 類(lèi),它是所有事件的父類(lèi)。
ApplicationEvent 主要的核心是類(lèi)構(gòu)造器,它可以初始化一個(gè) source 事件關(guān)聯(lián)對(duì)象,以便在事件監(jiān)聽(tīng)器中獲取并通知更新。
ApplicationListener(應(yīng)用程序事件監(jiān)聽(tīng)器)它是一個(gè)接口,相當(dāng)于觀察者模式中的觀察者。
ApplicationListener 源碼如下:
public interface ApplicationListener<E extends ApplicationEvent> extends EventListener { /** * Handle an application event. * @param event the event to respond to */ void onApplicationEvent(E event); }
ApplicationListener 繼承自 Java 中的 EventListener 事件監(jiān)聽(tīng)接口,ApplicationListener 類(lèi)中只有一個(gè) onApplicationEvent 方法,當(dāng)指定監(jiān)聽(tīng)的事件被發(fā)布后就會(huì)被觸發(fā)執(zhí)行,可以通過(guò) event 獲取事件中的關(guān)聯(lián)對(duì)象。
應(yīng)用程序事件發(fā)布接口,封裝了事件發(fā)布功能的基礎(chǔ)接口。
public interface ApplicationEventPublisher { /** * Notify all <strong>matching</strong> listeners registered with this * application of an application event. Events may be framework events * (such as ContextRefreshedEvent) or application-specific events. * <p>Such an event publication step is effectively a hand-off to the * multicaster and does not imply synchronous/asynchronous execution * or even immediate execution at all. Event listeners are encouraged * to be as efficient as possible, individually using asynchronous * execution for longer-running and potentially blocking operations. * @param event the event to publish * @see #publishEvent(Object) * @see org.springframework.context.event.ContextRefreshedEvent * @see org.springframework.context.event.ContextClosedEvent */ default void publishEvent(ApplicationEvent event) { publishEvent((Object) event); } /** * Notify all <strong>matching</strong> listeners registered with this * application of an event. * <p>If the specified {@code event} is not an {@link ApplicationEvent}, * it is wrapped in a {@link PayloadApplicationEvent}. * <p>Such an event publication step is effectively a hand-off to the * multicaster and does not imply synchronous/asynchronous execution * or even immediate execution at all. Event listeners are encouraged * to be as efficient as possible, individually using asynchronous * execution for longer-running and potentially blocking operations. * @param event the event to publish * @since 4.2 * @see #publishEvent(ApplicationEvent) * @see PayloadApplicationEvent */ void publishEvent(Object event); }
ApplicationEventPublisher 有一個(gè)默認(rèn)接口方法和接口方法,接口方法需要由具體的子類(lèi)容器實(shí)現(xiàn)。
ApplicationContext 這個(gè)類(lèi)就再熟悉不過(guò)了,它是 Spring 框架中的核心容器。
如下圖所示,ApplicationContext 接口繼承了 ApplicationEventPublisher 接口,所以常用的 ApplicationContext 就可以用來(lái)發(fā)布事件。
以上介紹的 Spring 事件監(jiān)聽(tīng)發(fā)布角色串起來(lái)就是,通過(guò) ApplicationEventPublisher 或者 ApplicationContext 容器發(fā)布 ApplicationEvent 事件并關(guān)聯(lián)事件對(duì)象,然后 ApplicationListener 監(jiān)聽(tīng)該事件,當(dāng)事件發(fā)布后,監(jiān)聽(tīng)器就會(huì)收?qǐng)?zhí)行并獲取到事件及關(guān)聯(lián)對(duì)象。
搞懂了 Spring 框架中的事件和監(jiān)聽(tīng)機(jī)制,那我們還是以上篇中觀察者模式的例子來(lái)改造下。
Spring Boot 基礎(chǔ)性的知識(shí)和搭建過(guò)程就不介紹了,不熟悉的可以關(guān)注公眾號(hào)Java技術(shù)棧,在后臺(tái)回復(fù)關(guān)鍵字 "boot" 閱讀我之前寫(xiě)的系列教程。
所有 Spring Boot 教程實(shí)戰(zhàn)源碼在下面?zhèn)€倉(cāng)庫(kù):
https://github.com/javastacks/spring-boot-best-practice
import lombok.Getter; import org.springframework.context.ApplicationEvent; /** * 觀察目標(biāo):棧長(zhǎng) * 來(lái)源微信公眾號(hào):Java技術(shù)棧 */ @Getter public class JavaStackEvent extends ApplicationEvent { /** * Create a new {@code ApplicationEvent}. * * @param source the object on which the event initially occurred or with * which the event is associated (never {@code null}) */ public JavaStackEvent(Object source) { super(source); } }
實(shí)現(xiàn) Spring 框架中的 ApplicationEvent 應(yīng)用程序事件接口,相當(dāng)于是一個(gè)觀察者目標(biāo)。
import lombok.NonNull; import lombok.RequiredArgsConstructor; import org.springframework.context.ApplicationListener; import org.springframework.scheduling.annotation.Async; /** * 觀察者:讀者粉絲 * 來(lái)源微信公眾號(hào):Java技術(shù)棧 */ @RequiredArgsConstructor public class ReaderListener implements ApplicationListener<JavaStackEvent> { @NonNull private String name; private String article; @Async @Override public void onApplicationEvent(JavaStackEvent event) { // 更新文章 updateArticle(event); } private void updateArticle(JavaStackEvent event) { this.article = (String) event.getSource(); System.out.printf("我是讀者:%s,文章已更新為:%s\n", this.name, this.article); } }
實(shí)現(xiàn) Spring 框架中的 ApplicationListener 應(yīng)用監(jiān)聽(tīng)接口,相當(dāng)于是觀察者。
import lombok.extern.slf4j.Slf4j; import org.springframework.boot.CommandLineRunner; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Slf4j @Configuration public class ObserverConfiguration { @Bean public CommandLineRunner commandLineRunner(ApplicationContext context) { return (args) -> { log.info("發(fā)布事件:什么是觀察者模式?"); context.publishEvent(new JavaStackEvent("什么是觀察者模式?")); }; } @Bean public ReaderListener readerListener1(){ return new ReaderListener("小明"); } @Bean public ReaderListener readerListener2(){ return new ReaderListener("小張"); } @Bean public ReaderListener readerListener3(){ return new ReaderListener("小愛(ài)"); } }
在 Spring 配置中創(chuàng)建了三個(gè)讀者 Bean,在 Spring Boot 啟動(dòng)后發(fā)布一個(gè)觀察者模式事件,然后這三個(gè) Bean 就會(huì)收到通知。
輸出結(jié)果:
這里每個(gè)讀者創(chuàng)建一個(gè) Bean 可能不太合適,因?yàn)橐7律弦粋€(gè)觀察者模式的應(yīng)用。
實(shí)際中的觀察者模式應(yīng)用應(yīng)該是指具體業(yè)務(wù),舉例說(shuō)一個(gè)電商支付場(chǎng)景,在用戶支付完后可以發(fā)布一個(gè)支付事件,然后會(huì)有扣減積分,短信通知、贈(zèng)送優(yōu)惠券等一系列后續(xù)的事件監(jiān)聽(tīng)器觀察者,這樣可以實(shí)現(xiàn)業(yè)務(wù)解耦,這是一種很典型的應(yīng)用場(chǎng)景。
到此,關(guān)于“Spring事件監(jiān)聽(tīng)機(jī)制主要涉及到的核心類(lèi)和接口有哪些”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)?lái)更多實(shí)用的文章!
文章題目:Spring事件監(jiān)聽(tīng)機(jī)制主要涉及到的核心類(lèi)和接口有哪些
本文來(lái)源:http://jinyejixie.com/article16/ppeodg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供全網(wǎng)營(yíng)銷(xiāo)推廣、手機(jī)網(wǎng)站建設(shè)、建站公司、虛擬主機(jī)、外貿(mào)網(wǎng)站建設(shè)、關(guān)鍵詞優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
網(wǎng)頁(yè)設(shè)計(jì)公司知識(shí)