spring中ApplicationListener源码分析

本文深入解析Spring框架中的事件监听机制,通过观察者模式实现,在容器加载完成后触发特定事件,介绍如何自定义监听器并实现ApplicationListener接口。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近做一个功能,需要在整个服务器容器加载完成就触发执行,这个时候我只需要实现ApplicationListener即可。spring的这个接口就是给一个观察的入口,可以在各个阶段去触发相应的操作。不难看出这就是典型的设计模式之观察者模式。这次我们以观察者的观点去分析一下ApplicationListener。

/**
 * Interface to be implemented by application event listeners.
 * Based on the standard {@code java.util.EventListener} interface
 * for the Observer design pattern.
 *
 * <p>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);

}

Rod Johnson大神也在注释里写清楚了这个功能就是基于观察者模式,并且用户可以在实现类中指定自己感兴趣的事件观察。这个就是所有观察者的抽象接口,这个接口是给客户端使用的,客户端只是对接口透明,对具体的需要通知的对象是动态注入的。一旦观察者感兴趣的事件发生,客户端就会调用接口的方法通知。
这个通知方法,就是在:org.springframework.context.event.SimpleApplicationEventMulticaster中的:

protected void invokeListener(ApplicationListener listener, ApplicationEvent event) {
        ErrorHandler errorHandler = getErrorHandler();
        if (errorHandler != null) {
            try {
                listener.onApplicationEvent(event);
            }
            catch (Throwable err) {
                errorHandler.handleError(err);
            }
        }
        else {
            listener.onApplicationEvent(event);
        }
    }

这里只是通知一个观察者,具体要通知哪些观察者在该类的父类org.springframework.context.event.SimpleApplicationEventMulticaster定义一种事件类型的观察者有哪些(ApplicationListener):

public void multicastEvent(final ApplicationEvent event, ResolvableType eventType) {
        ResolvableType type = (eventType != null ? eventType : resolveDefaultEventType(event));
        for (final ApplicationListener<?> listener : getApplicationListeners(event, type)) {
            Executor executor = getTaskExecutor();
            if (executor != null) {
                executor.execute(new Runnable() {
                    @Override
                    public void run() {
                        invokeListener(listener, event);
                    }
                });
            }
            else {
                invokeListener(listener, event);
            }
        }
    }

所有的监听器的注册在org.springframework.context.support.AbstractApplicationContext的refresh()方法当中的registerListener()方法,该方法基本完成了容器的初始化:

protected void registerListeners() {
        // Register statically specified listeners first.
        for (ApplicationListener<?> listener : getApplicationListeners()) {
            getApplicationEventMulticaster().addApplicationListener(listener);
        }

        // Do not initialize FactoryBeans here: We need to leave all regular beans
        // uninitialized to let post-processors apply to them!
        String[] listenerBeanNames = getBeanNamesForType(ApplicationListener.class, true, false);
        for (String listenerBeanName : listenerBeanNames) {
            getApplicationEventMulticaster().addApplicationListenerBean(listenerBeanName);
        }

        // Publish early application events now that we finally have a multicaster...
        Set<ApplicationEvent> earlyEventsToProcess = this.earlyApplicationEvents;
        this.earlyApplicationEvents = null;
        if (earlyEventsToProcess != null) {
            for (ApplicationEvent earlyEvent : earlyEventsToProcess) {
                getApplicationEventMulticaster().multicastEvent(earlyEvent);
            }
        }
    }

当容器初始化结束后就会调用finishRefresh(),该事件会触发一系列操作,其中就包括通知观察者,即监听器:

protected void publishEvent(Object event, ResolvableType eventType) {
        Assert.notNull(event, "Event must not be null");
        if (logger.isTraceEnabled()) {
            logger.trace("Publishing event in " + getDisplayName() + ": " + event);
        }

        // Decorate event as an ApplicationEvent if necessary
        ApplicationEvent applicationEvent;
        if (event instanceof ApplicationEvent) {
            applicationEvent = (ApplicationEvent) event;
        }
        else {
            applicationEvent = new PayloadApplicationEvent<Object>(this, event);
            if (eventType == null) {
                eventType = ResolvableType.forClassWithGenerics(PayloadApplicationEvent.class, event.getClass());
            }
        }

        // Multicast right now if possible - or lazily once the multicaster is initialized
        if (this.earlyApplicationEvents != null) {
            this.earlyApplicationEvents.add(applicationEvent);
        }
        else {
            getApplicationEventMulticaster().multicastEvent(applicationEvent, eventType);
        }

        // Publish event via parent context as well...
        if (this.parent != null) {
            if (this.parent instanceof AbstractApplicationContext) {
                ((AbstractApplicationContext) this.parent).publishEvent(event, eventType);
            }
            else {
                this.parent.publishEvent(event);
            }
        }
    }

以上代码是根据事件去通知对应的监听器。这里的具体情况是在finishRefresh()传入了容器加载完成事件:
这样就去调用对这个事件感兴趣的监听器。

        // Publish the final event.
        publishEvent(new ContextRefreshedEvent(this));

这里完成接口调用通知。

public void multicastEvent(final ApplicationEvent event, ResolvableType eventType) {
        ResolvableType type = (eventType != null ? eventType : resolveDefaultEventType(event));
        for (final ApplicationListener<?> listener : getApplicationListeners(event, type)) {
            Executor executor = getTaskExecutor();
            if (executor != null) {
                executor.execute(new Runnable() {
                    @Override
                    public void run() {
                        invokeListener(listener, event);
                    }
                });
            }
            else {
                invokeListener(listener, event);
            }
        }
    }

这样就用观察者模式实现了spring的整个事件监听功能。

Spring框架中,我们可以通过实现ApplicationListener接口来监听Spring容器的事件,从而在特定事件发生时执行一些特定的处理。 例如,在项目启动时我们可能需要进行一些初始化操作,比如读取配置文件、连接数据库等等。此时,我们可以实现ApplicationListener接口,在onApplicationEvent方法中编写初始化逻辑。 下面是一个简单的示例代码: ```java @Component public class MyApplicationListener implements ApplicationListener<ContextRefreshedEvent> { @Override public void onApplicationEvent(ContextRefreshedEvent event) { // 在Spring容器初始化完成后执行 // 这里可以写一些项目启动时的初始化逻辑 System.out.println("项目启动完成,执行初始化操作..."); } } ``` 在上面的代码中,我们使用@Component注解将MyApplicationListener类纳入Spring容器的管理中,并实现了ApplicationListener接口,并指定了监听的事件类型为ContextRefreshedEvent,这是Spring容器初始化完成后会发布的事件。 当Spring容器初始化完成后,就会自动触发onApplicationEvent方法,执行我们在其中编写的初始化逻辑。 除了ContextRefreshedEvent事件,Spring容器还支持许多其他事件类型,例如ContextStartedEvent(容器启动时触发)、ContextStoppedEvent(容器停止时触发)等等。我们可以根据具体需求选择不同的事件类型来监听。 在底层实现上,Spring容器会通过调用ConfigurableApplicationContext.publishEvent()方法来发布事件,然后遍历所有实现了ApplicationListener接口的bean,并调用它们的onApplicationEvent()方法来处理相应的事件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值