『深入学习 Spring Boot』——refresh 方法

refresh 方法

refresh 方法概览

我们还是从 SpringApplication.run(String... args) 看起。

// SpringApplication # run(String... args) # 311-313
public ConfigurableApplicationContext run(String... args) {
      // …………
            prepareContext(context, environment, listeners, applicationArguments, printedBanner);
            refreshContext(context);
            afterRefresh(context, applicationArguments);
      // …………
        return context;
    }

我们进入 refreshContext(context) 方法体中:

// SpringApplication # refreshContext(ConfigurableApplicationContext context) # 390
private void refreshContext(ConfigurableApplicationContext context) {
        refresh(context);
        if (this.registerShutdownHook) {
            try {
                context.registerShutdownHook();
            }
            catch (AccessControlException ex) {
                // Not allowed in some environments.
            }
        }
    }

很显然,核心方法还是去调用了 refresh(context):

    // SpringApplication # refresh(ApplicationContext applicationContext) # 742
protected void refresh(ApplicationContext applicationContext) {
        Assert.isInstanceOf(AbstractApplicationContext.class, applicationContext);
        ((AbstractApplicationContext) applicationContext).refresh();
    }

继续进入((AbstractApplicationContext) applicationContext).refresh()

// AbstractApplicationContext # refresh() # 515
public void refresh() throws BeansException, IllegalStateException {
        synchronized (this.startupShutdownMonitor) {
            // Prepare this context for refreshing.
      // 为刷新容器,准备上下文
            prepareRefresh();
            // Tell the subclass to refresh the internal bean factory.
      // 告诉子类刷新内部bean工厂
            ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
            // Prepare the bean factory for use in this context.
      // 准备在此上下文使用的bean工厂。
            prepareBeanFactory(beanFactory);
            try {
              // Allows post-processing of the bean factory in context subclasses.
        // 允许在上下文子类中对bean工厂进行后处理
                postProcessBeanFactory(beanFactory);
                // Invoke factory processors registered as beans in the context.
         // 调用在上下文中注册为bean的工厂处理器。
                invokeBeanFactoryPostProcessors(beanFactory);
                // Register bean processors that intercept bean creation.
         // 注册拦截Bean创建的Bean处理器。
                registerBeanPostProcessors(beanFactory);
                // Initialize message source for this context.
        // 初始化此上下文的消息源。
                initMessageSource();
                // Initialize event multicaster for this context.
        // 为上下文,初始化事件广播器
                initApplicationEventMulticaster();
                // Initialize other special beans in specific context subclasses.
        // 在特定上下文子类中初始化其他特殊bean。
                onRefresh();
                // Check for listener beans and register them.
        // 检查监听器并注册。
                registerListeners
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值