Spring源码学习笔记(九)、initApplicationEventMulticaster

该代码段展示了在Spring应用上下文中如何初始化事件多播器。如果上下文中存在名为applicationEventMulticaster的bean,则使用该bean,否则将创建并使用SimpleApplicationEventMulticaster,并将其以单例模式注册到bean工厂。

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

一、方法功能概述

// 初始化事件多播器
initApplicationEventMulticaster();

初始化事件多播器

二、整体代码展示

//AbstractApplicationContext
/**
 * Initialize the ApplicationEventMulticaster.
 * 初始化ApplicationEventMulticaster。
 * Uses SimpleApplicationEventMulticaster if none defined in the context.
 * 如果上下文中未定义,则使用SimpleApplicationEventMulticaster。
 * @see org.springframework.context.event.SimpleApplicationEventMulticaster
 */
protected void initApplicationEventMulticaster() {
    ConfigurableListableBeanFactory beanFactory = getBeanFactory();
    //如果已经有名字是"applicationEventMulticaster"的bean,就用这个
    if (beanFactory.containsLocalBean(APPLICATION_EVENT_MULTICASTER_BEAN_NAME)) {
        this.applicationEventMulticaster =
                beanFactory.getBean(APPLICATION_EVENT_MULTICASTER_BEAN_NAME, ApplicationEventMulticaster.class);
        if (logger.isTraceEnabled()) {
            logger.trace("Using ApplicationEventMulticaster [" + this.applicationEventMulticaster + "]");
        }
    }
    else {
        //否则用SimpleApplicationEventMulticaster
        this.applicationEventMulticaster = new SimpleApplicationEventMulticaster(beanFactory);
        //以单例方式注册到bean工厂里
        beanFactory.registerSingleton(APPLICATION_EVENT_MULTICASTER_BEAN_NAME, this.applicationEventMulticaster);
        if (logger.isTraceEnabled()) {
            logger.trace("No '" + APPLICATION_EVENT_MULTICASTER_BEAN_NAME + "' bean, using " +
                    "[" + this.applicationEventMulticaster.getClass().getSimpleName() + "]");
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

每天进步亿点点的小码农

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值