Spring中ApplicationEvent和ApplicationListener的应用

2022年7月21日08:16:50

这个是spring基于Application的监听器,继承ApplicationEvent类就可以创建一个监听器监听的对象。具体代码如下:
这个是spring基于Application的监听器,继承ApplicationEvent类就可以创建一个监听器监听的对象。具体代码如下:

  1. 创建监听器监听的类
package com.example.demo.event;import lombok.Data;import org.springframework.context.ApplicationEvent;/**
 * 监听器监听的对象
 */@DatapublicclassDemoEventextendsApplicationEvent{private String msg;publicDemoEvent(Object source,String msg){super(source);this.msg= msg;}}
  1. 创建监听器监听 DemoEvent 事件
package com.example.demo.event;import org.springframework.context.ApplicationListener;import org.springframework.context.annotation.Configuration;/**
 * 监听DemoEvent事件
 * @author javacfox
 */@ConfigurationpublicclassDemoEventListenerimplementsApplicationListener<DemoEvent>{@OverridepublicvoidonApplicationEvent(DemoEvent demoEvent){
        System.out.println("DemoEventListener接受到了demoPublisher发布的消息:"+demoEvent.getMsg());}}
  1. 事件发布
package com.example.demo.event;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.ApplicationContext;import org.springframework.context.annotation.Configuration;@ConfigurationpublicclassDemoEventPublisher{@Autowiredprivate ApplicationContext applicationContext;publicvoidpublish(){
        DemoEvent event=newDemoEvent(this,"发布成功!");
        System.out.println("发部event:"+event);
        applicationContext.publishEvent(event);}}
  1. 测试
package com.test.demo.event;import org.springframework.context.annotation.AnnotationConfigApplicationContext;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;@Configuration@ComponentScan({"com.test.demo.event"})publicclassEventTest{publicstaticvoidmain(String[] args){
        AnnotationConfigApplicationContext context=newAnnotationConfigApplicationContext(EventTest.class);
		context.start();
		String targetObj="i am the target object";
		DemoEvent demoEvent=newDemoEvent(targetObj,"目标发布成功!");// 发布事件
        context.publishEvent(demoEvent);

        context.close();}}
  1. 测试结果
21:55:21.065[main] DEBUG org.springframework.context.annotation.AnnotationConfigApplicationContext- Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@16f6561221:55:21.079[main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory- Creating shared instance of singleton bean'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'21:55:21.129[main] DEBUG org.springframework.context.annotation.ClassPathBeanDefinitionScanner- Identified candidate componentclass: file[F:\spring-boot-2.0.1.RELEASE\spring-boot-project\DemoTest\target\classes\com\test\demo\event\DemoEventListener.class]21:55:21.130[main] DEBUG org.springframework.context.annotation.ClassPathBeanDefinitionScanner- Identified candidate componentclass: file[F:\spring-boot-2.0.1.RELEASE\spring-boot-project\DemoTest\target\classes\com\test\demo\event\DemoEventPublisher.class]21:55:21.207[main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory- Creating shared instance of singleton bean'org.springframework.context.event.internalEventListenerProcessor'21:55:21.209[main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory- Creating shared instance of singleton bean'org.springframework.context.event.internalEventListenerFactory'21:55:21.210[main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory- Creating shared instance of singleton bean'org.springframework.context.annotation.internalAutowiredAnnotationProcessor'21:55:21.211[main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory- Creating shared instance of singleton bean'org.springframework.context.annotation.internalCommonAnnotationProcessor'21:55:21.217[main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory- Creating shared instance of singleton bean'eventTest'21:55:21.221[main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory- Creating shared instance of singleton bean'demoEventListener'21:55:21.222[main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory- Creating shared instance of singleton bean'demoEventPublisher'
DemoEventListener接受到了demoPublisher发布的消息:目标发布成功!21:55:21.248[main] DEBUG org.springframework.context.annotation.AnnotationConfigApplicationContext- Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@16f65612, started on Tue Jan1421:55:21 CST2020

Process finished with exit code0
  • 作者:笑不语
  • 原文链接:https://blog.csdn.net/weixin_43935907/article/details/103980944
    更新时间:2022年7月21日08:16:50 ,共 4286 字。