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 讲的确实不错的更多相关文章

  1. spring中的事件 applicationevent 讲的确实不错(转)

    event,listener是observer模式一种体现,在spring 3.0.5中,已经可以使用annotation实现event和eventListner里. 我们以spring-webflo ...

  2. Spring 中的事件机制

    说到事件机制,可能脑海中最先浮现的就是日常使用的各种 listener,listener去监听事件源,如果被监听的事件有变化就会通知listener,从而针对变化做相应的动作.这些listener是怎 ...

  3. JavaEE开发之Spring中的事件发送与监听以及使用@Profile进行环境切换

    本篇博客我们就来聊一下Spring框架中的观察者模式的应用,即事件的发送与监听机制.之前我们已经剖析过观察者模式的具体实现,以及使用Swift3.0自定义过通知机制.所以本篇博客对于事件发送与监听的底 ...

  4. 三种方式实现观察者模式 及 Spring中的事件编程模型

    观察者模式可以说是众多设计模式中,最容易理解的设计模式之一了,观察者模式在Spring中也随处可见,面试的时候,面试官可能会问,嘿,你既然读过Spring源码,那你说说Spring中运用的设计模式吧, ...

  5. Tomcat与Spring中的事件机制详解

    最近在看tomcat源码,源码中出现了大量事件消息,可以说整个tomcat的启动流程都可以通过事件派发机制串起来,研究透了tomcat的各种事件消息,基本上对tomcat的启动流程也就有了一个整体的认 ...

  6. 关于spring中的事件体系

    在客户这边上班,平时做开发的时候用到了一个客户自己写的一个开发框架,和spring类似,就是功能少一点,提供了依赖注入,事件体系,任务执行等常用的功能,还提供了一个桥接器,可以把spring中的bea ...

  7. spring学习笔记(二)spring中的事件及多线程

    我们知道,在实际开发中为了解耦,或者提高用户体验,都会采用到异步的方式.这里举个简单的例子,在用户注册的sh时候,一般我们都会要求手机验证码验证,邮箱验证,而这都依赖于第三方.这种情况下,我们一般会通 ...

  8. 【Spring】9、Spring中的事件Event

    Spring的ApplicationContext 提供了支持事件和代码中监听器的功能. 我们可以创建bean用来监听在ApplicationContext 中发布的事件.ApplicationEve ...

  9. Spring中的事件监听实现

    在spring中我们可以自定义事件,并且可以使用ApplicationContext类型对象(就是spring容器container)来发布这个事件 事件发布之后,所有的ApplicaitonList ...

随机推荐

  1. Windows通过data文件夹恢复mysql数据库

    mysql--1146--报错 先找到数据库存放地址,即Data文件夹(复制留下来) 再用电脑管家把所有的mysql卸载 然后把mysql文件夹弄走(卸载不会清掉它,需手动,一般在C:\Program ...

  2. hdu 4502 dp

    吉哥系列故事——临时工计划 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Tot ...

  3. CodeVS 1503 愚蠢的宠物

    题目描述 Description 大家都知道,sheep有两只可爱的宠物(一只叫神牛,一只叫神菜).有一天,sheep带着两只宠物到狗狗家时,这两只可爱的宠物竟然迷路了…… 狗狗的家因为常常遭到猫猫的 ...

  4. Array拼接字符串

    原文发布时间为:2011-01-12 -- 来源于本人的百度文章 [由搬家工具导入] Array拼接字符串本来就是一种投机取巧的无聊玩意,来源是IE6对字符串的+实现错误一般情况下,如果是语义性的字符 ...

  5. JS add script tag to dynamically call script

    //IE: var script = document.createElement("script"); script.setAttribute("type", ...

  6. SQLServer Split

    ALTER FUNCTION dbo.splitl ( @String VARCHAR(MAX), @Delimiter VARCHAR(MAX) ) RETURNS @temptable TABLE ...

  7. activeMQ队列模式和主题模式的Java实现

    一.队列模式 生产者 import javax.jms.Connection; import javax.jms.ConnectionFactory; import javax.jms.Destina ...

  8. 关于js延迟加载(异步操作)的方式

    一.概述 最近重新开始学习js,在第一章的一个小节里写到了“脚本调用策略”,书上写的这部分不多,但是发现在我之前的(笔)面试中,问到的频率还是比较高的.自己一直习惯于直接把所有js文件写在head里, ...

  9. Algorithm | Vector

    因为平常用的话只是vector的一些非常简单的功能,基本上把它当数组来用,现在也只是把这一部分写了一些. template<class T> class XVector { public: ...

  10. Decrease (Judge ver.)

    题目描述 We have a sequence of length N consisting of non-negative integers. Consider performing the fol ...