Spring源码 13 IOC refresh方法8

本文章基于 Spring 5.3.15

Spring IOC 的核心是 AbstractApplicationContextrefresh 方法。
其中一共有 13 个主要方法,这里分析第 8 个:initApplicationEventMulticaster

1 AbstractApplicationContext

1-1 初始化应用消息广播器

initApplicationEventMulticaster()
protected void initApplicationEventMulticaster() {
    // 获取 Bean 工厂
    ConfigurableListableBeanFactory beanFactory = getBeanFactory();
    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 {
        // 定义应用事件多播器
        this.applicationEventMulticaster = new SimpleApplicationEventMulticaster(beanFactory);
        // 注册单例
        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() + "]");
        }
    }
}

获取 Bean 工厂注册单例 在前面已经分析过,这里不再分析。

1-2 定义应用事件多播器

SimpleApplicationEventMulticaster(beanFactory)

2 SimpleApplicationEventMulticaster

public SimpleApplicationEventMulticaster(BeanFactory beanFactory) {
    // 设置 Bean 工厂
    setBeanFactory(beanFactory);
}

2-1 设置 Bean 工厂

public void setBeanFactory(BeanFactory beanFactory) {
   if (!(beanFactory instanceof ConfigurableBeanFactory)) {
      throw new IllegalStateException("Not running in a ConfigurableBeanFactory: " + beanFactory);
   }
   this.beanFactory = (ConfigurableBeanFactory) beanFactory;
   if (this.beanClassLoader == null) {
      this.beanClassLoader = this.beanFactory.getBeanClassLoader();
   }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

天航星

感谢你的鼓励和认可

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

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

打赏作者

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

抵扣说明:

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

余额充值