Spring源码阅读

作者: 小蜗牛Aaron | 来源:发表于2019-12-10 19:45 被阅读0次

spring是一个庞大的框架体系,提供了IOC AOP 等等功能。它给我们企业应用提供便利的同时,不可否认的是其内部框架之美,对我们编写代码提供的大量的思想上面的参考。
Spring源码系列将从类图结构,设计模式的角度来阐述Spring主要的源码。主要先从Context容器,配置加载,BeanDefinition加载等方面。

ApplicationContext

image.png

ApplicationContext主要实现了6个接口
EnvironmentCable
这个是和环境相关的接口,里面只有获取环境变量的方法。
ListableBeanFactory
这个接口是BeanFactory的扩展实现。和HierarchicalBeanFactory接口一样,Listable主要的是可以根据名字获取处理一系列的bean实例 不必要通过名字一个一个的处理。而HierarchicalBeanFactory主要的处理父子容器的beanFactory。然而如果一个类没有实现父子容器的beanFactory 代表着不会去找其父容器的beanDefinition。
MessageSource
国际化的接口
提供了获取国际化信息的功能
ApplicationEventPublisher
应用事件的发布
ResourcePatternResolver
提供了加载资源的解析接口

ApplicationContext主要的实现类型

image.png

从图中我们可以看到 AbstractApplicationContext 主要有两种实现 一种是AbstractRefreshableApplicationContext 另一个是GenericApplicationContext 他们之间的不同是 GenericApplicationContext实现了beanDefinition注册接口

下面我们选取一个大家最熟悉的ClassPathXmlApplicationContext来看看ApplicationContext加载的全过程。

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

            // Tell the subclass to refresh the internal bean factory.
            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();
            }
        }
    }

这里是刷新beanFactory的接口的是实现


未命名文件.png

prepareBeanFactory:

B}3(W@RD$%R1TWV$HU$C%C5.png

相关文章

网友评论

    本文标题:Spring源码阅读

    本文链接:https://www.haomeiwen.com/subject/cvfbgctx.html