一、简介
spring框架是java开发中常用的,通常我们需要在spring的某些事件发生时,做某些操作。
在此介绍spring的内置事件,以父抽象类ApplicationContextEvent下的四个子事件类为主。
类图如下:
二、事件类
1、ApplicationContextEvent
是spring内置事件的父抽象类,构造方法传入spring的context容器,同时也有获取spring的context容器的方法。
源码如下:
/** * Base class for events raised for an {@code ApplicationContext}. * * @author Juergen Hoeller * @since 2.5 */ @SuppressWarnings("serial") public abstract class ApplicationContextEvent extends ApplicationEvent { /** * Create a new ContextStartedEvent. * @param source the {@code ApplicationContext} that the event is raised for * (must not be {@code null}) */ public ApplicationContextEvent(ApplicationContext source) { super(source); } /** * Get the {@code ApplicationContext} that the event was raised for. */ public final ApplicationContext getApplicationContext() { return (ApplicationContext) getSource(); } }2、ContextRefreshedEvent
当spring容器初始化或刷新时,会触发此事件。此事件在开发中常用,用于在spring容器启动时,导入自定义的bean实例到spring容器中。
源码如下:
/** * Event raised when an {@code ApplicationContext} gets initialized or refreshed. * * @author Juergen Hoeller * @since 04.03.2003 * @see ContextClosedEvent */ @SuppressWarnings("serial") public class ContextRefreshedEvent extends ApplicationContextEvent { /** * Create a new ContextRefreshedEvent. * @param source the {@code ApplicationContext} that has been initialized * or refreshed (must not be {@code null}) */ public ContextRefreshedEvent(ApplicationContext source) { super(source); } }
3、ContextStartedEvent
当spring启动时,或者说是context调用start()方法时,会触发此事件。
源码如下:
/** * Event raised when an {@code ApplicationContext} gets started. * * @author Mark Fisher * @author Juergen Hoeller * @since 2.5 * @see ContextStoppedEvent */ @SuppressWarnings("serial") public class ContextStartedEvent extends ApplicationContextEvent { /** * Create a new ContextStartedEvent. * @param source the {@code ApplicationContext} that has been started * (must not be {@code null}) */ public ContextStartedEvent(ApplicationContext source) { super(source); } }4、ContextStoppedEvent
当spring停止时,或者说context调用stop()方法时,会触发此事件。
源码如下:
/** * Event raised when an {@code ApplicationContext} gets stopped. * * @author Mark Fisher * @author Juergen Hoeller * @since 2.5 * @see ContextStartedEvent */ @SuppressWarnings("serial") public class ContextStoppedEvent extends ApplicationContextEvent { /** * Create a new ContextStoppedEvent. * @param source the {@code ApplicationContext} that has been stopped * (must not be {@code null}) */ public ContextStoppedEvent(ApplicationContext source) { super(source); } }5、ContextClosedEvent
当spring关闭时,或者说context调用close()方法时,会触发此事件。
源码如下:
/** * Event raised when an {@code ApplicationContext} gets closed. * * @author Juergen Hoeller * @since 12.08.2003 * @see ContextRefreshedEvent */ @SuppressWarnings("serial") public class ContextClosedEvent extends ApplicationContextEvent { /** * Creates a new ContextClosedEvent. * @param source the {@code ApplicationContext} that has been closed * (must not be {@code null}) */ public ContextClosedEvent(ApplicationContext source) { super(source); } }三、监听类
在spring中,我们通常实现接口ApplicationListener
在接口内的方法onApplicationEvent中实现监听到事件时需处理的逻辑。
ApplicationListener接口的源码如下:
/** * Interface to be implemented by application event listeners. * Based on the standard {@code java.util.EventListener} interface * for the Observer design pattern. * * As of Spring 3.0, an ApplicationListener can generically declare the event type * that it is interested in. When registered with a Spring ApplicationContext, events * will be filtered accordingly, with the listener getting invoked for matching event * objects only. * * @author Rod Johnson * @author Juergen Hoeller * @param <E> the specific ApplicationEvent subclass to listen to * @see org.springframework.context.event.ApplicationEventMulticaster */ public interface ApplicationListener<E extends ApplicationEvent> extends EventListener { /** * Handle an application event. * @param event the event to respond to */ void onApplicationEvent(E event); }四、代码实例
代码中,我们可以直接实现不带泛型的接口ApplicationListener,在代码中对不同事件做不同处理,如:
@Component public class SpringContextEventDemo implements ApplicationListener { public void onApplicationEvent(ApplicationEvent event) { if(event instanceof ContextRefreshedEvent){ System.out.println("ContextRefreshedEvent happen"); }else if(event instanceof ContextStartedEvent){ System.out.println("ContextStartedEvent happen"); }else if(event instanceof ContextStoppedEvent){ System.out.println("ContextStoppedEvent happen"); }else if(event instanceof ContextClosedEvent){ System.out.println("ContextClosedEvent happen"); } } }
也可直接监听特定事件,即实现带泛型接口的ApplicationListener,在此以监听ContextRefreshedEvent事件为例。
1、监听类
@Component public class SpringContextRefreshedEventDemo implements ApplicationListener2、测试类{ public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) { ApplicationContext ctx = contextRefreshedEvent.getApplicationContext(); System.out.println("ContextRefreshedEvent happen"); } }
public class SpringSelfEventMain { public static void main(String[] args) { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class); // ctx.start(); //ContextStartedEvent事件发生 // ctx.stop(); //ContextStoppedEvent事件发生 // ctx.close(); //ContextClosedEvent事件发生 } }运行结果(在spring容器启动时,触发ContextRefreshedEvent事件,执行监听逻辑):
ContextRefreshedEvent happen