我是靠谱客的博主 年轻保温杯,最近开发中收集的这篇文章主要介绍springboot事件发布和监听器,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

一. 监听springboot1.5.10容器事件

SpringApplicationRunListener接口定义

package org.springframework.boot;
public interface SpringApplicationRunListener {
// 在run()方法开始执行时,该方法就立即被调用,可用于在初始化最早期时做一些工作
void starting();
// 当environment构建完成,ApplicationContext创建之前,该方法被调用
void environmentPrepared(ConfigurableEnvironment environment);
// 当ApplicationContext构建完成时,该方法被调用
void contextPrepared(ConfigurableApplicationContext context);
// 在ApplicationContext完成加载,但没有被刷新前,该方法被调用
void contextLoaded(ConfigurableApplicationContext context);
// 在ApplicationContext刷新并启动后,CommandLineRunners和ApplicationRunner未被调用前,该方法被调用
void started(ConfigurableApplicationContext context);
// 在run()方法执行完成前该方法被调用
void running(ConfigurableApplicationContext context);
// 当应用运行出错时该方法被调用
void failed(ConfigurableApplicationContext context, Throwable exception);
}

自定义监听实现SpringApplicationRunListener接口

package com.feng.baseframework.listener;
import com.feng.baseframework.autoconfig.SystemPropertyInit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringApplicationRunListener;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;
/**
* baseframework
* 2019/8/8 15:43
* 自定义spring启动监听器
*
* @author lanhaifeng
* @since
**/
public class MyApplicationRunListener implements SpringApplicationRunListener {
private static Logger logger = LoggerFactory.getLogger(MyApplicationRunListener.class);
public MyApplicationRunListener(SpringApplication application, String[]
args){
logger.info("MyApplicationRunListener constructor");
}
@Override
public void starting() {
logger.info("application starting");
SystemPropertyInit.initSystemProperty();
}
@Override
public void environmentPrepared(ConfigurableEnvironment configurableEnvironment) {
logger.info("application environmentPrepared");
}
@Override
public void contextPrepared(ConfigurableApplicationContext configurableApplicationContext) {
logger.info("application contextPrepared");
}
@Override
public void contextLoaded(ConfigurableApplicationContext configurableApplicationContext) {
logger.info("application contextLoaded");
}
@Override
public void finished(ConfigurableApplicationContext configurableApplicationContext, Throwable throwable) {
logger.info("application finished");
}
}

在spring.factories(srcmainresourcesMETA-INFspring.factories)中配置自定义SpringApplicationRunListener的实现

org.springframework.boot.SpringApplicationRunListener=
com.feng.baseframework.listener.MyApplicationRunListener

启动应用程序,输出结果如下:

19:19:08.307 [main] INFO com.feng.baseframework.listener.MyApplicationRunListener - MyApplicationRunListener constructor
2019-08-22 19:19:08.876
INFO 26072 --- [
main] c.f.b.listener.MyApplicationRunListener
: application contextPrepared
2019-08-22 19:23:12.767
INFO 24824 --- [
main] c.f.b.listener.MyApplicationRunListener
: application contextPrepared
2019-08-22 19:23:12.829
INFO 24824 --- [
main] c.f.b.listener.MyApplicationRunListener
: application contextLoaded
2019-08-22 19:23:22.847
INFO 24824 --- [
main] c.f.b.listener.MyApplicationRunListener
: application finished

触发原理,SpringApplication中

public ConfigurableApplicationContext run(String... args) {
StopWatch stopWatch = new StopWatch();
stopWatch.start();
ConfigurableApplicationContext context = null;
FailureAnalyzers analyzers = null;
this.configureHeadlessProperty();
//getRunListeners通过spi读取spring.factories文件中SpringApplicationRunListener实现类
SpringApplicationRunListeners listeners = this.getRunListeners(args);
listeners.starting();
try {
ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
ConfigurableEnvironment environment = this.prepareEnvironment(listeners, applicationArguments);
Banner printedBanner = this.printBanner(environment);
context = this.createApplicationContext();
new FailureAnalyzers(context);
this.prepareContext(context, environment, listeners, applicationArguments, printedBanner);
this.refreshContext(context);
this.afterRefresh(context, applicationArguments);
listeners.finished(context, (Throwable)null);
stopWatch.stop();
if (this.logStartupInfo) {
(new StartupInfoLogger(this.mainApplicationClass)).logStarted(this.getApplicationLog(), stopWatch);
}
return context;
} catch (Throwable var9) {
this.handleRunFailure(context, listeners, (FailureAnalyzers)analyzers, var9);
throw new IllegalStateException(var9);
}
}

getRunListeners方法,读取SpringApplicationRunListener实现类,存放到SpringApplicationRunListeners的listeners属性中

private SpringApplicationRunListeners getRunListeners(String[] args) {
Class<?>[] types = new Class[]{SpringApplication.class, String[].class};
return new SpringApplicationRunListeners(logger, this.getSpringFactoriesInstances(SpringApplicationRunListener.class, types, this, args));
}

执行时,遍历listeners中的实例

public void starting() {
Iterator var1 = this.listeners.iterator();
while(var1.hasNext()) {
SpringApplicationRunListener listener = (SpringApplicationRunListener)var1.next();
listener.starting();
}
}

还可以直接将监听配置在application.properties中

context.listener.classes=com.jwb.demo.listener.MyApplicationEventListener

触发是通过DelegatingApplicationListener类,该类会读取key为context.listener.classes的值,但是只会触发事件类型为ApplicationEnvironmentPreparedEvent的事件,该事件是启动时环境变量初始化完成以后产生的

public class DelegatingApplicationListener implements ApplicationListener<ApplicationEvent>, Ordered {
private static final String PROPERTY_NAME = "context.listener.classes";
private int order = 0;
private SimpleApplicationEventMulticaster multicaster;
public DelegatingApplicationListener() {
}
public void onApplicationEvent(ApplicationEvent event) {
if (event instanceof ApplicationEnvironmentPreparedEvent) {
List<ApplicationListener<ApplicationEvent>> delegates = this.getListeners(((ApplicationEnvironmentPreparedEvent)event).getEnvironment());
if (delegates.isEmpty()) {
return;
}
this.multicaster = new SimpleApplicationEventMulticaster();
Iterator var3 = delegates.iterator();
while(var3.hasNext()) {
ApplicationListener<ApplicationEvent> listener = (ApplicationListener)var3.next();
this.multicaster.addApplicationListener(listener);
}
}
if (this.multicaster != null) {
this.multicaster.multicastEvent(event);
}
}
private List<ApplicationListener<ApplicationEvent>> getListeners(ConfigurableEnvironment environment) {
if (environment == null) {
return Collections.emptyList();
} else {
String classNames = environment.getProperty("context.listener.classes");
List<ApplicationListener<ApplicationEvent>> listeners = new ArrayList();
if (StringUtils.hasLength(classNames)) {
Iterator var4 = StringUtils.commaDelimitedListToSet(classNames).iterator();
while(var4.hasNext()) {
String className = (String)var4.next();
try {
Class<?> clazz = ClassUtils.forName(className, ClassUtils.getDefaultClassLoader());
Assert.isAssignable(ApplicationListener.class, clazz, "class [" + className + "] must implement ApplicationListener");
listeners.add((ApplicationListener)BeanUtils.instantiateClass(clazz));
} catch (Exception var7) {
throw new ApplicationContextException("Failed to load context listener class [" + className + "]", var7);
}
}
}
AnnotationAwareOrderComparator.sort(listeners);
return listeners;
}
}
public void setOrder(int order) {
this.order = order;
}
public int getOrder() {
return this.order;
}
}
二. springboot1.5.10监听web容器事件

方式一:使用ServletListenerRegistrationBean(同理servlet、filter也可以使用类似的注册类)

@Configuration
public class WebConfig {
@Bean
public ServletListenerRegistrationBean<EventListener> getDemoListener(){
ServletListenerRegistrationBean<EventListener> registrationBean = new ServletListenerRegistrationBean<>();
registrationBean.setListener(new XbqListener());
registrationBean.setOrder(1);
return registrationBean;
}
}

方式二:使用注解@WebListener(同理servlet、filter也可以使用类似的注解类)

@WebListener
public class JoeListener implements ServletContextListener{
private static Logger logger = LoggerFactory.getLogger(JoeListener.class);
@Override
public void contextInitialized(ServletContextEvent sce) {
logger.info("--Joe-监听器-ServletContext 初始化");
logger.info(sce.getServletContext().getServerInfo());
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
logger.info("--Joe-监听器-ServletContext 销毁");
}
}

触发原理
ServletListenerRegistrationBean继承抽象RegistrationBean类,并实现了RegistrationBean类中ServletContextInitializer接口的onStartup方法

public void onStartup(ServletContext servletContext) throws ServletException {
if (!this.isEnabled()) {
logger.info("Listener " + this.listener + " was not registered (disabled)");
} else {
try {
servletContext.addListener(this.listener);
} catch (RuntimeException var3) {
throw new IllegalStateException("Failed to add listener '" + this.listener + "' to servlet context", var3);
}
}
}

其中servletContext.addListener(this.listener);将监听器注入到web容器中
而ServletListenerRegistrationBean触发过程,主要分析嵌入式tomcat
springboot启动入口

public ConfigurableApplicationContext run(String... args) {
StopWatch stopWatch = new StopWatch();
stopWatch.start();
ConfigurableApplicationContext context = null;
FailureAnalyzers analyzers = null;
this.configureHeadlessProperty();
SpringApplicationRunListeners listeners = this.getRunListeners(args);
listeners.starting();
try {
ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
ConfigurableEnvironment environment = this.prepareEnvironment(listeners, applicationArguments);
Banner printedBanner = this.printBanner(environment);
context = this.createApplicationContext();
new FailureAnalyzers(context);
this.prepareContext(context, environment, listeners, applicationArguments, printedBanner);
this.refreshContext(context);
this.afterRefresh(context, applicationArguments);
listeners.finished(context, (Throwable)null);
stopWatch.stop();
if (this.logStartupInfo) {
(new StartupInfoLogger(this.mainApplicationClass)).logStarted(this.getApplicationLog(), stopWatch);
}
return context;
} catch (Throwable var9) {
this.handleRunFailure(context, listeners, (FailureAnalyzers)analyzers, var9);
throw new IllegalStateException(var9);
}
}

其中,context = this.createApplicationContext();在web项目构建的是AnnotationConfigEmbeddedWebApplicationContext的实例
而 this.refreshContext(context);执行了AnnotationConfigEmbeddedWebApplicationContext父类EmbeddedWebApplicationContext的refresh()
EmbeddedWebApplicationContext

public final void refresh() throws BeansException, IllegalStateException {
try {
super.refresh();
} catch (RuntimeException var2) {
this.stopAndReleaseEmbeddedServletContainer();
throw var2;
}
}

这里可以看到直接调用了父类的refresh()方法,而这个方法位于AbstractApplicationContext中

public void refresh() throws BeansException, IllegalStateException {
Object var1 = this.startupShutdownMonitor;
synchronized(this.startupShutdownMonitor) {
this.prepareRefresh();
ConfigurableListableBeanFactory beanFactory = this.obtainFreshBeanFactory();
this.prepareBeanFactory(beanFactory);
try {
this.postProcessBeanFactory(beanFactory);
this.invokeBeanFactoryPostProcessors(beanFactory);
this.registerBeanPostProcessors(beanFactory);
this.initMessageSource();
this.initApplicationEventMulticaster();
this.onRefresh();
this.registerListeners();
this.finishBeanFactoryInitialization(beanFactory);
this.finishRefresh();
} catch (BeansException var9) {
if (this.logger.isWarnEnabled()) {
this.logger.warn("Exception encountered during context initialization - cancelling refresh attempt: " + var9);
}
this.destroyBeans();
this.cancelRefresh(var9);
throw var9;
} finally {
this.resetCommonCaches();
}
}
}

其中调用方法this.onRefresh();

protected void onRefresh() throws BeansException {
}

onRefresh()被protected 修饰,由子类提供即EmbeddedWebApplicationContext提供

protected void onRefresh() {
super.onRefresh();
try {
this.createEmbeddedServletContainer();
} catch (Throwable var2) {
throw new ApplicationContextException("Unable to start embedded container", var2);
}
}

接着我们看createEmbeddedServletContainer()方法

private void createEmbeddedServletContainer() {
EmbeddedServletContainer localContainer = this.embeddedServletContainer;
ServletContext localServletContext = this.getServletContext();
if (localContainer == null && localServletContext == null) {
EmbeddedServletContainerFactory containerFactory = this.getEmbeddedServletContainerFactory();
this.embeddedServletContainer = containerFactory.getEmbeddedServletContainer(new ServletContextInitializer[]{this.getSelfInitializer()});
} else if (localServletContext != null) {
try {
this.getSelfInitializer().onStartup(localServletContext);
} catch (ServletException var4) {
throw new ApplicationContextException("Cannot initialize servlet context", var4);
}
}
this.initPropertySources();
}

主要看this.getSelfInitializer().onStartup(localServletContext);这行
我们先看getSelfInitializer()方法

private ServletContextInitializer getSelfInitializer() {
return new ServletContextInitializer() {
public void onStartup(ServletContext servletContext) throws ServletException {
EmbeddedWebApplicationContext.this.selfInitialize(servletContext);
}
};
}

该方法返回ServletContextInitializer接口匿名类实例,该实例的onStartup方法又调用了当前实例的selfInitialize方法

private void selfInitialize(ServletContext servletContext) throws ServletException {
this.prepareEmbeddedWebApplicationContext(servletContext);
ConfigurableListableBeanFactory beanFactory = this.getBeanFactory();
EmbeddedWebApplicationContext.ExistingWebApplicationScopes existingScopes = new EmbeddedWebApplicationContext.ExistingWebApplicationScopes(beanFactory);
WebApplicationContextUtils.registerWebApplicationScopes(beanFactory, this.getServletContext());
existingScopes.restore();
WebApplicationContextUtils.registerEnvironmentBeans(beanFactory, this.getServletContext());
Iterator var4 = this.getServletContextInitializerBeans().iterator();
while(var4.hasNext()) {
ServletContextInitializer beans = (ServletContextInitializer)var4.next();
beans.onStartup(servletContext);
}
}

看到this.getServletContextInitializerBeans().iterator();这样一句代码,这句代码就是获取ServletContextInitializer接口的实现类实例,循环执行了ServletContextInitializer的onStartup方法,即ServletListenerRegistrationBean的onStartup方法

protected Collection<ServletContextInitializer> getServletContextInitializerBeans() {
return new ServletContextInitializerBeans(this.getBeanFactory());
}

发现真正的逻辑在ServletContextInitializerBeans中

public ServletContextInitializerBeans(ListableBeanFactory beanFactory) {
this.addServletContextInitializerBeans(beanFactory);
this.addAdaptableBeans(beanFactory);
List<ServletContextInitializer> sortedInitializers = new ArrayList();
Iterator var3 = this.initializers.entrySet().iterator();
while(var3.hasNext()) {
Entry<?, List<ServletContextInitializer>> entry = (Entry)var3.next();
AnnotationAwareOrderComparator.sort((List)entry.getValue());
sortedInitializers.addAll((Collection)entry.getValue());
}
this.sortedList = Collections.unmodifiableList(sortedInitializers);
}

可以看到addServletContextInitializerBeans方法通过beanFactory获取ServletContextInitializer接口的实例,依次放入容器中

private void addServletContextInitializerBeans(ListableBeanFactory beanFactory) {
Iterator var2 = this.getOrderedBeansOfType(beanFactory, ServletContextInitializer.class).iterator();
while(var2.hasNext()) {
Entry<String, ServletContextInitializer> initializerBean = (Entry)var2.next();
this.addServletContextInitializerBean((String)initializerBean.getKey(), (ServletContextInitializer)initializerBean.getValue(), beanFactory);
}
}
三. spring发布自定义事件及监听

使用springboot注解

/**
* 监听配置类
*
* @author oKong
*
*/
@Configuration
@Slf4j
public class EventListenerConfig {
@EventListener
public void handleEvent(Object event) {
//监听所有事件 可以看看 系统各类时间 发布了哪些事件
//可根据 instanceof 监听想要监听的事件
//
if(event instanceof CustomEvent) {
//
//
}
log.info("事件:{}", event);
}
@EventListener
public void handleCustomEvent(CustomEvent customEvent) {
//监听 CustomEvent事件
log.info("监听到CustomEvent事件,消息为:{}, 发布时间:{}", customEvent.getMessageEntity(), customEvent.getTimestamp());
}
/**
* 监听 code为oKong的事件
*/
@EventListener(condition="#customEvent.messageEntity.code == 'oKong'")
public void handleCustomEventByCondition(CustomEvent customEvent) {
//监听 CustomEvent事件
log.info("监听到code为'oKong'的CustomEvent事件,消息为:{}, 发布时间:{}", customEvent.getMessageEntity(), customEvent.getTimestamp());
}
@EventListener
public void handleObjectEvent(MessageEntity messageEntity) {
//这个和eventbus post方法一样了
log.info("监听到对象事件,消息为:{}", messageEntity);
}
}

注意:从Spring4.2开始,事件源不强迫继承ApplicationEvent接口的,也就是可以直接发布任意一个对象类。但内部其实是使用PayloadApplicationEvent类进行包装了一层。这点和guava的eventBus类似。

而且,使用@EventListener的condition可以实现更加精细的事件监听,condition支持SpEL表达式,可根据事件源的参数来判断是否监听。
异步监听@Async,同时@Async(“taskEx”)可以指定连接池

@EventListener
@Async
public void handleObjectEvent(MessageEntity messageEntity) {
//这个和eventbus post方法一样了
log.info("监听到对象事件,消息为:{}", messageEntity);
}

事务绑定
当一些场景下,比如在用户注册成功后,即数据库事务提交了,之后再异步发送邮件等,不然会发生数据库插入失败,但事件却发布了,也就是邮件发送成功了的情况。此时,我们可以使用@TransactionalEventListener注解或者TransactionSynchronizationManager类来解决此类问题,也就是:事务成功提交后,再发布事件。当然也可以利用返回上层(事务提交后)再发布事件的方式了,只是不够优雅而已

@EventListener
@Async
@TransactionalEventListener
public void handleObjectEvent(MessageEntity messageEntity) {
//这个和eventbus post方法一样了
log.info("监听到对象事件,消息为:{}", messageEntity);
}
@EventListener
public void handleObjectEvent(MessageEntity messageEntity) {
if(TransactionSynchronizationManager.isActualTransactionActive()){
TransactionSynchronizationManager.registerSynchronization(
new TransactionSynchronizationAdapter({
@Override
public void afterCommit(){
//这个和eventbus post方法一样了
log.info("监听到对象事件,消息为:{}", messageEntity);
}
});
);
}
}

使用ApplicationListener方式

@Component
@Slf4j
public class EventListener implements ApplicationListener<CustomEvent>{
@Override
public void onApplicationEvent(CustomEvent event) {
//这里也可以监听所有事件 使用
ApplicationEvent 类即可
//这里仅仅监听自定义事件 CustomEvent
log.info("ApplicationListener方式监听事件:{}", event);
}
}

事件发布
可以使用ApplicationEventPublisher事件发布器接口,也可以使用ApplicationContext实例,实际上ApplicationContext实现了ApplicationEventPublisher接口

@Autowired
ApplicationEventPublisher eventPublisher;
@GetMapping
public String push(String code,String message) {
log.info("发布applicationEvent事件:{},{}", code, message);
eventPublisher.publishEvent(new CustomEvent(this, MessageEntity.builder().code(code).message(message).build()));
return "事件发布成功!";
}

至于原理很简单,利用了事件广播器来处理事件发布,将所有的监听器添加到广播器中,发生事件的时候进行广播
首先看一下监听的添加addApplicationListener,代码很简单当事件广播器为初始化,就存放到applicationListeners中

public void addApplicationListener(ApplicationListener<?> listener) {
Assert.notNull(listener, "ApplicationListener must not be null");
if (this.applicationEventMulticaster != null) {
this.applicationEventMulticaster.addApplicationListener(listener);
} else {
this.applicationListeners.add(listener);
}
}

事件广播器是在initApplicationEventMulticaster这里初始化的,逻辑很清楚,如果定义了事件广播器就使用定义的,否则初始化默认的事件广播器SimpleApplicationEventMulticaster

protected void initApplicationEventMulticaster() {
ConfigurableListableBeanFactory beanFactory = this.getBeanFactory();
if (beanFactory.containsLocalBean("applicationEventMulticaster")) {
this.applicationEventMulticaster = (ApplicationEventMulticaster)beanFactory.getBean("applicationEventMulticaster", ApplicationEventMulticaster.class);
if (this.logger.isDebugEnabled()) {
this.logger.debug("Using ApplicationEventMulticaster [" + this.applicationEventMulticaster + "]");
}
} else {
this.applicationEventMulticaster = new SimpleApplicationEventMulticaster(beanFactory);
beanFactory.registerSingleton("applicationEventMulticaster", this.applicationEventMulticaster);
if (this.logger.isDebugEnabled()) {
this.logger.debug("Unable to locate ApplicationEventMulticaster with name 'applicationEventMulticaster': using default [" + this.applicationEventMulticaster + "]");
}
}
}

applicationListeners监听器和自定义监听又是通过registerListeners方法添加到事件广播器中

protected void registerListeners() {
Iterator var1 = this.getApplicationListeners().iterator();
while(var1.hasNext()) {
ApplicationListener<?> listener = (ApplicationListener)var1.next();
this.getApplicationEventMulticaster().addApplicationListener(listener);
}
String[] listenerBeanNames = this.getBeanNamesForType(ApplicationListener.class, true, false);
String[] var7 = listenerBeanNames;
int var3 = listenerBeanNames.length;
for(int var4 = 0; var4 < var3; ++var4) {
String listenerBeanName = var7[var4];
this.getApplicationEventMulticaster().addApplicationListenerBean(listenerBeanName);
}
Set<ApplicationEvent> earlyEventsToProcess = this.earlyApplicationEvents;
this.earlyApplicationEvents = null;
if (earlyEventsToProcess != null) {
Iterator var9 = earlyEventsToProcess.iterator();
while(var9.hasNext()) {
ApplicationEvent earlyEvent = (ApplicationEvent)var9.next();
this.getApplicationEventMulticaster().multicastEvent(earlyEvent);
}
}
}

至于事件发生时进行广播逻辑也很简单就是将广播器中的监听器循环通知

public void multicastEvent(final ApplicationEvent event, ResolvableType eventType) {
ResolvableType type = eventType != null ? eventType : this.resolveDefaultEventType(event);
Iterator var4 = this.getApplicationListeners(event, type).iterator();
while(var4.hasNext()) {
final ApplicationListener<?> listener = (ApplicationListener)var4.next();
Executor executor = this.getTaskExecutor();
if (executor != null) {
executor.execute(new Runnable() {
public void run() {
SimpleApplicationEventMulticaster.this.invokeListener(listener, event);
}
});
} else {
this.invokeListener(listener, event);
}
}
}

参考:
https://www.jianshu.com/p/b86a7c8b3442
https://www.cnblogs.com/xbq8080/p/7768916.html
https://www.jb51.net/article/161033.htm
https://blog.csdn.net/ahilll/article/details/83785433

最后

以上就是年轻保温杯为你收集整理的springboot事件发布和监听器的全部内容,希望文章能够帮你解决springboot事件发布和监听器所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(43)

评论列表共有 0 条评论

立即
投稿
返回
顶部