springboot实战原理(10)–配置事件监听的4种方式和原理

目录:

springboot实战原理(10)--配置事件监听的4种方式和原理源码地址:https://Github.com/hufanglei/Springboot-v-study/tree/demo7

一、配置监听器的步骤:

  • 1.自定义事件,一般是继承ApplicationEvent抽象类
  • 2定义事件监听器,一般是实现ApplicationListener的接口
  • 3.启动时候,需要把监听器加入到spring容器中
  • 4.发布事件,使用ApplicationEventPublisher.publishEvent 发布事件

二、配置监听器:4种方式:

①SpringApplication..addListeners增加

例子: //定义事件

//定义事件
public class MyAplicationEvent extends ApplicationEvent {

    private static final long serialVersionUID = 1L;

    public MyAplicationEvent(Object source) {
        super(source);
    }
}

//监听器

public class MyApplicationListener implements ApplicationListener<MyAplicationEvent{

    @Override
    public void onApplicationEvent(MyAplicationEvent event) {
        System.out.println("接受到事件============"+ event.getClass());
    }
}

//在启动入口中:通过addListeners增加添加到spring容器和发布事件

@SpringBootApplication
public class Demo7Application {

    public static void main(String[] args) {

        SpringApplication app = new SpringApplication(Demo7Application.class);
        //addListeners增加
        app.addListeners(new MyApplicationListener());
        ConfigurableApplicationContext context = app.run(args);
        //发布事件
        context.publishEvent(new MyAplicationEvent(new Object()));
        context.close();
//        context.stop();
    }

}

运行结果:springboot实战原理(10)--配置事件监听的4种方式和原理显示监听器已经注入。

② 使用@Component 把监听器纳入到spring容器中管理

修改刚才的代码:我们在监听器上加上@Component

//监听器
@Component
public class MyApplicationListener implements ApplicationListener<MyAplicationEvent{

    @Override
    public void onApplicationEvent(MyAplicationEvent event) {
        System.out.println("接受到事件============"+ event.getClass());
    }
}

入口函数删除 addListener

@SpringBootApplication
public class Demo7Application {

    public static void main(String[] args) {
        SpringApplication app = new SpringApplication(Demo7Application.class);
        ConfigurableApplicationContext context = app.run(args);
        context.publishEvent(new MyAplicationEvent(new Object()));
        context.close();
//        context.stop();
    }

}

重新运行代码:springboot实战原理(10)--配置事件监听的4种方式和原理也说明,监听器生效了。

③ application.properties中配置context.listener.classes属性

配置如下:  context.listener.classes=com.springboot.demo7.MyApplicationListenerspringboot实战原理(10)--配置事件监听的4种方式和原理入口函数不变,继续运行:

@SpringBootApplication
public class Demo7Application {

    public static void main(String[] args) {
        SpringApplication app = new SpringApplication(Demo7Application.class);
        ConfigurableApplicationContext context = app.run(args);
        context.publishEvent(new MyAplicationEvent(new Object()));
        context.close();
//        context.stop();
    }

}

同样,监听器生效:springboot实战原理(10)--配置事件监听的4种方式和原理

④使用注解 @EventListener

我们定义一个MyEventHandle监听器处理类, 使用注解 @EventListener,在方法上面加入@EventListener注解,且该类需要纳入到spring容器中管理。看代码:

@Component
public class MyEventHandle {
    /*
     * 任意参数
     * 所有,该参数事件,或者其子事件都可以接收到
     * @param event  任意参数
     */

    @EventListener
    public void onApplicationEvent(MyAplicationEvent event) {
        System.out.println("接受到事件============"+ event.getClass());
    }
}

运行:

@SpringBootApplication
public class Demo7Application {

    public static void main(String[] args) {
        SpringApplication app = new SpringApplication(Demo7Application.class);
        ConfigurableApplicationContext context = app.run(args);
        context.publishEvent(new MyAplicationEvent(new Object()));
        context.close();
//        context.stop();
    }

}

springboot实战原理(10)--配置事件监听的4种方式和原理依然注入进来了。这里可以修改springboot实战原理(10)--配置事件监听的4种方式和原理如果改成Object,就表示任意参数,所有,该参数事件,或者其子事件都可以接收到。

@Component
public class MyEventHandle {
    /*
     * 任意参数
     * 所有,该参数事件,或者其子事件都可以接收到
     * @param event  任意参数
     */

    @EventListener
    public void onApplicationEvent(Object event) {
        System.out.println("接受到事件============"+ event.getClass());
    }
 }

运行:

@SpringBootApplication
public class Demo7Application {

    public static void main(String[] args) {
        SpringApplication app = new SpringApplication(Demo7Application.class);
        ConfigurableApplicationContext context = app.run(args);
        context.publishEvent(new MyAplicationEvent(new Object()));
        context.close();
//        context.stop();
    }

}

springboot实战原理(10)--配置事件监听的4种方式和原理可以看到,不过有我们自定义的事件,spring默认注入的事件都打印出来了。

三、原理

第一二种方式都是启动时候,把监听器加入到spring容器中,ConfigurableApplicationContext拿到这个对象再发布就可使用监听器了。第三四中最终也是把监听器ConfigurableApplicationContext,只不过实现有了2个特别的类:

①context.listener.classes配置原理之DelegatingApplicationListener:

看下源码

springboot实战原理(10)--配置事件监听的4种方式和原理springboot实战原理(10)--配置事件监听的4种方式和原理springboot实战原理(10)--配置事件监听的4种方式和原理我们可以看到,这个类是读取了配置的属性context.listener.classes,加入到监听器列表并排序好给我们的spring容器。

② 注解@EventListener配置原理之 EventListenerMethodProcessor

看名字就知道EventListenerMethodProcessor这个是个后置处理器:看下源码:

springboot实战原理(10)--配置事件监听的4种方式和原理afterSingletonsInstantiated方法调用了processBean方法: processBean方法获取所有的@EventListener的注解:springboot实战原理(10)--配置事件监听的4种方式和原理该方法有这些注解的类加入到了context中,Springboot就可以使用了。springboot实战原理(10)--配置事件监听的4种方式和原理

四、spring和springboot默认自定义的事件查看

他们都在context包下:看下springboot下的很多默认的事件和监听器springboot实战原理(10)--配置事件监听的4种方式和原理看下spring5下的很多默认的事件和监听器:

springboot实战原理(10)--配置事件监听的4种方式和原理
在这里插入图片描述

五、测试下默认的监听器:

我们测试下spring提供的:springboot实战原理(10)--配置事件监听的4种方式和原理我们使用第4种方式测试下:

@Component
public class MyEventHandle {
    @EventListener
    public void event2(ContextStoppedEvent event) {
        System.out.println("应用停止事件============"+ event.getClass());
    }
}
@SpringBootApplication
public class Demo7Application {
    public static void main(String[] args) {
        SpringApplication app = new SpringApplication(Demo7Application.class);
        ConfigurableApplicationContext context = app.run(args);
        context.stop();
    }
}

看下打印结果:springboot实战原理(10)--配置事件监听的4种方式和原理说明这个应用关闭的事件触发了。

总结:本文章主要讲了springboot配置监听器的步骤方式,原理和自带的事件等。


个人微信公号:
搜索:怒放de每一天
不定时推送相关文章,期待和大家一起成长!!



原文始发于微信公众号(怒放de每一天):springboot实战原理(10)–配置事件监听的4种方式和原理

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/106691.html

(0)
小半的头像小半

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!