软件编程
位置:首页>> 软件编程>> java编程>> SpringBoot深入分析讲解 * 模式上

SpringBoot深入分析讲解 * 模式上

作者:lhf2112  发布时间:2022-06-25 21:04:04 

标签:SpringBoot, , ,模式

SpringBoot深入分析讲解 * 模式上

注:图片来源于网络

SpringBoot作为业内公认的优秀开源框架,它的 * 是如何实现呢?在这里首先对一些基础组件进行分析;

1、事件ApplicationEvent

ApplicationEvent是一个抽象类,idea上展开其继承关系如图:

SpringBoot深入分析讲解 * 模式上

可见SpringBoot所定义的事件类型是极为丰富的。

2、 * ApplicationListener

ApplicationListener是一个接口,我们也可以通过实现这个接口来定义自己的 * ,可以通过与事件初始化器方式相似的方式进行加载。

@FunctionalInterface
public interface ApplicationListener<E extends ApplicationEvent> extends EventListener {
/**
* Handle an application event.
* @param event the event to respond to
*/
void onApplicationEvent(E event);
}

我们可以看到代码中它接受一个上文中提到的事件泛型,这代表了此 * 关注的事件;

还有一种实现 * 的方式,即实现SmartApplicationListener接口,SmartApplicationListener继承了ApplicationListener接口,通过这种方式实现 * ,可以同时注册多个感兴趣的事件,只需实现接口的supportsEventType方法即可;

public interface SmartApplicationListener extends ApplicationListener<ApplicationEvent>, Ordered {
/**
* Determine whether this listener actually supports the given event type.
* @param eventType the event type (never {@code null})
*/
boolean supportsEventType(Class<? extends ApplicationEvent> eventType);
/**
* Determine whether this listener actually supports the given source type.
* <p>The default implementation always returns {@code true}.
* @param sourceType the source type, or {@code null} if no source
*/
default boolean supportsSourceType(@Nullable Class<?> sourceType) {
return true;
}
/**
* Determine this listener's order in a set of listeners for the same event.
* <p>The default implementation returns {@link #LOWEST_PRECEDENCE}.
*/
@Override
default int getOrder() {
return LOWEST_PRECEDENCE;
}
}

3、事件广播器ApplicationEventMulticaster

ApplicationEventMulticaster是一个接口,定义了添加 * 、删除 * 、传播事件等方法;

SpringBoot为我们实现了SimpleApplicationEventMulticaster这一事件广播器,继承关系如图所示:

SpringBoot深入分析讲解 * 模式上

SpringBoot如何传播事件,有时间在下一篇博文进行整理,本文有哪些不对之处,也感谢大家的指正。

来源:https://blog.csdn.net/lhf2112/article/details/103796423

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com