spring監聽器demo
歡迎進入《一起學spring》系列博文第三篇,
spring容器的事件監聽機制,同樣有事件、事件源和監聽者。而spring中的事件需要繼承ApplicationEvent,監聽者需要繼承ApplicationListener。其他的基本和普通的事件監聽差不多。我們用示例說話!
1、這是spring的ApplicationEvent類的原始碼,我們可以看到它繼承了JDK中的EventObject,EventObject中只有一個Object型別的source屬性以及一個getSource方法。下面這個ApplicationEvent類只是增加了一個時間戳的屬性以及getTimeStamp的方法。
- package org.springframework.context;
- import java.util.EventObject;
- /**
- * Class to be extended by all application events. Abstract as it
- * doesn't make sense for generic events to be published directly.
- *
- * @author Rod Johnson
- * @author Juergen Hoeller
- */
- 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 ApplicationEvent.
- * @param source the component that published the event (never {@code null})
- */
- public ApplicationEvent(Object source) {
- super(source);
- this.timestamp = System.currentTimeMillis();
- }
- /**
- * Return the system time in milliseconds when the event happened.
- */
- public final long getTimestamp() {
- return this.timestamp;
- }
- }
2、spring中的ApplicationListener原始碼;我們可以清楚地知道,這個類和我們自定義監聽器中的監聽器類基本一樣。
- public interface ApplicationListener<E extends ApplicationEvent> extends EventListener {
- /**
- * Handle an application event.
- * @param event the event to respond to
- */
- void onApplicationEvent(E event);
- }
下面回到我們的程式碼:
3、我的事件
- package com.huai.listener;
- import org.springframework.context.ApplicationEvent;
- public class MyEvent extends ApplicationEvent{
- private String text;
- public MyEvent(Object source) {
- super(source);
- }
- public void setText(String text){
- this.text = text;
- }
- public String getText(){
- return this.text;
- }
- }
4、我的監聽器
- package com.huai.listener;
- import org.springframework.context.ApplicationEvent;
- import org.springframework.context.ApplicationListener;
- public class MyListener implements ApplicationListener<ApplicationEvent>{
- @Override
- public void onApplicationEvent(ApplicationEvent event) {
- if(event instanceof MyEvent){
- System.out.println("my event laungh");
- }else{
- System.out.println("other envet");
- }
- }
- }
5、測試類:
- package com.huai.listener;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- import org.springframework.context.*;
- public class SpringTest {
- public static void main(String[] args) {
- ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
- MyEvent event = new MyEvent("hello World");
- event.setText("hello");
- context.publishEvent(event);
- }
- }
仔細看上面的測試類,MyEvent類已經例項化,但MyListener類呢?我們並沒有例項化,那麼我們應該讓容器例項化,所以我們需要在spring的配置檔案中告訴spring容器,讓它幫我們例項化MyListener類。我們還應該注意到這條程式碼:context.publishEvent(event);作用是讓spring容器中的所有監聽器都知道有這樣一個事件發生了。在spring原始碼中的解釋是:Notify all listeners registered with this application of an application event. Events may be framework events (such as RequestHandledEvent) or application-specific events.
6、spring的配置檔案:我們在spring中配置了一個實現了ApplicationListener的Bean,Spring容器就會把這個bean當成容器事件的監聽器。
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-3.0.xsd
- http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
- http://www.springframework.org/schema/tx
- http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
- http://www.springframework.org/schema/mvc
- http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
- <bean class="com.huai.listener.MyListener"/>
- </beans>
執行結果為:
other envet
my event laungh
第一個是spring容器內建的事件;
第二個是我們自己的事件。
實際上,如果開發者需要在spring容器中初始化、銷燬時回撥自定義的方法,就可以通過上面的事件監聽器來實現。