Spring之事件监听

pring借助于org.springframework.context.event.ApplicationEvent抽象类及其子类实现事件的发布;借助于org.springframework.context.ApplicationListener接口及其实现者实现事件的监听。这两者构成了观察者模式(Observer)。


ApplicationContext提供了publishEvent方法,实现事件的发布。Spring提供了如下三种常见的ApplicationEvent事件实现:
org.springframework.web.context.support.RequestHandledEvent:一旦客户请求处理完毕,将发布该事件。
org.springframework.context.event.ContextRefreshedEvent:在ApplicationContext容器初始化完成或者刷新时,发布该事件。
org.springframework.context.event.ContextClosedEvent:在关闭ApplicationContext容器时,发布该事件。


为监听ApplicationEvent事件,开发者需要在目标JavaBean中实现ApplicationListener接口。

你可能感兴趣的:(spring)