日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

java 自定义监听_Spring 中的自定义事件

發布時間:2025/3/15 javascript 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java 自定义监听_Spring 中的自定义事件 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Spring 中的自定義事件

編寫和發布自己的自定義事件有許多步驟。按照在這一章給出的說明來編寫,發布和處理自定義 Spring 事件。

步驟

描述

1

創建一個名稱為SpringExample的項目,并且在創建項目的src文件夾中創建一個包com.tutorialspoint。

2

使用AddExternalJARs選項,添加所需的Spring庫,解釋見SpringHelloWorldExample章節。

3

通過擴展ApplicationEvent,創建一個事件類CustomEvent。這個類必須定義一個默認的構造函數,它應該從ApplicationEvent類中繼承的構造函數。

4

一旦定義事件類,你可以從任何類中發布它,假定EventClassPublisher實現了ApplicationEventPublisherAware。你還需要在XML配置文件中聲明這個類作為一個bean,之所以容器可以識別bean作為事件發布者,是因為它實現了ApplicationEventPublisherAware接口。

5

發布的事件可以在一個類中被處理,假定EventClassHandler實現了ApplicationListener接口,而且實現了自定義事件的onApplicationEvent方法。

6

在src文件夾中創建bean的配置文件Beans.xml和MainApp類,它可以作為一個Spring應用程序來運行。

7

最后一步是創建的所有Java文件和Bean配置文件的內容,并運行應用程序,解釋如下所示。

這個是 CustomEvent.java 文件的內容:

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 文件的內容:

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 文件的內容:

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 文件的內容:

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:

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

class="com.tutorialspoint.CustomEventHandler"/>

class="com.tutorialspoint.CustomEventPublisher"/>

一旦你完成了創建源和 bean 的配置文件后,我們就可以運行該應用程序。如果你的應用程序一切都正常,將輸出以下信息:

My Custom Event

My Custom Event

總結

以上是生活随笔為你收集整理的java 自定义监听_Spring 中的自定义事件的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。