自定义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中使用 Response.Write("<script>alert('****');</script>");后CSS界面发生变化的解决方法 (经验证)
在后台使用Response.Write("<script>alert('Hello World');</script>);弹出alert窗口后发现网页的界面和原来CS ...
- maven 基础整理
教程 依赖管理 IDE设置121 IntelliJ,Edit Configurations中添加maven,选中 Resolve Workspace artifacts能自动编译依赖模块 内置命令 m ...
- C#自定义控件背景色透明的方法
I struggled for ages with the problem of having controls show through a control that was painted on ...
- ssh-keygen 无密码登陆需要注意一个问题
从今天开始记录一下工程上的小知识. ssh-keygen -t rsa cat id_rsa.pub >> authorized_keys后,切记,将authrorized_keys文件的 ...
- Codeforce Gym 100015I Identity Checker 暴力
Identity Checker 题目连接: http://codeforces.com/gym/100015/attachments Description You likely have seen ...
- git常见问题
一.设置git的user name和email: git config --global user.name "lei.li" git config --global user.e ...
- unity3d快捷键大全
Unity是由Unity Technologies开发的一个让玩家轻松创建诸如三维视频游戏.建筑可视化.实时三维动画等类型互动内容的多平台的综合型游戏开发工具,是一个全面 整合的专业游戏引擎.Unit ...
- 深入了解android平台的jni---本地多线程调用java代码
一.jni调用java对象 JNI提供的功能之一是在本地代码中使用Java对象.包括:创建一个java类对象和通过函数传递一个java对象.创建一个java类对象,首先需要得到得到使用Find ...
- js代码的一些小技巧
1. 数组中通过赋值语句来改变值 var a = 1; var msg = ["value0","value1"]; for(var i = 0;i<10 ...
- mysql优化之连接优化(open-files-limit与table_open_cache)
MySQL打开的文件描述符限制 Can't open file: '.\test\mytable.frm' (errno: 24) OS error code : Too many open file ...