spring事件(一)
在事件体系中,有5个重要概念:
事件源 ? 事件 ? 时间广播器 ? 事件监听器注册表(事件监听器)。
事件监听器注册表是保存事件监听器的地方。
?
spring的事件类结构
事件类:ApplicationEvent(Object source)ApplicationEvent 继承了java.util.EventObject,
有两个子类:ApplicationContextEvent和RequestHandledEvent。
ApplicationContextEvent有四个子类:ContextStartedEvent(容器启动),????????????????????????????????????????????????????????
???????????????????????????????????????????????????? ContextClosedEvent(容器关闭),????????????????????????????????????????????????????????
???????????????????????????????????????????????????? ContextRefreshedEvent(容器刷新),????????????????????????????????????????????????????????
???????????????????????????????????????????????????? ContextStoppedEvent(容器停止)。
RequestHandledEvent与web应用相关的事件,只有在web.xml中定义了DispatcherServlet时才会产生该事件。有两个子类:ServletRequestHandledEvent和PortletRequestHandledEvent。
?
事件监听器接口:ApplicationListener(接口)
ApplicationListener继承了java.util.EventListener接口,
?????? 只提供了一个方法onApplicationEvent(ApplicationEvent event),在该方法中编写事件的响应处理逻辑。SmartApplicationListener接口继承了ApplicationListener,是Spring3.0新增的。定义了2个方法:????????
??????? boolean supportsEventType(Class<? extends ApplicationEvent > eventType):指定监听器支持哪种类
?????????????????????? 型的容器事件,只对这种类型的事件进行响应。????????
??????? booleansupportsSourceType(Class<?> sourceType):指定监听器仅对哪种事件源作出响应。
?
事件广播器:ApplicationEventMulticaster(接口)
spring定义了事件广播器的接口,并且提供了实现类。
类SimpleApplicationEventMulticaster继承了抽象类AbstractApplicationEventMulticaster,
抽象类AbstractApplicationEventMulticaster实现了接口ApplicationEventMulticaster。????????????