import org.springframework.context.ApplicationContext; import org.springframework.context.event.ApplicationContextEvent; public class MyEvent extends ApplicationContextEvent { public MyEvent(ApplicationContext source) { super(source); System.out.println("MyEvent 事件執行了。。。"+source.toString()); } public void out(String name) { System.out.println("MyEvent 事件執行了 name :"+name); } }
import org.springframework.context.ApplicationListener; public class MyListenerA implements ApplicationListener<MyEvent> { @Override public void onApplicationEvent(MyEvent myEvent) { System.out.println("MyListenerA 執行了"); myEvent.out("wangmingyuan not love"); } }
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; @Configuration @Import(value = {MyEvent.class,MyListenerA.class,MyPublisher.class}) public class Config { }
import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.context.ApplicationEvent; public class MyPublisher implements ApplicationContextAware { public ApplicationContext applicationContext; @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { this.applicationContext=applicationContext; } public void publisherEvent(ApplicationEvent event) { System.out.println("-----發送事件-----"+event); applicationContext.publishEvent(event); } }
import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class Test { public static void main(String[] args) { AnnotationConfigApplicationContext context=new AnnotationConfigApplicationContext(Config.class); MyPublisher myPublisher = context.getBean(MyPublisher.class); MyEvent myEvent = context.getBean(MyEvent.class); myPublisher.publisherEvent(myEvent); } }
11:34:56.652 [main] DEBUG org.springframework.context.annotation.AnnotationConfigApplicationContext - Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@3b084709
11:34:56.695 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'
11:34:56.952 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.event.internalEventListenerProcessor'
11:34:56.955 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.event.internalEventListenerFactory'
11:34:56.959 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor'
11:34:56.961 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalCommonAnnotationProcessor'
11:34:57.042 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'config'
11:34:57.048 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'com.raise3d.bye.listener.MyEvent'
11:34:57.117 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Autowiring by type from bean name 'com.raise3d.bye.listener.MyEvent' via constructor to bean named 'org.springframework.context.annotation.AnnotationConfigApplicationContext@3b084709'
MyEvent 事件執行了。。。org.springframework.context.annotation.AnnotationConfigApplicationContext@3b084709, started on Mon Nov 25 11:34:56 CST 2019
11:34:57.121 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'com.raise3d.bye.listener.MyListenerA'
11:34:57.135 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'com.raise3d.bye.listener.MyPublisher'
-----發送事件-----com.raise3d.bye.listener.MyEvent[source=org.springframework.context.annotation.AnnotationConfigApplicationContext@3b084709, started on Mon Nov 25 11:34:56 CST 2019]
MyListenerA 執行了
MyEvent 事件執行了 name :wangmingyuan not love