Spring源码之事件驱动模型

SpringContext中初始化事件发布者 ###

//spring初始化事件的地方
//spring初始化事件的地方
public abstract class AbstractApplicationContext extends DefaultResourceLoader
        implements ConfigurableApplicationContext, DisposableBean {

    /** 用于事件发布 */
    private ApplicationEventMulticaster applicationEventMulticaster;

    /** 静态指定的监听器*/
    private Set<ApplicationListener<?>> applicationListeners = new LinkedHashSet<ApplicationListener<?>>();

    //======省略一段代码========/
    public void refresh() throws BeansException, IllegalStateException {

        //======省略一段代码========/
        // Initialize event multicaster for this context.
        initApplicationEventMulticaster();
        // Initialize other special beans in specific context subclasses.
        onRefresh();

        // 检查监听器bean并注册它们。
        registerListeners();

        //======省略一段代码========/
    }

    //======省略一段代码========/

    /**
     * 初始化ApplicationEventMulticaster.
     * <p>如果在上下文中没有定义,则使用SimpleApplicationEventMulticaster。
     * @see org.springframework.context.event.SimpleApplicationEventMulticaster
     */
    protected void initApplicationEventMulticaster() {
        //获取bean工厂
        ConfigurableListableBeanFactory beanFactory = getBeanFactory();
        //先找工厂中ApplicationEventMulticaster是否有对应的实例(注册过)
        if (beanFactory.containsLocalBean(APPLICATION_EVENT_MULTICASTER_BEAN_NAME)) {
            this.applicationEventMulticaster =
                    beanFactory.getBean(APPLICATION_EVENT_MULTICASTER_BEAN_NAME, ApplicationEventMulticaster.class);
            if (logger.isDebugEnabled()) {
                logger.debug("Using ApplicationEventMulticaster [" + this.applicationEventMulticaster + "]");
            }
        }
        else {
  //没有对应的实例,则自己初始化一个,可以看到没有初始化线程次,所以默认是同步调用
            this.applicationEventMulticaster = new SimpleApplicationEventMulticaster(beanFactory);
            beanFactory.registerSingleton(APPLICATION_EVENT_MULTICASTER_BEAN_NAME, this.applicationEventMulticaster);
            if (logger.isDebugEnabled()) {
                logger.debug("Unable to locate ApplicationEventMulticaster with name '" +
                        APPLICATION_EVENT_MULTICASTER_BEAN_NAME +
                        "': using default [" + this.applicationEventMulticaster + "]");
            }
        }
    }

    /**
     * 添加实现ApplicationListener的bean作为监听器
     * Doesn't affect other listeners, which can be added without being beans.
     */
    protected void registerListeners() {
        // 首先注册静态指定的监听器。
        for (ApplicationListener<?> listener : getApplicationListeners()) {
            getApplicationEventMulticaster().addApplicationListener(listener);
        }

        /**
        *不要在这里初始化FactoryBeans:我们需要将所有常规的bean保留为初始化,以便后bean后置处理器适用于它们!
        */

        //查找所有ApplicationListener类型的类
        //includeNonSingletons为false表示只取单例Bean,true则不是
        //allowEagerInit为true表示立刻加载,false表示延迟加载。
        String[] listenerBeanNames = getBeanNamesForType(ApplicationListener.class, true, false);
        for (String lisName : listenerBeanNames) {
            getApplicationEventMulticaster().addApplicationListenerBean(lisName);
        }
    }


    //发布事件
    public void publishEvent(ApplicationEvent event) {
        Assert.notNull(event, "Event must not be null");
        if (logger.isTraceEnabled()) {
            logger.trace("Publishing event in " + getDisplayName() + ": " + event);
        }
        //获取applicationEventMulticaster对象,调用multicastEvent(event)方法
        getApplicationEventMulticaster().multicastEvent(event);
        if (this.parent != null) {
            this.parent.publishEvent(event);
        }
    }

}
//可以看到,如果要实现异步的事件发布,id必须为“applicationEventMulticaster”
<bean id="applicationEventMulticaster" class="org.springframework.context.event.SimpleApplicationEventMulticaster">  
    <!-- 注入任务执行器 这样就实现了异步调用(缺点是全局的,要么全部异步,要么全部同步(删除这个属性即是同步))  -->  
    <property name="taskExecutor" ref="executor"/>  
</bean>  

事件ApplicationEvent


                
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值