Spring监听器

监听器的使用

  1. 定义事件
public class DemoEvent extends ApplicationEvent {
    private String name;

    public DemoEvent(Object source, String name) {
        super(source);
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
  1. 监听器
  • 方式一:接口实现监听器:
@Component
public class DemoListener implements ApplicationListener<DemoEvent> {
    @Override
    public void onApplicationEvent(DemoEvent event) {
        System.out.println("事件"+event.getName()+"接口触发!");
    }
}
  • 方式二:注解监听器:
@Component
public class DemoAnnotationListener {
    @EventListener
    public void onEvent(DemoEvent event) {
        System.out.println("事件"+event.getName()+"注解触发!");
    }
}
  1. 发布事件
@SpringBootApplication
public class DemoApplication {
	public static void main(String[] args) {
		ConfigurableApplicationContext run = SpringApplication.run(DemoApplication.class, args);
		run.publishEvent(new DemoEvent(run,"发布"));
	}
}

监听器源码解析

监听器是观察者设计模式的实现。
在refresh()方发中调用了prepareRefresh()方法,预刷新上下文环境。

  1. 看下prepareRefresh()方法:
protected void prepareRefresh() {
		/**
		 * 创建一个早期事件监听器对象集合
		 */
		if (this.earlyApplicationListeners == null) {
			this.earlyApplicationListeners = new LinkedHashSet<>(this.applicationListeners);
		}
		else {
			// Reset local application listeners to pre-refresh state.
			this.applicationListeners.clear();
			this.applicationListeners.addAll(this.earlyApplicationListeners);
		}

		/**
		 * 创建一个容器用于保存早期待发布的事件集合
		 * 什么是早期事件?
		 * 就是我们的事件监听器还没有注册到多播器上的时候都称为早期事件
		 * 早期事件不需要手动publishEvent发布, 在registerListeners中会自动发布, 发布完早期事件就不存在了。
		 */
		this.earlyApplicationEvents = new LinkedHashSet<>();
	}
  1. 创建事件多播器
    initApplicationEventMulticaster()方法:
protected void initApplicationEventMulticaster() {
		//获取我们的bean工厂对象
		ConfigurableListableBeanFactory beanFactory = getBeanFactory();
		//判断容器中是没有有我们的applicationEventMulticaster 应用多播器组件
		if (beanFactory.containsLocalBean(APPLICATION_EVENT_MULTICASTER_BEAN_NAME)) {
			//直接显示的调用我们的getBean获取出来赋值给我们的applicationContext对象
			this.applicationEventMulticaster =
					beanFactory.getBean(APPLICATION_EVENT_MULTICASTER_BEAN_NAME, ApplicationEventMulticaster.class);
			if (logger.isDebugEnabled()) {
				logger.debug("Using ApplicationEventMulticaster [" + this.applicationEventMulticaster + "]");
			}
		}
		//容器中没有的话
		else {
			//spring ioc显示的new 一个SimpleApplicationEventMulticaster对象保存在applicatoinContext对象中
			this.applicationEventMulticaster = new SimpleApplicationEventMulticaster(beanFactory);
			//并且注入到容器中
			beanFactory.registerSingleton(APPLICATION_EVENT_MULTICASTER_BEAN_NAME, this.applicationEventMulticaster);
			if (logger.isDebugEnabled()) {
				logger.debug("Unable to locate ApplicationEventMulticaster with name '" +
						APPLICATION_EVENT_MULTICASTER_BEAN_NAME +
						"': using default [" + this.applicationEventMulticaster + "]");
			}
		}
	}

AbstractApplicationEventMulticaster有属性ListenerRetriever,存储了所有的listener。
在这里插入图片描述
applicationListenerBeans只存储了监听器的名字,主要是为了捕获懒加载的监听器。
多播器负责存储监听器和后面广播事件。

  1. 注册监听器 registerListeners()
    在这里插入图片描述
    以上三步整个监听器就注册完成了。

看下发布事件的流程:
在这里插入图片描述

自定义多播器

自定义多播器可以选择监听事件是否异步执行。

@Configuration
@ComponentScan(basePackages = {"com.tuling.event"})
@EnableAsync
public class MainConfig {
	/*   
		往SimpleApplicationEventMulticaster设置taskExecutor则为异步事件
     	或者使用@Async
     */
    @Bean(name = "applicationEventMulticaster")
    public ApplicationEventMulticaster simpleApplicationEventMulticaster() {
        SimpleApplicationEventMulticaster eventMulticaster
                = new SimpleApplicationEventMulticaster();
        eventMulticaster.setTaskExecutor(new SimpleAsyncTaskExecutor());
        return eventMulticaster;
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hi wei

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值