spring中监听器的使用

spring中监听器的使用

自定义监听器和事件

监听器相关组件

  • 多播器
  • 监听器
  • 事件

spring中监听器使用的不多 , springboot 中启动加载很多方法都是基于监听器实现的

例如, 启动的监听器

listeners.starting()方法

源码分析

springboot 中最关键的两个特性

  • 自动装配
  • 监听器

spring中对监听器的实现比较少 , 但是在 springboot 中监听器使用的很多

比如核心配置文件(application.yaml) 加载使用的就是监听器触发的事件

该事件去解析配置文件 (关键的类 ConfigFileApplicationListener.java)

spring关键的方法

AbstractApplicationContext.javarefresh 方法

image-20221026172303683

registerListeners()方法

spring 中 默认没有注册监听器

image-20221028234731810

finishRefresh()方法

publishEnvent(…) 发布事件

image-20221028234815779

publishEvent(Envent envent)方法

image-20221028235010831

multicatEvent方法

image-20221028235035717

doInvokeListener(listener,event)方法

image-20221028235050117

ApplicationListener的onApplicationEvent(event)方法

image-20221028235124956

image-20221028235206096

自定义监听器

1.自定义监听器类实现 ApplicationListener 接口

MyListener.java

public class MyListener implements ApplicationListener<ApplicationEvent> {
    @Override
    public void onApplicationEvent(ApplicationEvent event) {
        System.out.println("MyListener.onApplicationEvent");
    }
}

2.spring容器注册自定义监听器

@Configuration
@ComponentScan("com.example.listenertest")
public class SpringJavaConfig {

    @Bean
    public MyListener myListener() {
        return new MyListener();
    }
}

3.测试

package com.example.listenertest;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class AnnotationConfigApplicationTest {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(SpringJavaConfig.class);
    }
}

4.控制台打印

MyListener.onApplicationEvent

自定义监听器+自定义事件

1.自定义事件实现 ApplicationEvent 接口

MyEnvent.java

package com.example.listenertest;

import org.springframework.context.ApplicationEvent;

public class MyEnvent extends ApplicationEvent {
    /**
     * Create a new {@code ApplicationEvent}.
     *
     * @param source the object on which the event initially occurred or with
     *               which the event is associated (never {@code null})
     */
    public MyEnvent(Object source) {
        super(source);
        System.out.println("MyEnvent.MyEnvent");
    }
}

2.自定义监听器触发自定义事件

MyEnventListener.java

package com.example.listenertest;

import org.springframework.context.ApplicationListener;

public class MyEnventListener implements ApplicationListener<MyEnvent> {
    @Override
    public void onApplicationEvent(MyEnvent event) {
        Object source = event.getSource();
        System.out.println(source);
        System.out.println("MyEnventListener.onApplicationEvent");
    }
}

注意事项

image-20221029000051070

3.注册监听器

package com.example.listenertest;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan("com.example.listenertest")
public class SpringJavaConfig {

    @Bean
    public MyEnventListener myEnventListener() {
        return new MyEnventListener();
    }
}

4.发布事件

参考 spring 源码发布事件的方法

AbstractApplicationContext.java

		// Publish the final event.
		publishEvent(new ContextRefreshedEvent(this));

自定义触发事件

 AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(SpringJavaConfig.class);
        ac.publishEvent(new MyEnvent("我自定义的注册事件"));

5.测试

package com.example.listenertest;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class AnnotationConfigApplicationTest {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(SpringJavaConfig.class);
        ac.publishEvent(new MyEnvent("我自定义的注册事件"));
    }
}

6.源码解析

发现 spring 容器添加了自定义的监听器

image-20221029000828686

7.控制台打印

MyEnvent.MyEnvent
我自定义的注册事件
MyEnventListener.onApplicationEvent

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值