Spring之事件机制详解,看完老板就给加薪了

ApplicationEventMulticaster类:

在观察者模式中,一定要有一个管理维护监听者列表的功能。在Spring的事件机制中,将维护监听者列表的功能单独定义了一个接口,即ApplicationEventMulticaster接口。这也体现了单一责任原则的设计思想。我们看其源码:


public interface ApplicationEventMulticaster {



	/**

	 * Add a listener to be notified of all events.

	 * @param listener the listener to add

	 */

	void addApplicationListener(ApplicationListener<?> listener);



	/**

	 * Add a listener bean to be notified of all events.

	 * @param listenerBeanName the name of the listener bean to add

	 */

	void addApplicationListenerBean(String listenerBeanName);



	/**

	 * Remove a listener from the notification list.

	 * @param listener the listener to remove

	 */

	void removeApplicationListener(ApplicationListener<?> listener);



	/**

	 * Remove a listener bean from the notification list.

	 * @param listenerBeanName the name of the listener bean to remove

	 */

	void removeApplicationListenerBean(String listenerBeanName);



	/**

	 * Remove all listeners registered with this multicaster.

	 * <p>After a remove call, the multicaster will perform no action

	 * on event notification until new listeners are registered.

	 */

	void removeAllListeners();



	/**

	 * Multicast the given application event to appropriate listeners.

	 * <p>Consider using {@link #multicastEvent(ApplicationEvent, ResolvableType)}

	 * if possible as it provides better support for generics-based events.

	 * @param event the event to multicast

	 */

	void multicastEvent(ApplicationEvent event);



	/**

	 * Multicast the given application event to appropriate listeners.

	 * <p>If the {@code eventType} is {@code null}, a default type is built

	 * based on the {@code event} instance.

	 * @param event the event to multicast

	 * @param eventType the type of event (can be {@code null})

	 * @since 4.2

	 */

	void multicastEvent(ApplicationEvent event, @Nullable ResolvableType eventType);



} 

可以看到,这个接口定义了增删查监听者的方法,所以,监听者列表的维护通过这个接口实现。需要注意的是这个接口还定义了multicastEvent方法。通过这个方法,将事件传给监听器。所以这个类,将事件和监听器,连接在一起。这里采用的是中介者模式,这个接口就是中介者角色。

ApplicationEventPublisher:

Spring设计的事件发布类,我们看其源码:


public interface ApplicationEventPublisher {



	/**

	 * Notify all <strong>matching</strong> listeners registered with this

	 * application of an application event. Events may be framework events

	 * (such as ContextRefreshedEvent) or application-specific events.

	 * <p>Such an event publication step is effectively a hand-off to the

	 * multicaster and does not imply synchronous/asynchronous execution

	 * or even immediate execution at all. Event listeners are encouraged

	 * to be as efficient as possible, individually using asynchronous

	 * execution for longer-running and potentially blocking operations.

	 * @param event the event to publish

	 * @see #publishEvent(Object)

	 * @see org.springframework.context.event.ContextRefreshedEvent

	 * @see org.springframework.context.event.ContextClosedEvent

	 */

	default void publishEvent(ApplicationEvent event) {

		publishEvent((Object) event);

	}



	/**

	 * Notify all <strong>matching</strong> listeners registered with this

	 * application of an event.

	 * <p>If the specified {@code event} is not an {@link ApplicationEvent},

	 * it is wrapped in a {@link PayloadApplicationEvent}.

	 * <p>Such an event publication step is effectively a hand-off to the

	 * multicaster and does not imply synchronous/asynchronous execution

	 * or even immediate execution at all. Event listeners are encouraged

	 * to be as efficient as possible, individually using asynchronous

	 * execution for longer-running and potentially blocking operations.

	 * @param event the event to publish

	 * @since 4.2

	 * @see #publishEvent(ApplicationEvent)

	 * @see PayloadApplicationEvent

	 */

	void publishEvent(Object event);



} 

里面定义了publishEvent方法,进行事件的发布。但是事件不是直接发布到listener中,而是发布在ApplicationEventMulticaster类中,所以在ApplicationEventPublisher类中,一定会有ApplicationEventMulticaster对象,将事件发布到ApplicationEventMulticaster中。

事件流程总结:

通过上面几个类的描述,我们总结一下spring事件机制的流程:

流程的核心,就是PublishEvent。Event对象以参数的形式传入PublishEvent对象。然后将Event事件传入ApplicationEventMulticaster类中,由ApplicationEventMulticaster类将事件传给其维护的监听者,执行监听者方法。

[](

)领悟


由上面Spring设计事件模式思路我们可以感受到,Spring把单一的功能,都拎出来形成了一套接口规范,然后多个接口规范组合,去完成一件事情。所以我们在阅读源码时会感觉很乱。只要我们分析清楚每个对象的设计思路和作用是什么,再分析他们之间的组合完成了什么事情,就很容易理解其设计理念了。

[](

)应用


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值