spring事件驱动过程分析

Spring事件驱动过程分析


在看Spring源码之前确保理解了JDK中的 EventObject,EventListener的使用场景,其实体现的是观察者模式,适用于很多场景。

在Spring中,事件基类ApplicationEvent,在应用的周期中触发(发布)相应的事件。

public abstract class ApplicationEvent extends EventObject {

	/** use serialVersionUID from Spring 1.2 for interoperability */
	private static final long serialVersionUID = 7099057708183571937L;

	/** System time when the event happened */
	private final long timestamp;


	/**
	 * Create a new ApplicationEvent.
	 * @param source the object on which the event initially occurred (never {@code null})
	 */
	public ApplicationEvent(Object source) {
		super(source);
		this.timestamp = System.currentTimeMillis();
	}


	/**
	 * Return the system time in milliseconds when the event happened.
	 */
	public final long getTimestamp() {
		return this.timestamp;
	}

}

监听器基类如下,在接收到事件后如果是自己感兴趣的就进行相应处理。

public interface ApplicationListener<E extends ApplicationEvent> extends EventListener {

	/**
	 * Handle an application event.
	 * @param event the event to respond to
	 */
	void onApplicationEvent(E event);

}

Spring boot在启动的时候会加载一部分 ApplicationListener。Spring Context加载初始化完成(refresh)后会再次检测应用中的 ApplicationListener,并且注册,此时会将我们实现的 ApplicationListener 就会加入到 SimpleApplicationEventMulticaster 维护的 Listener 集合中。


事件触发的流程是怎样的?以 ContextRefreshedEvent 为例。在Context初始化完成后,在 finishRefresh 方法中会发布 ContextRefreshedEvent 事件。

protected void finishRefresh() {
	// Initialize lifecycle processor for this context.
	initLifecycleProcessor();

	// Propagate refresh to lifecycle processor first.
	getLifecycleProcessor().onRefresh();

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

	// Participate in LiveBeansView MBean, if active.
	LiveBeansView.registerApplicationContext(this);
}
protected void publishEvent(Object event, ResolvableType eventType) {
	// Decorate event as an ApplicationEvent if necessary
	ApplicationEvent applicationEvent;
	if (event instanceof ApplicationEvent) {
		applicationEvent = (ApplicationEvent) event;
	}
	else {
		applicationEvent = new PayloadApplicationEvent<Object>(this, event);
		if (eventType == null) {
			eventType = ((PayloadApplicationEvent)applicationEvent).getResolvableType();
		}
	}

	// Multicast right now if possible - or lazily once the multicaster is initialized
	if (this.earlyApplicationEvents != null) {
		this.earlyApplicationEvents.add(applicationEvent);
	}
	else { // 多播事件
		getApplicationEventMulticaster().multicastEvent(applicationEvent, eventType);
	}

	// Publish event via parent context as well... 上层的Context也会响应该事件
	if (this.parent != null) {
		if (this.parent instanceof AbstractApplicationContext) {
			((AbstractApplicationContext) this.parent).publishEvent(event, eventType);
		}
		else {
			this.parent.publishEvent(event);
		}
	}
}
public void multicastEvent(final ApplicationEvent event, ResolvableType eventType) {
	ResolvableType type = (eventType != null ? eventType : resolveDefaultEventType(event));
	for (final ApplicationListener<?> listener : getApplicationListeners(event, type)) {
		Executor executor = getTaskExecutor();
		// 如果设置了 Executor 则异步执行
		if (executor != null) {
			executor.execute(new Runnable() {
				@Override
				public void run() {
					invokeListener(listener, event);
				}
			});
		}
		else {// 否则同步调用listener的事件处理方法
			invokeListener(listener, event);
		}
	}
}

内部 listener 是如何维护的?使用的哪种集合?是否线程安全?

org.springframework.context.event.AbstractApplicationEventMulticaster.ListenerRetriever

private class ListenerRetriever {
    // 这里
	public final Set<ApplicationListener<?>> applicationListeners;

	public final Set<String> applicationListenerBeans;

	private final boolean preFiltered;

	public ListenerRetriever(boolean preFiltered) {
		this.applicationListeners = new LinkedHashSet<ApplicationListener<?>>();
		this.applicationListenerBeans = new LinkedHashSet<String>();
		this.preFiltered = preFiltered;
	}

	public Collection<ApplicationListener<?>> getApplicationListeners() {
		LinkedList<ApplicationListener<?>> allListeners = new LinkedList<ApplicationListener<?>>();
		for (ApplicationListener<?> listener : this.applicationListeners) {
			allListeners.add(listener);
		}
		if (!this.applicationListenerBeans.isEmpty()) {
			BeanFactory beanFactory = getBeanFactory();
			for (String listenerBeanName : this.applicationListenerBeans) {
				try {
					ApplicationListener<?> listener = beanFactory.getBean(listenerBeanName, ApplicationListener.class);
					if (this.preFiltered || !allListeners.contains(listener)) {
						allListeners.add(listener);
					}
				}
				catch (NoSuchBeanDefinitionException ex) {
					// Singleton listener instance (without backing bean definition) disappeared -
					// probably in the middle of the destruction phase
				}
			}
		}
		// 根据Order注解排序
		AnnotationAwareOrderComparator.sort(allListeners);
		return allListeners;
	}
}

在增加元素到Set的时候有同步操作。

// 使用 ListenerRetriever 作为加锁对象
private Object retrievalMutex = this.defaultRetriever;

public void addApplicationListener(ApplicationListener<?> listener) {
	synchronized (this.retrievalMutex) {
		this.defaultRetriever.applicationListeners.add(listener);
		this.retrieverCache.clear();
	}
}

public void addApplicationListenerBean(String listenerBeanName) {
	synchronized (this.retrievalMutex) {
		this.defaultRetriever.applicationListenerBeans.add(listenerBeanName);
		this.retrieverCache.clear();
	}
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ATDD是指行为驱动开发(Acceptance Test-Driven Development),它是TDD的一种扩展形式。ATDD的核心思想是通过编写可执行的验收测试来驱动软件开发的过程。这些验收测试描述了系统的期望行为,并且是与业务利益相关的。ATDD的目标是确保开发团队和业务利益相关者之间的共同理解,并促进更好的沟通和协作。 在Spring框架中,可以使用Cucumber来实现ATDD。Cucumber是一个行为驱动开发的工具,它使用自然语言来描述系统的行为,并将这些描述转化为可执行的测试。通过编写Cucumber的特性文件和步骤定义,开发团队和业务利益相关者可以共同参与到测试的编写和执行过程中。 引用\[3\]中提到了如何训练引导上下文来执行任何你喜欢的操作。可以通过在org.springframework.cloud.bootstrap.BootstrapConfiguration键下添加条目/META-INF/spring.factories来创建要用于自动装配的主应用程序上下文的任何bean。这意味着你可以在这里编写和配置你的ATDD测试所需的bean,并使用Cucumber来执行这些测试。 总结起来,ATDD是一种行为驱动开发的方法,它通过编写可执行的验收测试来驱动软件开发过程。在Spring框架中,可以使用Cucumber来实现ATDD,并通过配置主应用程序上下文的bean来支持测试的自动装配。 #### 引用[.reference_title] - *1* [高效的敏捷测试第九课 ATDD、需求分析、需求评审和设计评审](https://blog.csdn.net/fegus/article/details/124906829)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [springcloud中文手册API](https://blog.csdn.net/wudaoshihun/article/details/83034145)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值