[Spring]容器上下文的refresh概述.

容器上下文的refresh

该方法在容器的抽象类AbstractApplicationContext中,是一种模板方法的实现,其中声明了容器刷新时核心的方法,具体源码请查看org.springframework.context.support.AbstractApplicationContext#refresh.下面来看看其中声明的方法都有哪些.

  • prepareRefresh
    在容器刷新前容器要预先做的准备工作.
  • obtainFreshBeanFactory
    通知子类去刷新内置的beanFactory并返回ConfigurableListableBeanFactory实例.
  • prepareBeanFactory
    将从obtainFreshBeanFactory()中获取到的beanFactory进行配置.
  • postProcessBeanFactory
    允许在ApplicationContext子类中对beanFactory进行postProcessor(后置处理器)的注册。
  • invokeBeanFactoryPostProcessors
    激活容器中的beanFactoryPostProcessor,注意这里是容器级别的postProcessor.
  • registerBeanPostProcessors
    注册拦截Bean创建的BeanPostProcessor.
  • initMessageSource
    初始化容器的MessageSource->国际化配置
  • initApplicationEventMulticaster
    事件发布器,想了解事件发布机制的可以看我的历史文章->点我前往
  • onRefresh
    在特定ApplicationContext子类中初始化其他特殊bean,通常为单例Bean初始化之前。
  • registerListeners
    注册事件监听器.
  • finishBeanFactoryInitialization
    实例化所有的非懒加载的单例类.
  • finishRefresh
    发布事件,例如ContextRefreshedEvent.
  • resetCommonCaches
    重置Spring core的缓存区域.

前言

本文章所研究的容器实例为注解容器,即AnnotationConfigApplicationContext.

1. prepareRefresh

  • refresh
    public void refresh() throws BeansException, IllegalStateException {
        // startupShutdownMonitor是一个同步锁,防止容器在refresh或者destroy的时候发生并发冲突.
        synchronized (this.startupShutdownMonitor) {
            // Prepare this context for refreshing.
            prepareRefresh();
            ...
        }
    }
  • prepareRefresh

简要的总结:

  1. 切换容器状态为active.
  2. 初始化Environment的propertySources属性.一般指配置项.
  3. 验证是否可以解析必填的属性.
  4. 查看是否有事件监听器监听prepare行为,如果有进行注册。
  5. 创建ApplicationEvent.
    protected void prepareRefresh() {
        // 记录当前启动时的时间戳
        this.startupDate = System.currentTimeMillis();
        this.closed.set(false);
        // 1.切换容器状态为active
        this.active.set(true);

        if (logger.isDebugEnabled()) {
            if (logger.isTraceEnabled()) {
                logger.trace("Refreshing " + this);
            }
            else {
                logger.debug("Refreshing " + getDisplayName());
            }
        }

        // 2. 初始化Environment的propertySources属性.
        initPropertySources();

        // 3. 验证所有必填的属性是否可以被解析.
        // 参考 ConfigurablePropertyResolver#setRequiredProperties
        getEnvironment().validateRequiredProperties();

        // 4.存储容器启动前的事件监听器.
        if (this.earlyApplicationListeners == null) {
            this.earlyApplicationListeners = new LinkedHashSet<>(this.applicationListeners);
        }
        else {
            // 如果不为空,则清除当前监听器列表中的监听器,将这些监听器注册.
            this.applicationListeners.clear();
            this.applicationListeners.addAll(this.earlyApplicationListeners);
        }

        // 5. 允许创建应用事件,当前事件发布器就绪时进行发布.
        this.earlyApplicationEvents = new LinkedHashSet<>();
    }

2. obtainFreshBeanFactory

多态的实现,抽象方法,子类必须实现,最终都需要获取子类中的beanFactroy,即DefaultListableFactory.
如果是XML容器会在这里完成BeanDefinition的解析与注册过程.
但是注解容器只是仅仅做了容器状态的变更与设置DefaultListableFactory的serializationId.

  • AbstractApplicationContext#obtainFreshBeanFactory
    protected ConfigurableListableBeanFactory obtainFreshBeanFactory() {
        // 抽象方法,由子类实现.
        refreshBeanFactory();
        return getBeanFactory();
    }
  • GenericApplicationContext#refreshBeanFactory
    protected final void refreshBeanFactory() throws IllegalStateException {
        // CAS比较当前容器上下文的refreshed是否为false,并设置为true
        if (!this.refreshed.compareAndSet(false, true)) {
            throw new IllegalStateException(
                    "GenericApplicationContext does not support multiple refresh attempts: just call 'refresh' once");
        }
        this.beanFactory.setSerializationId(getId());
    }

3. prepareBeanFactory

配置DefaultListableFactory.配置项清单如下:

  1. ClassLoader-类加载器.
  2. BeanExpressionResolver-SPEL表达式解析器.
  3. PropertyEditorRegistrar-注册属性编辑器.
  4. BeanPostProcessor-主要是处理ApplicationContextAware的processor.
  5. 忽略特殊接口的实现类,在自动装配的过程中会进行特殊处理.
  6. 注册解析的依赖项,例如容器需要beanFactory时,以这里的注入项为基础依赖.
  7. BeanPostProcessor-注册发现ApplicationListener的processor.
  8. LoadTimeWeaver-检测是否需要织入LoadTimeWeaverz.
  9. 配置默认的环境参数.
    protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory) {
        // Tell the internal bean factory to use the context's class loader etc.
        // 将内置的BeanFactory的类加载器设置为当前容器的类加载器
        beanFactory.setBeanClassLoader(getClassLoader());
        // 设置beanFactory的SPEL表达式的解析器
        beanFactory.setBeanExpressionResolver(new StandardBeanExpressionResolver(beanFactory.getBeanClassLoader()));
        // 设置默认的propertyEditor
        beanFactory.addPropertyEditorRegistrar(new ResourceEditorRegistrar(this, getEnvironment()));
        // Configure the bean factory with context callbacks.
        // 配置BeanPostProcessor,处理应用程序定义实现ApplicationContextAware的Bean,将容器进行注入
        beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this));
        /**
         * 在自动装配的时候,忽略实现以下接口的实现类.
         * EnvironmentAware、EmbeddedValueResolverAware、ResourceLoaderAware、ApplicationEventPublisherAware
         * MessageSourceAware、ApplicationContextAware
         */
        beanFactory.ignoreDependencyInterface(EnvironmentAware.class);
        beanFactory.ignoreDependencyInterface(EmbeddedValueResolverAware.class);
        beanFactory.ignoreDependencyInterface(ResourceLoaderAware.class);
        beanFactory.ignoreDependencyInterface(ApplicationEventPublisherAware.class);
        beanFactory.ignoreDependencyInterface(MessageSourceAware.class);
        beanFactory.ignoreDependencyInterface(ApplicationContextAware.class);

        // BeanFactory interface not registered as resolvable type in a plain factory.
        // MessageSource registered (and found for autowiring) as a bean.
        // 修正依赖,注册一些自动装配的特殊规则,如果是BeanFactory的实现类,则将当前容器的beanFactory作为其需要的对象
        // 此时,容器的多职责体系出来了,充当ResourceLoader、ApplicationEventPublisher、ApplicationContext
        beanFactory.registerResolvableDependency(BeanFactory.class, beanFactory);
        beanFactory.registerResolvableDependency(ResourceLoader.class, this);
        beanFactory.registerResolvableDependency(ApplicationEventPublisher.class, this);
        beanFactory.registerResolvableDependency(ApplicationContext.class, this);

        // Register early post-processor for detecting inner beans as ApplicationListeners.
        // 注册触发阶段更早的postProcessor,ApplicationListenerDetector负责发现ApplicationListener的bean
        beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(this));

        // Detect a LoadTimeWeaver and prepare for weaving, if found.
        // 检测LoadTimeWeaver的存在,如果找到,则准备进行织入
        if (beanFactory.containsBean(LOAD_TIME_WEAVER_BEAN_NAME)) {
            beanFactory.addBeanPostProcessor(new LoadTimeWeaverAwareProcessor(beanFactory));
            // Set a temporary ClassLoader for type matching.
            // 临时的类加载器,用作类型匹配.临时的类加载器用于管理临时的代理类
            beanFactory.setTempClassLoader(new ContextTypeMatchClassLoader(beanFactory.getBeanClassLoader()));
        }

        // Register default environment beans.
        // 设置默认environment环境的beans
        if (!beanFactory.containsLocalBean(ENVIRONMENT_BEAN_NAME)) {
            beanFactory.registerSingleton(ENVIRONMENT_BEAN_NAME, getEnvironment());
        }
        // systemProperties
        if (!beanFactory.containsLocalBean(SYSTEM_PROPERTIES_BEAN_NAME)) {
            beanFactory.registerSingleton(SYSTEM_PROPERTIES_BEAN_NAME, getEnvironment().getSystemProperties());
        }
        // systemEnvironment
        if (!beanFactory.containsLocalBean(SYSTEM_ENVIRONMENT_BEAN_NAME)) {
            beanFactory.registerSingleton(SYSTEM_ENVIRONMENT_BEAN_NAME, getEnvironment().getSystemEnvironment());
        }
    }

4. postProcessBeanFactory

钩子方法,交由子类实现,注解容器并未选择去实现这个方法.所以这里略过.

protected void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {
}

5. invokeBeanFactoryPostProcessors

激活在容器中注册为bean的BeanFactoryPostProcessor。
简短的概括为: 实例化并调用所有注册的BeanFactoryPostProcessor Bean,并遵循显式顺序(如果给定)。感兴趣的朋友可以看我的注解容器扫描过程中,是如何通过内置的BeanDefinitionRegistryPostProcessor来加载注解标记bean的.

[Spring]Spring的PostProcessor-后置处理器

[Spring]基于注解的AnnotationConfigApplicationContext组件扫描过程

6. registerBeanPostProcessors

注册bean级别的postProcessor.在之后Bean的实例化过程中,会去执行这些BeanPostProcessor的方法.

    protected void registerBeanPostProcessors(ConfigurableListableBeanFactory beanFactory) {
        PostProcessorRegistrationDelegate.registerBeanPostProcessors(beanFactory, this);
    }

7. initMessageSource

初始化国际化配置。如果没有定义,则使用默认的.

8. initApplicationEventMulticaster

初始化ApplicationEventMulticaster.其中SimpleApplicationEventMulticaster为Spring的默认事件发布器,可以通过配置线程池与开启异步调用的方式进行异步消费.

    protected void initApplicationEventMulticaster() {
        ConfigurableListableBeanFactory beanFactory = getBeanFactory();
        // applicationEventMulticaster,如果容器中包含,则通过getBean的方式进行装配
        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 + "]");
            }
        }
        // 否则,使用SimpleApplicationEventMulticaster进行实现.向容器中注册一个单例对象
        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() + "]");
            }
        }
    }

9. onRefresh

在特定ApplicationContext子类中初始化其他特殊bean,通常为单例Bean初始化之前。
钩子方法,注解容器未实现此方法.
如果是SpringBoot工程,基于web的AnnotationConfigEmbeddedWebApplicationContext会在这里进行容器的配置.

10. registerListeners

分为3步:

  1. 注册静态指定的监听器.
  2. 获取所有ApplicationListener的beanName.
  3. 前面进行了发布器的注册,现在可以发布一些early event.
    protected void registerListeners() {
        // Register statically specified listeners first.
        // 首先注册静态指定的侦听器。
        for (ApplicationListener listener : getApplicationListeners()) {
            getApplicationEventMulticaster().addApplicationListener(listener);
        }

        // Do not initialize FactoryBeans here: We need to leave all regular beans
        // uninitialized to let post-processors apply to them!
        // 不要在这里初始化FactoryBeans:我们需要保留所有常规bean
        // 未初始化,以使后处理器适用于它们!
        String[] listenerBeanNames = getBeanNamesForType(ApplicationListener.class, true, false);
        for (String listenerBeanName : listenerBeanNames) {
            getApplicationEventMulticaster().addApplicationListenerBean(listenerBeanName);
        }

        // Publish early application events now that we finally have a multicaster...
        // 前面进行了发布器的注册,现在可以发布一些发生较早的事件
        Set earlyEventsToProcess = this.earlyApplicationEvents;
        this.earlyApplicationEvents = null;
        if (!CollectionUtils.isEmpty(earlyEventsToProcess)) {
            for (ApplicationEvent earlyEvent : earlyEventsToProcess) {
                getApplicationEventMulticaster().multicastEvent(earlyEvent);
            }
        }
    }

11.finishBeanFactoryInitialization

完成此上下文的bean工厂的初始化,初始化所有剩余的单例bean。
聚焦在实例化这三个字.

  1. Spring注册了EmbeddedValueResolver用来解析Value属性。
  2. 注册了一些用于AOP操作的LoadTimeWeaverAwares.
  3. 缓存BeanDefinition元数据.
  4. 开始实例化剩余的非延迟加载的单例类(容器默认注册的bean都是这个级别.)

    /**
     * Finish the initialization of this context's bean factory,
     * initializing all remaining singleton beans.
* 完成此上下文的bean工厂的初始化,初始化所有剩余的单例bean。 */ protected void finishBeanFactoryInitialization(ConfigurableListableBeanFactory beanFactory) { // Initialize conversion service for this context. // 类型转换器 if (beanFactory.containsBean(CONVERSION_SERVICE_BEAN_NAME) && beanFactory.isTypeMatch(CONVERSION_SERVICE_BEAN_NAME, ConversionService.class)) { beanFactory.setConversionService( beanFactory.getBean(CONVERSION_SERVICE_BEAN_NAME, ConversionService.class)); } // Register a default embedded value resolver if no bean post-processor // (such as a PropertyPlaceholderConfigurer bean) registered any before: // at this point, primarily for resolution in annotation attribute values. // 注册一个解析器,主要用来解析@Value("${xxx.xxx}")或者在xml中定义的的信息 if (!beanFactory.hasEmbeddedValueResolver()) { beanFactory.addEmbeddedValueResolver(strVal -> getEnvironment().resolvePlaceholders(strVal)); } // Initialize LoadTimeWeaverAware beans early to allow for registering their transformers early. // 更早地实例化LoadTimeWeaverAware beans 以便更早地注册它们的transformers. String[] weaverAwareNames = beanFactory.getBeanNamesForType(LoadTimeWeaverAware.class, false, false); for (String weaverAwareName : weaverAwareNames) { getBean(weaverAwareName); } // Stop using the temporary ClassLoader for type matching. // 停止在prepareBeanFactory中设置的临时用作类型匹配的类加载器 beanFactory.setTempClassLoader(null); // Allow for caching all bean definition metadata, not expecting further changes. // 缓存所有BeanDefinition元数据,不希望在之后的操作中发生变化 beanFactory.freezeConfiguration(); // Instantiate all remaining (non-lazy-init) singletons. // 实例化剩余的所有非延迟加载的单例bean beanFactory.preInstantiateSingletons(); }
  • DefaultListableBeanFactory#preInstantiateSingletons

此处阐述了从BeanDefinition到Bean的过程,并且为@EventListener提供了切面与适配器的处理.

    public void preInstantiateSingletons() throws BeansException {
        if (logger.isTraceEnabled()) {
            logger.trace("Pre-instantiating singletons in " + this);
        }

        // Iterate over a copy to allow for init methods which in turn register new bean definitions.
        // While this may not be part of the regular factory bootstrap, it does otherwise work fine.
        // 遍历一个副本以允许使用init方法,这些方法依次注册新的bean定义。
        List beanNames = new ArrayList<>(this.beanDefinitionNames);

        // Trigger initialization of all non-lazy singleton beans...
        // 触发所有非延迟加载的单例bean的实例化方法
        for (String beanName : beanNames) {
            // 在前面注解容器中加载的是GenericBeanDefinition,此处转化成了RootBeanDefinition.
            // 这里主要是为了兼容一些具有继承关系的BeanDefinition.
            RootBeanDefinition bd = getMergedLocalBeanDefinition(beanName);
            // 非抽象类(可实例化)、单例类、非延时加载
            if (!bd.isAbstract() && bd.isSingleton() && !bd.isLazyInit()) {
                // 是否为工厂方法的方式进行实例化
                if (isFactoryBean(beanName)) {
                    // &+beanName
                    Object bean = getBean(FACTORY_BEAN_PREFIX + beanName);
                    if (bean instanceof FactoryBean) {
                        FactoryBean factory = (FactoryBean) bean;
                        boolean isEagerInit;
                        if (System.getSecurityManager() != null && factory instanceof SmartFactoryBean) {
                            isEagerInit = AccessController.doPrivileged(
                                    (PrivilegedAction) ((SmartFactoryBean) factory)::isEagerInit,
                                    getAccessControlContext());
                        }
                        else {
                            isEagerInit = (factory instanceof SmartFactoryBean &&
                                    ((SmartFactoryBean) factory).isEagerInit());
                        }
                        if (isEagerInit) {
                            // 关键方法,创建bean
                            getBean(beanName);
                        }
                    }
                }
                else {
                    getBean(beanName);
                }
            }
        }

        // Trigger post-initialization callback for all applicable beans...
        // 经过上面的循环,bean已经完全处理完毕了.
        // @EventListener标注的方法被DefaultEventListenerFactory包装成ApplicationListenerMethodAdapter.
        // @EventListener中的classes就是事件对象
        // ApplicationListenerMethodAdapter注册到ApplicationContext中.
        // 等待事件源发布通知.
        // 通知后执行的逻辑就是标注@EventListener的方法逻辑
        for (String beanName : beanNames) {
            // 从容器缓存中获取bean实例,EventListenerMethodProcess.
            Object singletonInstance = getSingleton(beanName);
            if (singletonInstance instanceof SmartInitializingSingleton) {
                SmartInitializingSingleton smartSingleton = (SmartInitializingSingleton) singletonInstance;
                if (System.getSecurityManager() != null) {
                    AccessController.doPrivileged((PrivilegedAction) () -> {
                        smartSingleton.afterSingletonsInstantiated();
                        return null;
                    }, getAccessControlContext());
                }
                else {
                    // 非处理过的类,非Spring内部类进行处理,提取其中的@EventListener方法实例,然后使用AOP代理为ApplicationListenerMethodAdapter。
                    smartSingleton.afterSingletonsInstantiated();
                }
            }
        }
    }
 
 

12. finishRefresh

完成了容器上下文的刷新工作后:
1.清除资源缓存.
2.注册容器生命周期处理器并回调LifecycleProcessor's onRefresh().
3.发布ContextRefreshedEvent.
4.如果激活了JSM,那么提供JMS的支持.

    protected void finishRefresh() {
        // Clear context-level resource caches (such as ASM metadata from scanning).
        // 清除上下文级别的缓存,如扫描到的ASM元数据
        clearResourceCaches();

        // 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 MBean中.JMS
        LiveBeansView.registerApplicationContext(this);
    }

13.resetCommonCaches

重置Spring core的缓存区域.也许再也不需要单例bean的元数据了.

    protected void resetCommonCaches() {
        ReflectionUtils.clearCache();
        AnnotationUtils.clearCache();
        ResolvableType.clearCache();
        CachedIntrospectionResults.clearClassLoader(getClassLoader());
    }

你可能感兴趣的:([Spring]容器上下文的refresh概述.)