自定义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 ...
随机推荐
- ASP.NET读取配置文件发送邮件
之前写过一篇文章C#使用SMTP发送邮件 后来做了改进,改成读取独立的配置文件,本文只记录读取配置文件的部分,发送部分见上面的链接. 读取配置文件C#代码: using System; using S ...
- 在sql中使用了 hashbytes 函数
在做项目的时候,在sql中使用了hashbytes函数,运用md5算法去加密一密码.代码如下 DECLARE @psw nvarchar(20) SET @psw = 'admin' SELECT h ...
- C:基本语句
基本语句知识 do{}while(); 与 while()do{}:for :while 语句的区别: while()do{}:先判断条件是否成立,条件满足则执行循环体 do{}while();是先执 ...
- ASP.NET|跳转(redirect)到其它站点,提示:当前线程终止。
在XAF中,如果使用HttpContext.Response.Redirect()进行跳转,会出现”当前线程终止“的报错,跳转失败. 这时候,应该改用WebApplication.Redirect() ...
- AP(应付帐管理)
--更新供应商地点 PROCEDURE update_vendor_site(p_init_msg_list IN VARCHAR2 DEFAULT fnd_api.g_false, x_return ...
- ......那么Win8.1怎么去掉文件夹?
(从已经死了一次又一次终于挂掉的百度空间人工抢救出来的,发表日期2014-05-11) 细心的朋友会发现,在Win8.1这台电脑(计算机)中,除了我们最熟悉的磁盘外,还新增了视频.图片.文档.下载.音 ...
- 电子图书的编目和OPAC揭示
摘 要 步入数字图书馆时代,电子资源日益增多,尤其在高校图书馆更加迅猛,怎样将图书馆的电子资源尽可能地整合在OPAC系统的检索功能里,以便更加简便.直观地利用,电子图书的编目已成为重要研究课题.本 ...
- Jdk和Jre目录和三个lib目录说明----外部扩展jar包servlet,mysql,oracle等
以下文章转载自a personal blog:For Future,因为昨天下午在cmd模式下编译servlet失败,后来在网上找到这篇文章帮我解决了该问题,我觉得挺值得收藏的,并且这篇文章对&quo ...
- CodeForces 176C Playing with Superglue 博弈论
Playing with Superglue 题目连接: http://codeforces.com/problemset/problem/176/C Description Two players ...
- sscanf和sprintf是scanf和printf家族用法 (转)
sscanf和sprintf是scanf和printf家族用法 sscanf和sprintf是scanf和printf家族的一对成员,用于处理和分析字符串非常强大得两个函数头文件 stdio.h原型i ...