spring容器

2021-02-17  本文已影响0人  laowangv2

基于spring boot web项目分析

一、创建ApplicationContext

1. 构建beanFactory和ApplicationContext

ApplicationContext扩展了BeanFactory的功能,一些bean管理的核心功能使用BeanFactory实现。首先看这两个家伙的构建

1. create

createApplicationContext

从springboot的入口run进去,进入到createApplicationContext


反射创建

首先根据容器的类型启动不同的applicationcontext,web的情况下选择AnnotationConfigServletWebServerApplicationContext,这个类的继承关系如下


继承关系
进入到其父类GenericApplicationContext,可以看到初始化时构造了beanFactory
GenericApplicationContext

2. refresh

整体步骤在AbstractApplicationContext的refresh方法中

@Override
    public void refresh() throws BeansException, IllegalStateException {
        synchronized (this.startupShutdownMonitor) {
            // Prepare this context for refreshing.
            prepareRefresh();

            // 1.构建BeanFactory
            ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();

            // Prepare the bean factory for use in this context.
            prepareBeanFactory(beanFactory);

            try {
                // Allows post-processing of the bean factory in context subclasses.
                postProcessBeanFactory(beanFactory);

                // Invoke factory processors registered as beans in the context.
                invokeBeanFactoryPostProcessors(beanFactory);

                // Register bean processors that intercept bean creation.
                registerBeanPostProcessors(beanFactory);

                // Initialize message source for this context.
                initMessageSource();

                // Initialize event multicaster for this context.
                initApplicationEventMulticaster();

                // Initialize other special beans in specific context subclasses.
                onRefresh();

                // Check for listener beans and register them.
                registerListeners();

                // Instantiate all remaining (non-lazy-init) singletons.
                finishBeanFactoryInitialization(beanFactory);

                // Last step: publish corresponding event.
                finishRefresh();
            }

            catch (BeansException ex) {
                if (logger.isWarnEnabled()) {
                    logger.warn("Exception encountered during context initialization - " +
                            "cancelling refresh attempt: " + ex);
                }

                // Destroy already created singletons to avoid dangling resources.
                destroyBeans();

                // Reset 'active' flag.
                cancelRefresh(ex);

                // Propagate exception to caller.
                throw ex;
            }

            finally {
                // Reset common introspection caches in Spring's core, since we
                // might not ever need metadata for singleton beans anymore...
                resetCommonCaches();
            }
        }
    }

扫描后,BeanDefinition被放到BeanFactory中,等待下一步创建出来

二、spring bean的生命周期

bean 生命周期

参考

ConfigurationClassPostProcessor —— Spring中最!最!最!重要的后置处理器!没有之一!!!
理解 Spring(二):AOP 的概念与实现原理

上一篇 下一篇

猜你喜欢

热点阅读