返回介绍

6.8 finishRefresh

发布于 2025-04-22 22:09:13 字数 3504 浏览 0 评论 0 收藏

在 Spring 中还提供了 Lifecycle 接口,Lifecycle 中包含 start/stop 方法,实现此接口后 Spring 会保证在启动的时候调用其 start 方法开始生命周期,并在 Spring 关闭的时候调用 stop 方法来结束生命周期,通常用来配置后台程序,在启动后一直运行(如对 MQ 进行轮询等)。而 ApplicationContext 的初始化最后正是保证了这一功能的实现。

protected void finishRefresh() {

 initLifecycleProcessor();

  // Propagate refresh to lifecycle processor first.

 getLifecycleProcessor().onRefresh();

  // Publish the final event.

  publishEvent(new ContextRefreshedEvent(this));

}

1.initLifecycleProcessor

当 ApplicationContext 启动或停止时,它会通过 LifecycleProcessor 来与所有声明的 bean 的周期做状态更新,而在 LifecycleProcessor 的使用前首先需要初始化。

protected void initLifecycleProcessor() {

  ConfigurableListableBeanFactory beanFactory = getBeanFactory();

  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 + "]");

  }

  }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 + "]");

  }

 }

}

2.onRefresh

启动所有实现了 Lifecycle 接口的 bean。

public void onRefresh() {

 startBeans(true);

  this.running = true;

}

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.size() > 0) {

   List<Integer> keys = new ArrayList<Integer>(phases.keySet());

  Collections.sort(keys);

   for (Integer key : keys) {

   phases.get(key).start();

  }

 }

}

3.publishEvent

当完成 ApplicationContext 初始化的时候,要通过 Spring 中的事件发布机制来发出 Context RefreshedEvent 事件,以保证对应的监听器可以做进一步的逻辑处理。

public void publishEvent(ApplicationEvent event) {

  Assert.notNull(event, "Event must not be null");

  if (logger.isTraceEnabled()) {

   logger.trace("Publishing event in " + getDisplayName() + ": " + event);

 }

 getApplicationEventMulticaster().multicastEvent(event);

  if (this.parent != null) {

  this.parent.publishEvent(event);

 }

}

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。