自定义Spring event
通过Spring自定义event
首先我们定义我们的event类
package com.hyenas.spring.custom.event;
import org.springframework.context.ApplicationEvent;
public class CustomEvent extends ApplicationEvent{
private static final long serialVersionUID = -82737763905791865L;
public CustomEvent(Object source) {
super(source);
}
@Override
public String toString() {
return "my custom event";
}
}
然后我们实现一个ApplicationListener ,这个ApplicationListener是带泛型的,带入我们自定义的event类
package com.hyenas.spring.custom.event;
import org.springframework.context.ApplicationListener;
public class CustomEventHandler implements ApplicationListener<CustomEvent>{
@Override
public void onApplicationEvent(CustomEvent event) {
System.out.println(event.toString());
}
}
最后我们实现一个Publisher。
package com.hyenas.spring.custom.event; import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware; public class CustomEventPublisher implements ApplicationEventPublisherAware { private ApplicationEventPublisher publisher; @Override
public void setApplicationEventPublisher(ApplicationEventPublisher publisher) {
this.publisher = publisher;
} public void publish() {
CustomEvent event = new CustomEvent(this);
publisher.publishEvent(event);
} }
在这里我们也可以实现一个ApplicationContextAware,因为ApplicationContext接口也是继承了我们的ApplicationEventPublisher接口的。Spring源码如下
public interface ApplicationContext extends ListableBeanFactory, HierarchicalBeanFactory,
MessageSource, ApplicationEventPublisher, ResourcePatternResolver
我们实现ApplicationContextAware的话,会往我们的程序中setApplicationContext,ApplicationContext同样可以publishEvent
然后,我们定义我们的spring xml文件custom-event.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="customEventHandler"
class="com.hyenas.spring.custom.event.CustomEventHandler"/> <bean id="customEventPublisher"
class="com.hyenas.spring.custom.event.CustomEventPublisher"/> </beans>
好,最后我们写一个MainApp测试一下我们的程序吧。
package com.hyenas.spring.custom.event; import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainApp {
private static ConfigurableApplicationContext context; public static void main(String[] args) {
context = new ClassPathXmlApplicationContext("custom-event.xml"); CustomEventPublisher cvp =
(CustomEventPublisher) context.getBean("customEventPublisher");
cvp.publish();
cvp.publish();
}
}
运行结果
my custom event
my custom event
这样我们就完成了整个event从建立,到触发的过程。
自定义Spring event的更多相关文章
- EventBus VS Spring Event
EventBus VS Spring Event 本地异步处理,采用事件机制 可以使 代码解耦,更易读.事件机制实现模式是 观察者模式(或发布订阅模式),主要分为三部分:发布者.监听者.事件. Gua ...
- Spring Event事件驱动
Spring事件驱动模型,简单来说类似于Message-Queue消息队列中的Pub/Sub发布/订阅模式,也类似于Java设计模式中的观察者模式. 自定义事件 Spring的事件接口位于org.sp ...
- spring event
昨天看到了一遍关于spring event的帖子,觉得很好,就照着敲了一份代码,感觉对spring event有了进一步的认识.帖子链接:https://segmentfault.com/a/1190 ...
- 自定义spring参数注解 - 打破@RequestBody单体限制
本文主要描述怎样自定义类似@RequestBody这样的参数注解来打破@RequestBody的单体限制. 目录1 @RequestBody的单体限制2 自定义spring的参数注解3 编写sprin ...
- 自定义Spring Security的身份验证失败处理
1.概述 在本快速教程中,我们将演示如何在Spring Boot应用程序中自定义Spring Security的身份验证失败处理.目标是使用表单登录方法对用户进行身份验证. 2.认证和授权(Authe ...
- Spring Security学习笔记-自定义Spring Security过滤链
Spring Security使用一系列过滤器处理用户请求,下面是spring-security.xml配置文件. <?xml version="1.0" encoding= ...
- 自定义spring boot的自动配置
文章目录 添加Maven依赖 创建自定义 Auto-Configuration 添加Class Conditions 添加 bean Conditions Property Conditions Re ...
- 自定义spring boot starter 初尝试
自定义简单spring boot starter 步骤 从几篇博客中了解了如何自定义starter,大概分为以下几个步骤: 1 引入相关依赖: 2 生成属性配置类: 3 生成核心服务类: 4 生成自动 ...
- 23种设计模式之自定义Spring框架(五)
7,自定义Spring框架 7.1 spring使用回顾 自定义spring框架前,先回顾一下spring框架的使用,从而分析spring的核心,并对核心功能进行模拟. 数据访问层.定义UserDao ...
随机推荐
- JQery icheck 插件
<script type="text/javascript"> $(document).ready(function(){ var callbacks_list = $ ...
- JavaScript一些不常用的写法
如何写JavaScript才能逼格更高呢?怎样才能组织JavaScript才能让别人一眼看出你不简单呢?是否很期待别人在看完你的代码之后感叹一句“原来还可以这样写”呢?下面列出一些在JavaScrip ...
- android 48dp美化
48dp
- as3.0:文字 效果
//文字描边效果var tf1 = _root.createTextField("tf1", _root.getNextHighestDepth(), 10, 10, 0, 0); ...
- Nutch搜索引擎系列
下面是Nutch搜索系列目录,希望对研究Nutch的同学有所帮助. 目录安排: 1)Nutch搜索引擎(第1期)_ Nutch简介及安装[下载] 2)Nutch搜索引擎(第2期)_ Solr简介及安装 ...
- C166 Interfacing C to Assembler
Interfacing C to Assembler You can easily interface your C programs to routines written in XC16x/C16 ...
- 通过RDB还原用户误删除的邮件
1.在任意一台邮箱服务器上新建RDB数据库目录:C:\rdb01 2.使用Windows Server Backup还原已备份的邮件数据(mbdb02)库到 C:\rdb01 目录下 3.创建RDB数 ...
- 【M11】禁止异常流出析构方法之外
1.在两种情况下,调用析构方法:a.在正常状态下被销毁,栈上的对象离开作用域或者堆上的对象执行delete:b.抛出异常,堆栈回滚,栈上已经构造好的对象,也就是抛出异常之前的代码,自动调用析构方法.注 ...
- cookie标准话
php设置cookie setcookie( * * ,'/'); setcookie( * * ,'/'); //清除cookie setcookie("loginname",' ...
- Delphi开发OCX详细步骤总结
首先要弄明白你要写的OCX是用在客户端还是用在服务器端 假如用在客户端: 1.创建 打开delphi 7,选择菜单"new"->"other"- ...