spring中的事件 applicationevent 讲的确实不错
event,listener是observer模式一种体现,在spring 3.0.5中,已经可以使用annotation实现event和eventListner里。
我们以spring-webflow里的hotel booking为例,看一下实现,步骤如下:
1,建立event
public class BookingCreatedEvent extends ApplicationEvent {
private static final long serialVersionUID = 3039313222160544111L;
private Booking booking;
public BookingCreatedEvent(Object source) {
super(source);
}
public BookingCreatedEvent(Object source, Booking booking) {
super(source);
this.booking = booking;
}
public Booking getBooking() {
return booking;
}
}
event需要继承ApplicationEvent。
2,建立listener
@Component
public class BookingEventsListener implements ApplicationListener<BookingCreatedEvent> {
private static final Logger log = Logger.getLogger(); //listener实现
public void onApplicationEvent(BookingCreatedEvent event) {
log.debug("bookingId:" + event.getBooking().getId());
//do something
}
}
listener需要实现ApplicationListener。
注意在spring 3.0.5中的ApplicationListener是带泛型的,这样BookingEventsListener只会监听BookingCreatedEvent事件。
另外可以用@Component来注册组件,这样就不需要在spring的配置文件中指定了。
3,触发event
@Service("bookingService")
@Repository
public class JpaBookingService implements BookingService, ApplicationContextAware {
private ApplicationContext context;
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
log.debug("Autowired applicationContext");
this.context = applicationContext;
}
//省略的代码
@Transactional
public void persistBooking(Booking booking) throws HibernateException, SQLException {
em.persist(booking);
log.debug("fire BookingCreatedEvent");
BookingCreatedEvent bookingCreatedEvent = new BookingCreatedEvent(this, booking);
//触发event
this.context.publishEvent(bookingCreatedEvent);
}
}
触发要实现ApplicationContextAware,用于引入ApplicationContext,由于bookingService也 是spring组件,所以在系统启动的时候,ApplicationContext已经注入。也可以用如下方式直接注入 ApplicationContext。
@Autowired
private ApplicationContext applicationContext;
那么event listener这种模式的好处是什么呢?举个例子,如果客人booking了hotel以后,系统要发email给客人,那我们就可以在listener的do something处加入发送email的代码。
上面我们讲起用@Component把listener注册成了spring的组件,这样listener的用途是在runtime的时候解耦。
而如果我们把listener用配置文件的方式注册的话,主要的用途是在部署时解耦。
在实际应用中,两种情况都有。
另外要注意的一点是,service和listener是同步的,在service中的persistBooking有注册
@Transactional的情况下,listener中的do
something和service中的persistBooking是在同一个tansaction下。
如果要做异步,需要通过MQ或者数据库中转。
spring中的事件 applicationevent 讲的确实不错的更多相关文章
- spring中的事件 applicationevent 讲的确实不错(转)
event,listener是observer模式一种体现,在spring 3.0.5中,已经可以使用annotation实现event和eventListner里. 我们以spring-webflo ...
- Spring 中的事件机制
说到事件机制,可能脑海中最先浮现的就是日常使用的各种 listener,listener去监听事件源,如果被监听的事件有变化就会通知listener,从而针对变化做相应的动作.这些listener是怎 ...
- JavaEE开发之Spring中的事件发送与监听以及使用@Profile进行环境切换
本篇博客我们就来聊一下Spring框架中的观察者模式的应用,即事件的发送与监听机制.之前我们已经剖析过观察者模式的具体实现,以及使用Swift3.0自定义过通知机制.所以本篇博客对于事件发送与监听的底 ...
- 三种方式实现观察者模式 及 Spring中的事件编程模型
观察者模式可以说是众多设计模式中,最容易理解的设计模式之一了,观察者模式在Spring中也随处可见,面试的时候,面试官可能会问,嘿,你既然读过Spring源码,那你说说Spring中运用的设计模式吧, ...
- Tomcat与Spring中的事件机制详解
最近在看tomcat源码,源码中出现了大量事件消息,可以说整个tomcat的启动流程都可以通过事件派发机制串起来,研究透了tomcat的各种事件消息,基本上对tomcat的启动流程也就有了一个整体的认 ...
- 关于spring中的事件体系
在客户这边上班,平时做开发的时候用到了一个客户自己写的一个开发框架,和spring类似,就是功能少一点,提供了依赖注入,事件体系,任务执行等常用的功能,还提供了一个桥接器,可以把spring中的bea ...
- spring学习笔记(二)spring中的事件及多线程
我们知道,在实际开发中为了解耦,或者提高用户体验,都会采用到异步的方式.这里举个简单的例子,在用户注册的sh时候,一般我们都会要求手机验证码验证,邮箱验证,而这都依赖于第三方.这种情况下,我们一般会通 ...
- 【Spring】9、Spring中的事件Event
Spring的ApplicationContext 提供了支持事件和代码中监听器的功能. 我们可以创建bean用来监听在ApplicationContext 中发布的事件.ApplicationEve ...
- Spring中的事件监听实现
在spring中我们可以自定义事件,并且可以使用ApplicationContext类型对象(就是spring容器container)来发布这个事件 事件发布之后,所有的ApplicaitonList ...
随机推荐
- hdu 4502 dp
吉哥系列故事——临时工计划 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Tot ...
- Codeforces 2014-2015 ACM-ICPC, NEERC, Southern Subregional Contest L. Useful Roads
L. Useful Roads time limit per test:4 seconds memory limit per test:512 megabytes input:standard inp ...
- 【HDOJ5519】Kykneion asma(状压DP,容斥)
题意:给定n和a[i](i=0..4),求所有n位5进制数中没有前导0且i出现的次数不超过a[i]的数的个数 2<=n<=15000,0<=a[i]<=3e4 思路:设f(n, ...
- POJ1195Mobile phones
二维树状数组板子题. #include<cstdio> #include<cstring> #include<iostream> #include<cstdl ...
- NOI2001食物链
描述 动物王国中有三类动物 A,B,C,这三类动物的食物链构成了有趣的环形.A吃B,B吃C,C吃A. 现有N个动物,以1-N编号.每个动物都是A,B,C中的一种,但是我们并不知道它到底是哪一种. 有人 ...
- html框架集
通过框架集的使用定义页面分布 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> ...
- hadoop(三)HDFS 文件系统
Hadoop 附带了一个名为 HDFS(Hadoop 分布式文件系统)的分布式文件系统,专门 存储超大数据文件,为整个 Hadoop 生态圈提供了基础的存储服务. 本章内容: 1) HDFS 文件系统 ...
- IE/FF/Chrome下document.documentElement和document.body的 scrollHeight/scrollTop/clientHeight 以及判断滚动条是否已拉到页面最底部
DTD已声明 IE document.documentElement.scrollHeight 浏览器所有内容高度 ,document.body.scrollHeight 浏览器所有内容高度 docu ...
- LeetCode OJ-- Longest Substring Without Repeating Characters ***@
https://oj.leetcode.com/problems/longest-substring-without-repeating-characters/ 给一个string,找出其中不含有重复 ...
- spring 自定义解析类
设计配置属性和JavaBean 编写XSD文件 编写NamespaceHandler和BeanDefinitionParser完成解析工作 编写spring.handlers和spring.schem ...