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. 洛谷 P1783 海滩防御

    题目描述 WLP同学最近迷上了一款网络联机对战游戏(终于知道为毛JOHNKRAM每天刷洛谷效率那么低了),但是他却为了这个游戏很苦恼,因为他在海边的造船厂和仓库总是被敌方派人偷袭.于是,WLP动用了他 ...

  2. class.getDeclaredFields()与class.getFields()

    * getFields()与getDeclaredFields()区别:getFields()只能访问类中声明为公有的字段,私有的字段它无法访问.getDeclaredFields()能访问类中所有的 ...

  3. postgresql 10 数据类型 (完整版)

    官方数据类型document https://www.postgresql.org/docs/10/static/datatype.html PostgreSQL拥有丰富的数据类型供用户使用.用户也可 ...

  4. 【转载】SQL Server XML Path

    FOR XML PATH 有的人可能知道有的人可能不知道,其实它就是将查询结果集以XML形式展现,有了它我们可以简化我们的查询语句实现一些以前可能需要借助函数活存储过程来完成的工作.那么以一个实例为主 ...

  5. ASP.NET MVC创建静态页

    1.在MVC下新建一个类:StaticPageHelper public class StaticPageHelper { /// <summary> /// 根据View视图生成静态页面 ...

  6. MySQL的INFORMATION_SCHEMA数据库简介

    大家在安装或使用MYSQL时,会发现除了自己安装的数据库以外,还有一个 information_schema数据库.information_schema数据库是做什么用的呢,使用WordPress博客 ...

  7. (入门SpringBoot)SpringBoot后台验证(八)

    后台验证的作用主要是防止postman...等等工具的恶意提交,前后台都判断数据,双保险. .可以在SpringBoot传参数 加上NotNull.... //分组Default,分组的好处就是可重复 ...

  8. Android Spinner In Toolbar

    As the title of the post suggest in this tutorial we will see how to have spinner widget inside the ...

  9. 【spring cloud】对接口调用者提供API使用的安全验证微服务【这里仅通过代码展示一种设计思想】【后续可以加入redis限流的功能,某段时间某个IP可以访问API几次】

    场景: 公司的微服务集群,有些API 会对外提供接口,供其他厂商进行调用.这些公开的API接口,由一个OpenAPI微服务统一提供给大家. 那么所有的调用者在调用公开API接口的时候,需要验证是否有权 ...

  10. 【报错】项目启动,仅仅报错 One or more listeners failed to start. Full details will be found in the appropriate container log file

    今天spring4.3.13 项目,整合ActiveMQ的时候,项目启动在自动部署到tomcat下的时候,不能正常的部署,仅仅报错如下: Connected to server [-- ::,] Ar ...