spring中的event listener模式和解耦
2012-04-16 13:42
375 查看
event,listener是observer模式一种体现,在spring 3.0.5中,已经可以使用annotation实现event和eventListner里。
我们以spring-webflow里的hotel booking为例,看一下实现,步骤如下:
1,建立event
event需要继承ApplicationEvent。
2,建立listener
listener需要实现ApplicationListener。
注意在spring 3.0.5中的ApplicationListener是带泛型的,这样BookingEventsListener只会监听BookingCreatedEvent事件。
另外可以用@Component来注册组件,这样就不需要在spring的配置文件中指定了。
3,触发event
触发要实现ApplicationContextAware,用于引入ApplicationContext,由于bookingService也是spring组件,所以在系统启动的时候,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-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中的event listener模式和解耦
- java事件监听机制,spring中的event listener模式和解耦
- 框架漫谈之spring(一)工厂模式实现程序解耦,spring框架的引出
- Spring、Hibernate 解耦方法与设计模式(2)
- Spring、Hibernate 解耦方法与设计模式(1)
- 使用门面模式进一步地解耦Spring与Struts的整合
- spring,ioc模式与ejb3调用
- Spring 框架的设计理念与设计模式分析
- Spring 设计模式——观察者|事件驱动模型
- spring中的设计模式
- 反射实现 AOP 动态代理模式(Spring AOP实现原理)
- spring的mvc开发模式-jsp和java交互
- spring boot 使用h2数据库配置(内存模式)
- Spring 源码分析-----委派模式(不关心过程,只关心结果)
- Spring Boot 2.0 执行器端点(Actuator Endpoint)精简模式
- spring 与设计模式(创建型)之工厂模式Factory
- Spring 资源访问剖析和策略模式应用
- Android学习探索之运用MVP设计模式实现项目解耦
- spring bean scope模式配置的一些思考
- spring整合activemq发送MQ消息[queue模式]实例