写点什么

Spring 系列之 IOC 容器的初始化过程九

作者:王威07325
  • 2023-05-29
    上海
  • 本文字数:1670 字

    阅读完需:约 5 分钟

refresh 方法中的 finishRefresh 方法

闲话先不聊,咱们直接开始今天的主题,今天我们讲解的是 refresh 方法中的 finishRefresh 方法


finishRefresh

发布相应事件


完成此上下文的刷新,调用 LifecycleProcessor 的 onRefresh() 方法并发布 ContextRefreshedEvent


protected void finishRefresh() {    // Initialize lifecycle processor for this context.    initLifecycleProcessor();
// Propagate refresh to lifecycle processor first. getLifecycleProcessor().onRefresh();
// Publish the final event. publishEvent(new ContextRefreshedEvent(this));
// Participate in LiveBeansView MBean, if active. LiveBeansView.registerApplicationContext(this); }
复制代码


initLifecycleProcessor


初始化生命周期处理器。如果上下文中没有定义,则使用 DefaultLifecycleProcessor


protected void initLifecycleProcessor() {    ConfigurableListableBeanFactory beanFactory = getBeanFactory();    //如果有实现了LifecycleProcessor接口的bean存在,将lifecycleProcessor进行赋值    if (beanFactory.containsLocalBean(LIFECYCLE_PROCESSOR_BEAN_NAME)) {      this.lifecycleProcessor =          beanFactory.getBean(LIFECYCLE_PROCESSOR_BEAN_NAME, LifecycleProcessor.class);      if (logger.isDebugEnabled()) {        logger.debug("Using LifecycleProcessor [" + this.lifecycleProcessor + "]");      }    }    //如果没有实现lifecycleProcessor接口的bean存在,则自己new一个    //DefaultLifecycleProcessor对象然后赋值给lifecycleProcessor,再注册到spring容器中        else {      DefaultLifecycleProcessor defaultProcessor = new DefaultLifecycleProcessor();      defaultProcessor.setBeanFactory(beanFactory);      this.lifecycleProcessor = defaultProcessor;      beanFactory.registerSingleton(LIFECYCLE_PROCESSOR_BEAN_NAME, this.lifecycleProcessor);      if (logger.isDebugEnabled()) {        logger.debug("Unable to locate LifecycleProcessor with name '" +            LIFECYCLE_PROCESSOR_BEAN_NAME +            "': using default [" + this.lifecycleProcessor + "]");      }    }  }
复制代码


onRefresh


上下文刷新通知,例如自动启动组件


public void onRefresh() {    startBeans(true);    this.running = true;  }
复制代码


startBeans:


private void startBeans(boolean autoStartupOnly) {    Map<String, Lifecycle> lifecycleBeans = getLifecycleBeans();    Map<Integer, LifecycleGroup> phases = new HashMap<Integer, LifecycleGroup>();    for (Map.Entry<String, ? extends Lifecycle> entry : lifecycleBeans.entrySet()) {      Lifecycle bean = entry.getValue();      if (!autoStartupOnly || (bean instanceof SmartLifecycle && ((SmartLifecycle) bean).isAutoStartup())) {        int phase = getPhase(bean);        LifecycleGroup group = phases.get(phase);        if (group == null) {          group = new LifecycleGroup(phase, this.timeoutPerShutdownPhase, lifecycleBeans, autoStartupOnly);          phases.put(phase, group);        }        group.add(entry.getKey(), bean);      }    }    if (!phases.isEmpty()) {      List<Integer> keys = new ArrayList<Integer>(phases.keySet());      Collections.sort(keys);      for (Integer key : keys) {        phases.get(key).start();      }    }  }
复制代码


autoStartupOnly 在 onRefresh 时传入的是 true,表示只执行可以自动启动的 bean,即为:SmartLifecycle 的实现类,并且 SmartLifecycle#isAutoStartup 返回值必须为 true


publishEvent


发布最终事件


LiveBeansView#registerApplicationContext(this)


这个目前没啥用处,干脆就不说了


很好,我承认这篇文章划水了,明天再继续更新一篇

发布于: 刚刚阅读数: 2
用户头像

王威07325

关注

还未添加个人签名 2021-12-29 加入

还未添加个人简介

评论

发布
暂无评论
Spring系列之IOC容器的初始化过程九_spring ioc_王威07325_InfoQ写作社区