onApplicationEvent调用了2次,3次,多次
onApplicationEvent调用了2次,3次,多次 解决方案
首先,被执行这么多次,肯定是自己配置的问题,我以spring boot为例来说
我也遇到了执行三次,排查后发现一个是spring boot的,另外两个是自己配的,所以才导致三次,理论上可以导致n次的
三个事件源分别为:
- spring boot 自带的注解形式上下文(解析所有通过开放端口发来的请求)
org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@184497d1, started on Sat Oct 12 10:33:03 CST 2019
- 开发者自己创建的(仅负责解析 /ui/** 请求)
org.springframework.context.event.ContextRefreshedEvent[source=WebApplicationContext for namespace 'ui-servlet', started on Sat Oct 12 10:34:53 CST 2019, parent: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@184497d1]
- 开发者自己创建的(仅负责解析 /api/** 请求)
org.springframework.context.event.ContextRefreshedEvent[source=WebApplicationContext for namespace 'api-servlet', started on Sat Oct 12 10:35:06 CST 2019, parent: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@184497d1]
可以看到第一个是spring boot自身的,另外两个是 WebApplicationContext 的不同空间,而他们有同样的父容器(即第一个)
后面两个怎么来的?
@Configuration
public class ServletConfig {@Beanpublic ServletRegistrationBean dispatcherUI() {AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext();applicationContext.scan("com.xxx.*.controller.ui");DispatcherServlet dispatcherServlet = new DispatcherServlet(applicationContext);ServletRegistrationBean servletRegistration = new ServletRegistrationBean(dispatcherServlet,"/ui/*");servletRegistration.setName("ui"); // 起名return servletRegistration;}@Beanpublic ServletRegistrationBean dispatcherApi() {AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext();applicationContext.scan("com.xxx.*.controller.api");DispatcherServlet dispatcherServlet = new DispatcherServlet(applicationContext);ServletRegistrationBean servletRegistration = new ServletRegistrationBean(dispatcherServlet,"/api/*");servletRegistration.setName("api"); // 起名return servletRegistration;}
}
同时验证了 spring为它命名是 xxx-servlet,xxx是自己起的。
只想让它执行一次?
可见onApplicationEvent被调用多次并不是spring的bug,而是spring为开发者留的口子,即允许开发者自由自在的关心哪个,执行哪个即可,如果你不说,我就每次都通知你。
一般来说如果作为启动配置,不是特定容器的配置的话,只关心spring boot本身那个就好了,判断一下parent 是否为空即可。若为空则证明是root applicationContext。
onApplicationEvent调用了2次,3次,多次相关推荐
- Spring5源码 - 13 Spring事件监听机制_@EventListener源码解析
文章目录 Pre 概览 开天辟地的时候初始化的处理器 @EventListener EventListenerMethodProcessor afterSingletonsInstantiated 小 ...
- EventListener
一.@EventListener 演示案例 写一个Userservice,在方法上标注@EventListener,属性是监听的事件Class. 如果监听到此事件Class,则会执行被注解的方法. @ ...
- 七.Spring之ApplicationListener事件监听、@EventListener
看看注释:由应用事件监听器实现的接口,基于观察者设计模式. 方法是处理应用事件. /**由应用事件监听器实现的接口,基于观察者设计模式* Interface to be implemented by ...
- dubbo请求调用过程分析
服务消费方发起请求 当服务的消费方引用了某远程服务,服务的应用方在spring的配置实例如下: <dubbo:reference id="demoService" inter ...
- html5调用系统声音1s响一次_记录一次系统性能调优过程
在线上环境,由于业务场景需要,要求程序能够在普通的4G机器中依然正常运行. 而原来的环境配置为8核16G,微服务部署,一共有6个功能模块. 而现在要求在一台4核4G的设备上正常运行. 问题清单 模块合 ...
- java调用支付接口实例_Java 调用支付宝接口
1.注册开发者中心应用 登录点击沙箱服务 https://open.alipay.com/platform/developerIndex.htm 2.创建沙箱应用获取APPID和支付宝网关地址(创 ...
- controller调用另一个controller中的方法 获取返回值_必须掌握!你知道 Spring 中运用的 9 种设计模式吗 ?...
Spring中涉及的设计模式总结,在面试中也会经常问道 Spring 中设计模式的问题.本文以实现方式.实质.实现原理的结构简单介绍 Sping 中应用的 9 种设计模型,具体详细的刨析会在后面的文章 ...
- mqtt 发送消息过多_阿里云MQTT服务端注解式消息处理分发与同步调用实践小结
一.前言 前段时间公司预研了设备app端与服务端的交互方案,出于多方面考量最终选用了阿里云的微服务队列MQTT方案,基于此方案,本人主要实践有: 1. 封装了RocketMQ实现MQTT订阅与发布的实 ...
- Java api 调用Sqoop2进行MySQL--Hive的数据同步
1.相关jar包 2.一些需要的参数定义在message.properties中 jdbcHiveUrl=jdbc:hive2://10.1.9.91:10000 //hive地址jdbcHiveDr ...
最新文章
- 60行代码爬取知乎“神回复”,句句戳中泪点
- java中final class的一点思考
- 计算机二级excel13,计算机二级Excel篇-实操真题详解13
- OpenCV边缘之间的距离转换功能的实例(附完整代码)
- Scala教程之:PartialFunction
- 【JUC】第三章 多线程锁、CallableFuture 接口
- ISA2006之SP1的作用
- linux之压缩文件,查看压缩文件,解压文件的操作命令
- 激光雷达原理及发展现状
- 【防火墙流控配置 基于主机的带宽控制】
- python毕业设计项目源码选题(5)校园网站系统毕业设计毕设作品开题报告开题答辩PPT
- amd显卡用黑苹果输出黑屏_微星HD7850显卡DVI接口黑屏,改DSDT无果
- 如何避免高不成低不就? 疫情当下Java学习路线分享
- uni-app(Vue.js)创建运行微信小程序
- 交互设计书单--西南交大课程推荐
- 游戏3D美术设计师前景怎么样?
- 洛谷-P2550-彩票摇奖
- c语言程序设计李学刚戴白刃答案,李学刚
- HBS服务器什么样装系统,GitHub - unclay/hbs-test: hbs入门测试
- 整合优势资源+智慧科技管理 众美集团创新终端物业服务