Spring 框架笔记—4.16.2 标准与自定义事件
Event handling in the
ApplicationContext is provided through the
ApplicationEvent class and
ApplicationListener interface. If a bean that implements the
ApplicationListener interface is deployed into the context, every time an
ApplicationEvent gets published to the
ApplicationContext, that bean is notified. Essentially, this is the standard Observer design pattern. Spring 内置提供的标准事件包括:
ContextRefreshedEvent
Published when the
ApplicationContext is initialized or refreshed, for example, using the refresh() method on the
ConfigurableApplicationContext interface. "Initialized" here means that all beans are loaded, post-processor beans are detected and activated, singletons are pre-instantiated, and the ApplicationContext object is ready for use. As long as the context has not been closed, a refresh can be triggered multiple times, provided that the chosen ApplicationContext actually supports such "hot" refreshes. For example, XmlWebApplicationContext supports hot refreshes, but GenericApplicationContext does not.
ContextStartedEvent
Published when the ApplicationContext is started, using the start() method on the ConfigurableApplicationContext interface. "Started" here means that all Lifecycle beans receive an explicit start signal. Typically this signal is used to restart beans after an explicit stop, but it may also be used to start components that have not been configured for autostart , for example, components that have not already started on initialization.
ContextStoppedEvent
Published when the ApplicationContext is stopped, using the stop() method on the ConfigurableApplicationContext interface. "Stopped" here means that all Lifecycle beans receive an explicit stop signal. A stopped context may be restarted through a start() call.
ContextClosedEvent
Published when the ApplicationContext is closed, using the close() method on the ConfigurableApplicationContext interface. "Closed" here means that all singleton beans are destroyed. A closed context reaches its end of life; it cannot be refreshed or restarted.
RequestHandledEvent
A web-specific event telling all beans that an HTTP request has been serviced. This event is published after the request is complete. This event is only applicable to web applications using Spring’s DispatcherServlet.
创建自己的事件只需要继承ApplicationEvent类,如下所示:
public class BlackListEvent extends ApplicationEvent {
private final String address;
private final String test;
public BlackListEvent(Object source, String address, String test) {
super(source);
this.address = address;
this.test = test;
}
// accessor and other methods...
}
如果我们要发布上述事件,我们需要调用
ApplicationEventPublisher接口的publishEvent()方法,通常我们可以创建一个类实现ApplicationEventPublisherAware接口,并将它注册为Bean.示例如下:
public class EmailService implements ApplicationEventPublisherAware {
private List<String> blackList;
private ApplicationEventPublisher publisher;
public void setBlackList(List<String> blackList) {
this.blackList = blackList;
}
public void setApplicationEventPublisher(ApplicationEventPublisher publisher) {
this.publisher = publisher;
}
public void sendEmail(String address, String text) {
if (blackList.contains(address)) {
BlackListEvent event = new BlackListEvent(this, address, text);
publisher.publishEvent(event);
return;
}
// send email...
}
}
在配置时,Spring容器会探测到EmailService 实现了ApplicationEventPublisherAware接口并自动的调用setApplicationEventPublisher()方法。实际上这个方法的参数就是Spring容器本身;通过ApplicationEventPublisher 接口与我们便可以与应用程序上下文进行交互。
如果要消费这个事件,则需要编写一个类实现ApplicationListener 接口并将之注册为Spring 的bean.以下示例阐述了这一点。
public class BlackListNotifier implements ApplicationListener<BlackListEvent> {
private String notificationAddress;
public void setNotificationAddress(String notificationAddress) {
this.notificationAddress = notificationAddress;
}
public void onApplicationEvent(BlackListEvent event) {
// notify appropriate parties via notificationAddress...
}
}
Notice that ApplicationListener is generically parameterized with the type of your custom event, BlackListEvent. This means that the onApplicationEvent() method can remain type-safe, avoiding any need for downcasting.
You may register as many event listeners as you wish, 需要注意的一点是所有事件监听都是同步的接收事件.这就表示 publishEvent() 方法被阻塞直到所有监听器完成事件处理. 这种同步和单线程方式的一个优点是,当监听器接收到事件时,如果发现事务上下文可用,那么它将在发布服务者的事务上下文中操作。如果我们想要定制事件发布的一些策略,可了解Spring的ApplicationEventMulticaster接口。
分享到:
相关推荐
Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的镜像中
勇敢:核心图书馆 io.zipkin.brave/brave/4.16.2/brave-4.16.2.jar
Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的镜像中
Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的镜像中
Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的镜像中
Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的镜像中
Website Auditor for Mac是Mac OS上一款SEO优化软件,能够查看对手在搜索引擎上的排名情况,并为你提供一些优化报告和建议,快来下载体验吧。 软件介绍 WebSite Auditor是一个必须具有的onpage优化工具,网站管理员...
webpack4.16.2-配置
4K视频下载http://www.4kgood.com,4K Video Downloader For mac 是一款视频下载器,视频网站下载高清视频及字幕,也可以下载的整个播放列表或者下载订阅频道,可以选择下载视频的清晰度(分辨率)及格式。
ckeditor for jave上传图片
4. 软件框架 3 4.1模块描述 3 4.2软件架构 4 4.3模块加载机制 5 4.4应用(Applications) 6 4.4.1电话生命周期 6 4.4.2 mod_conference模块 8 4.4.3 mod_voicemail模块 14 4.4.4 mod_commands 14 4.4.5 mod_dptools 17...
4.16.2 基本事件流 19 4.16.3 活动图 19 4.17 管理管理员 20 4.17.1 用例简述 20 4.17.2 基本事件流 20 4.17.3 活动图 20 4.18 付款状态批处理 20 4.18.1 用例简述 20 4.18.2 基本事件流 21 4.18.3 活动图 21 4.19 ...
利用jsp实现ckeditor_3_6_2 上传图片功能,里面包含完整的工程,不过需要自己引入commons-fileupload-1.2.1.jar,commons-io-1.4.jar文件上传包,网上一大把。只要能正确引入,该工程就能完成
上传图片功能设置的开启,处理Action在config.js中
富文本控件的4.0版本,可以进行在线编辑,上传文件的强大控件
第4章 Web程序设计中的字符串与正则表达式动手实验 276 4.1 实验1 Sort()方法和Reverse()方法 276 4.1.1 实验目标 276 4.1.2 实验步骤 276 4.2 实验2 String类型是引用类型 278 4.2.1 实验目标 278 4.2.2 实验步骤 ...
最初是因为我们需要这样一个SQL服务器,它能处理与任何可不昂贵硬件平台上提供数据库的厂家在一个数量级上的大型数据库,但速度更快,MySQL就开发出来。自1996年以来,我们一直都在使用MySQL,其环境有超过 40 个...
ExpressJS:4.16.2 Mongodb驱动器:3.1.0-beta4 摩卡:5.1.1, Mongo-mock:3.1.0 设置 要求 的NodeJS:8.11.1 npm:5.6.0 安装 克隆存储库 运行命令npm install 使用npm run dev命令启动项目 服务从以下端口...
发布地图依赖关系节点 v7.4.0 Socket.io v2.0.3 快递 v4.16.2
资源来自pypi官网。 资源全名:transformers-4.7.0-py3-none-any.whl