spring-事件
spring事件类型:ApplicationEvent
spring事件监听器: ApplicationListener
spring事件广播器: ApplicationEventMulticaster
监听所有spring事件:
public class SpringEventDemo { public static void main(String[] args) { GenericApplicationContext context = new GenericApplicationContext(); context.addApplicationListener(new ApplicationListener<ApplicationEvent>() { @Override public void onApplicationEvent(ApplicationEvent event) { System.err.println(event); } }); context.refresh(); //ContextRefreshedEvent context.publishEvent("first"); //PayloadApplicationEvent context.publishEvent(new MyEvent("myEvent")); //SpringEventDemo$MyEvent context.close(); //ContextClosedEvent } private static class MyEvent extends ApplicationEvent{ /** * Create a new ApplicationEvent. * * @param source the object on which the event initially occurred (never {@code null}) */ public MyEvent(Object source) { super(source); } } }
打印结果:
org.springframework.context.event.ContextRefreshedEvent[source=org.springframework.context.support.GenericApplicationContext@707f7052, started on Sun Sep 22 21:25:19 CST 2019]
21:25:20.225 [main] DEBUG org.springframework.context.support.GenericApplicationContext - Closing org.springframework.context.support.GenericApplicationContext@707f7052, started on Sun Sep 22 21:25:19 CST 2019
org.springframework.context.PayloadApplicationEvent[source=org.springframework.context.support.GenericApplicationContext@707f7052, started on Sun Sep 22 21:25:19 CST 2019]
com.example.springbootdemo.SpringEventDemo$MyEvent[source=myEvent]
org.springframework.context.event.ContextClosedEvent[source=org.springframework.context.support.GenericApplicationContext@707f7052, started on Sun Sep 22 21:25:19 CST 2019]
监听某个事件:
public class SpringEventDemo { public static void main(String[] args) { GenericApplicationContext context = new GenericApplicationContext(); // context.addApplicationListener(new ApplicationListener<ApplicationEvent>() { // @Override // public void onApplicationEvent(ApplicationEvent event) { // System.err.println(event); // } // }); context.addApplicationListener(new MyEventListener()); context.refresh(); //ContextRefreshedEvent context.publishEvent("first"); //PayloadApplicationEvent context.publishEvent(new MyEvent("myEvent")); //SpringEventDemo$MyEvent context.close(); //ContextClosedEvent } private static class MyEvent extends ApplicationEvent{ /** * Create a new ApplicationEvent. * * @param source the object on which the event initially occurred (never {@code null}) */ public MyEvent(Object source) { super(source); } } private static class MyEventListener implements ApplicationListener<MyEvent>{ @Override public void onApplicationEvent(MyEvent event) { System.err.println(event); } } }
输出结果:
com.example.springbootdemo.SpringEventDemo$MyEvent[source=myEvent]
public class DemoApplication { public static void main(String[] args) { ApplicationEventMulticaster multicaster = new SimpleApplicationEventMulticaster(); multicaster.addApplicationListener(event -> { if(event instanceof PayloadApplicationEvent){ System.out.println("接收到payloadApplicationEvent"); }else { System.out.println("接收到事件"); } }); multicaster.multicastEvent(new PayloadApplicationEvent<>("1","yin")); multicaster.multicastEvent(new MyEvent("myevent")); } private static class MyEvent extends ApplicationEvent{ public MyEvent(Object source) { super(source); } } }
输出结果:
接收到payloadApplicationEvent
接收到事件