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或者數據庫中轉。
