新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)Spring教程:Spring中的自定義事件
Spring 中的自定義事件
編寫和發(fā)布自己的自定義事件有許多步驟。按照在這一章給出的說明來編寫,發(fā)布和處理自定義 Spring 事件。

創(chuàng)新互聯(lián)專業(yè)為企業(yè)提供云南網(wǎng)站建設(shè)、云南做網(wǎng)站、云南網(wǎng)站設(shè)計(jì)、云南網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計(jì)與制作、云南企業(yè)網(wǎng)站模板建站服務(wù),10年云南做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。
| 步驟 | 描述 |
|---|---|
| 1 | 創(chuàng)建一個(gè)名稱為 SpringExample 的項(xiàng)目,并且在創(chuàng)建項(xiàng)目的 src 文件夾中創(chuàng)建一個(gè)包 com.tutorialspoint。 |
| 2 | 使用 Add External JARs 選項(xiàng),添加所需的 Spring 庫,解釋見 Spring Hello World Example 章節(jié)。 |
| 3 | 通過擴(kuò)展 ApplicationEvent,創(chuàng)建一個(gè)事件類 CustomEvent。這個(gè)類必須定義一個(gè)默認(rèn)的構(gòu)造函數(shù),它應(yīng)該從 ApplicationEvent 類中繼承的構(gòu)造函數(shù)。 |
| 4 | 一旦定義事件類,你可以從任何類中發(fā)布它,假定 EventClassPublisher 實(shí)現(xiàn)了 ApplicationEventPublisherAware。你還需要在 XML 配置文件中聲明這個(gè)類作為一個(gè) bean,之所以容器可以識(shí)別 bean 作為事件發(fā)布者,是因?yàn)樗鼘?shí)現(xiàn)了 ApplicationEventPublisherAware 接口。 |
| 5 | 發(fā)布的事件可以在一個(gè)類中被處理,假定 EventClassHandler 實(shí)現(xiàn)了 ApplicationListener 接口,而且實(shí)現(xiàn)了自定義事件的 onApplicationEvent 方法。 |
| 6 | 在 src 文件夾中創(chuàng)建 bean 的配置文件 Beans.xml 和 MainApp 類,它可以作為一個(gè) Spring 應(yīng)用程序來運(yùn)行。 |
| 7 | 最后一步是創(chuàng)建的所有 Java 文件和 Bean 配置文件的內(nèi)容,并運(yùn)行應(yīng)用程序,解釋如下所示。 |
這個(gè)是 CustomEvent.java 文件的內(nèi)容:
package com.tutorialspoint;
import org.springframework.context.ApplicationEvent;
public class CustomEvent extends ApplicationEvent{
public CustomEvent(Object source) {
super(source);
}
public String toString(){
return "My Custom Event";
}
}
下面是 CustomEventPublisher.java 文件的內(nèi)容:
package com.tutorialspoint;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
public class CustomEventPublisher
implements ApplicationEventPublisherAware {
private ApplicationEventPublisher publisher;
public void setApplicationEventPublisher
(ApplicationEventPublisher publisher){
this.publisher = publisher;
}
public void publish() {
CustomEvent ce = new CustomEvent(this);
publisher.publishEvent(ce);
}
}
下面是 CustomEventHandler.java 文件的內(nèi)容:
package com.tutorialspoint;
import org.springframework.context.ApplicationListener;
public class CustomEventHandler
implements ApplicationListener{
public void onApplicationEvent(CustomEvent event) {
System.out.println(event.toString());
}
}
下面是 MainApp.java 文件的內(nèi)容:
package com.tutorialspoint;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {
public static void main(String[] args) {
ConfigurableApplicationContext context =
new ClassPathXmlApplicationContext("Beans.xml");
CustomEventPublisher cvp =
(CustomEventPublisher) context.getBean("customEventPublisher");
cvp.publish();
cvp.publish();
}
}
下面是配置文件 Beans.xml:
一旦你完成了創(chuàng)建源和 bean 的配置文件后,我們就可以運(yùn)行該應(yīng)用程序。如果你的應(yīng)用程序一切都正常,將輸出以下信息:
My Custom Event
My Custom Event 分享標(biāo)題:創(chuàng)新互聯(lián)Spring教程:Spring中的自定義事件
分享鏈接:http://m.fisionsoft.com.cn/article/djjjihd.html


咨詢
建站咨詢
