Spring-context-ApplicationEvent/ApplicationListener/ApplicationEventMulticaster
示例代码:下面代码创建了一个喂兔粮的事件提醒,如果到中午12点了,就提示该喂兔粮了
public class MyEvent extends ApplicationEvent {public MyEvent(Object source) {super(source);}
}
public class MyListener implements ApplicationListener<MyEvent> {@Overridepublic void onApplicationEvent(MyEvent event) {System.out.println("该喂二抱三抱兔粮了,兔粮的品牌是:" + event.getSource());}
}
public static void main(String[] args) throws Exception {AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(MyListener.class);while(如果到中午12点){ctx.publishEvent(new MyEvent("洁西牌(JESSIE)"));}
}
ApplicationContext
接口实现了ApplicationEventPublisher
,所以具有publishEvent的功能,而publishEvent在底层调用的又是ApplicationEventMulticaster(实现类SimpleApplicationEventMulticaster)
的广播方法进行推送消息的
@Override
public void multicastEvent(final ApplicationEvent event, @Nullable ResolvableType eventType) {ResolvableType type = (eventType != null ? eventType : resolveDefaultEventType(event));Executor executor = getTaskExecutor();for (ApplicationListener<?> listener : getApplicationListeners(event, type)) {if (executor != null) {executor.execute(() -> invokeListener(listener, event));}else {invokeListener(listener, event);}}
}
Spring-context-ApplicationEvent/ApplicationListener/ApplicationEventMulticaster相关推荐
- 【spring】Spring事件监听器ApplicationListener的使用与源码分析
ApplicationEvent以及Listener是Spring为我们提供的一个事件监听.订阅的实现,内部实现原理是观察者设计模式,设计初衷也是为了系统业务逻辑之间的解耦,提高可扩展性以及可维护性. ...
- Spring的ApplicationEvent的使用
Spring的ApplicationEvent的使用 Spring 3.0中提供了很多类似*Aware的类,其中ApplicationContextAware接口可以实现我们在初始化bean的时候给b ...
- Spring Context 你真的懂了吗?
今天介绍一下大家常见的一个单词 context 应该怎么去理解,正确的理解它有助于我们学习 spring 以及计算机系统中的其他知识. 1. context 是什么 我们经常在编程中见到 contex ...
- java spring context_java将对象注册到spring context中 | 学步园
很多时候需要从spring中取出相应的对象,很多时候又需要将对象注入到spring context中. 小卒碰到了这样的问题.在构件中有DAO. 其他构件需要复用这个DAO,但是连接配置各不相同,并且 ...
- Spring context:component-scan代替context:annotation-config
Spring context:component-scan代替context:annotation-config XML: <?xml version="1.0" encod ...
- Arthas实践--获取到Spring Context,然后为所欲为
背景 Arthas 是Alibaba开源的Java诊断工具,深受开发者喜爱. https://github.com/alibaba/arthas Arthas提供了非常丰富的关于调用拦截的命令,比如 ...
- spring context标签
原文链接:http://kingliu.iteye.com/blog/1972678 spring context有以下标签: 1.annotation-config.作用是隐式地向Spring容器注 ...
- Spring中的ApplicationListener的使用详解案例(观察者模式)
1.ApplicationContext Spring的核心,Context我们通常解释为上下文环境.ApplicationContext则是应用的容器. Spring把Bean(object)放在容 ...
- 利用Spring的ApplicationEvent执行自定义方法
在Spring中已经定义了五个标准事件,分别介绍如下: 1)ContextRefreshedEvent:当ApplicationContext初始化或者刷新时触发该事件. 2)ContextClose ...
最新文章
- 【推荐】R for Data Science 新书抢先看
- onresize事件会被多次触发_玩转SpringBoot之通过事件机制参与SpringBoot应用的启动过程...
- 导出 C/C++ API 给 Lua 使用[转]
- OpenCV测试程序
- LeetCode50. Pow(x, n)(二进制位运算)
- Ajax调用服务器端C#方法
- 2022安徽合肥经济技术开发区招聘社区工作者冲刺试题及答案
- SNMP 简单网络管理协议
- 5输入的多数表决器(结构化建模)
- Google Play 开发者账号注册 上架应用
- 下载B站、秒拍等视频网站视频
- manjaro搭建java_Manjaro配置JDK
- 什么是CRM?多角度解析CRM系统
- 【验证工具类-ValidateUtil-java】
- 教育平台用户注册模块
- 转:WinCE驱动开发问题精华集锦
- 456. 132 模式
- jquery-1.11.1.js
- KVM安装部署搭建ngixn
- hdu3234 Exclusive-OR(带权并查集)
热门文章
- IDEA找到所有的断点
- mysql genlog 分析_使用mysqlsla 分析 mysql logs
- python是如何引起的_1.2.8 如何开始使用python
- sklearn逻辑回归参数设置_【机器学习笔记】:逻辑回归实战练习(二)
- 连接Oracle时报错ORA-28547
- QueryWrapper 一些常用操作 or like in 大于 小于 操作
- idea增加文件自动添加版本控制
- Android开发笔记(一百五十一)WebView与JavaScript交互的四种形式
- 操作系统知识点整理【均来自于网络】
- my-innodb-heavy-4G.cnf中文注释(主要配置文件注释)