自定义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 ...
随机推荐
- Spring REST实践之Spring Boot
Spring Boot基本描述 可以利用http://start.spring.io网站的进行Spring Boot的初始化构建.这个初始化构建器允许你输入工程基本信息.挑选工程支持的功能,最后会生成 ...
- (剑指Offer)面试题33:把数组排成最小的数
题目: 输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个.例如输入数组{3,32,321},则打印出这三个数字能排成的最小数字为321323. 思路: 1.全 ...
- [前端引用] 利用ajax实现类似php include require 等命令的功能
利用ajax实现类似php中的include.require等命令的功能 最新文件下载: https://github.com/myfancy/ajaxInclude 建议去这里阅读readme-2. ...
- CarDAQ-Plus
Overview CarDAQ-Plus is the most validated and accepted J2534 device in the world. It has been on th ...
- PL/pgSQL学习笔记之三
http://www.postgresql.org/docs/9.1/static/plpgsql-overview.html 39.1.2. Supported Argument and Resul ...
- Windows Server 2003下ASP.NET无法识别IE11的解决方法
由于IE11对User-Agent字符串进行了比较大的改动,所以导致很多通过User-Agent来识别浏览器的程序,都相应的出现了无法识别IE11的情况.(普通用户端则可以通过这个方法来进行设置.) ...
- git status对应中文名称
git status显示出来的中文文档是一串字符码,不能正常识别 git config --global core.quotepath false 这一句代码就可以帮你
- (转)用AGG实现高质量图形输出(三)
转自 :http://www.cnblogs.com/CoolJie/archive/2011/04/27/2030260.html 线段生成器(Span Generator) 我们前面举的例子使用的 ...
- 【C语言】模拟实现库函数strcat函数
//模拟实现库函数strcat函数 #include <stdio.h> #include <string.h> #include <assert.h> char ...
- CircleWaveProgressBar
https://github.com/eltld/CircleWaveProgressBar