首页   快速返回

spring5事件机制     所属分类 spring
org.springframework.context.ApplicationListener 

import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.PayloadApplicationEvent;

public class MyApplicationListener implements ApplicationListener{	
	
	public void onApplicationEvent(ApplicationEvent  event) {	
		System.out.println("event="+event+","+event.getClass());	
		if(event instanceof PayloadApplicationEvent){
			PayloadApplicationEvent pevent = (PayloadApplicationEvent)event;
			Object payload = pevent.getPayload();
			Object source = pevent.getSource();	
			System.out.println("payload="+payload+",source="+source);
		}
		         
    }
}

MyApplicationListener 配置为一个 bean

import org.springframework.context.ApplicationEvent;

public class MyEvent extends ApplicationEvent{	
	public MyEvent(Object source){
		super(source);
	}
}


ctx.publishEvent("payload-event");
ctx.publishEvent(new MyEvent("my-event"));

支持发布 ApplicationEvent 及 普通的事件  使用 PayloadApplicationEvent 包装

public class PayloadApplicationEvent extends ApplicationEvent


ContextRefreshedEvent
ContextClosedEvent


event=spring5.MyEvent[source=my-event],class spring5.MyEvent

event=org.springframework.context.PayloadApplicationEvent[source=org.springframework.context.annotation.AnnotationConfigApplicationContext@4f7d0008: startup date [Mon Jun 17 17:56:50 CST 2019]; root of context hierarchy],class org.springframework.context.PayloadApplicationEvent
payload=payload-event,source=org.springframework.context.annotation.AnnotationConfigApplicationContext@4f7d0008: startup date [Mon Jun 17 17:56:50 CST 2019]; root of context hierarchy

上一篇     下一篇
spring bean生命周期及扩展点

spring xml bean 配置读取

为何要定投指数基金

红黑树要点整理

BeanFactory和ApplicationContext的区别

jvm在线诊断工具greys