SpringBoot深入分析讲解 * 模式上
作者:lhf2112 时间:2022-06-25 21:04:04
注:图片来源于网络
SpringBoot作为业内公认的优秀开源框架,它的 * 是如何实现呢?在这里首先对一些基础组件进行分析;
1、事件ApplicationEvent
ApplicationEvent是一个抽象类,idea上展开其继承关系如图:
可见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如何传播事件,有时间在下一篇博文进行整理,本文有哪些不对之处,也感谢大家的指正。
来源:https://blog.csdn.net/lhf2112/article/details/103796423
标签:SpringBoot, , ,模式
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
SpringBoot自动配置原理详解
2023-08-19 09:25:55
![](https://img.aspxhome.com/file/2023/6/58096_0s.jpg)
Spring boot整合log4j2过程解析
2023-11-29 10:47:53
![](https://img.aspxhome.com/file/2023/7/60107_0s.png)
java中transient关键字用法分析
2022-01-22 04:27:05
Android基于SoftReference缓存图片的方法
2023-07-13 00:53:21
Spring Boot 详细分析Conditional自动化配置注解
2021-11-25 21:56:14
![](https://img.aspxhome.com/file/2023/9/61799_0s.png)
java面试常见问题之Hibernate总结
2023-11-27 10:37:05
java中多个@Scheduled定时器不执行的解决方法
2023-03-19 02:32:34
springboot自动扫描添加的BeanDefinition源码实例详解
2023-11-24 15:15:22
![](https://img.aspxhome.com/file/2023/3/59943_0s.png)
java字符串的重要使用方法以及实例
2023-11-13 23:11:51
JAVA中的Token 基于Token的身份验证实例
2023-11-09 18:05:09
Java 在PDF中添加骑缝章示例解析
2023-11-24 22:41:35
![](https://img.aspxhome.com/file/2023/5/59985_0s.png)
Java 随机生成验证码(支持大小写字母、数字、随机字体)的实例
2023-11-25 00:35:38
Flutter加载图片流程MultiFrameImageStreamCompleter解析
2023-07-19 02:45:55
基于SSM实现学生管理系统
2023-11-24 18:17:39
![](https://img.aspxhome.com/file/2023/0/59190_0s.jpg)
关于activemq安装配置以及启动错误的解决
2023-11-13 05:18:02
![](https://img.aspxhome.com/file/2023/5/59485_0s.png)
SpringBoot中如何对actuator进行关闭
2022-11-30 01:56:37
![](https://img.aspxhome.com/file/2023/9/62349_0s.png)
Java与C++实现相同的MD5加密算法简单实例
2023-08-31 09:43:02
![](https://img.aspxhome.com/file/2023/2/60892_0s.jpg)
解决springmvc关于前台日期作为实体类对象参数类型转换错误的问题
2023-11-28 20:53:42
![](https://img.aspxhome.com/file/2023/3/60813_0s.png)
Java SpringMVC异步处理详解
2021-08-10 15:03:58
![](https://img.aspxhome.com/file/2023/0/62050_0s.png)
MyBatis 中 ${}和 #{}的正确使用方法(千万不要乱用)
2023-11-29 05:02:37