美文网首页
SpringIoc源码阅读之整体流程

SpringIoc源码阅读之整体流程

作者: 逍遥白亦 | 来源:发表于2020-11-26 22:18 被阅读0次

    编译源码请参考,本系列第一篇文章,Spring5源码编译

    1. 源码入口

    首先从这一行开始看。

    ApplicationContext context = new AnnotationConfigApplicationContext(MainStat.class);
    

    2. 实例化容器 AnnotationConfigApplicationContext

    2.1 AnnotationConfigApplicationContext

    首先会先调用该类的有参构造方法

        public AnnotationConfigApplicationContext(Class<?>... componentClasses) {
            this();
            register(componentClasses);
            refresh();
        }
    

    先看该类整体继承机构。


    image

    可见AnnotationConfigApplicationContext继承了GenericApplicationContext,所以会先调用父类的无参构造方法。

    2.2 GenericApplicationContext构造方法

    初始化DefaultListableBeanFactory,并且赋值给beanFactory。

        /**
         * Create a new GenericApplicationContext.
         * @see #registerBeanDefinition
         * @see #refresh
         */
        public GenericApplicationContext() {
            this.beanFactory = new DefaultListableBeanFactory();
        }
    

    2.3 DefaultListableBeanFactory

    先看这个类的结构


    image

    这个类有注册BeanDefinition的能力以及BeanFactory这个接口的能力,这里通过继承将Spring里最上层的BeanFactory接口的能力传递给了DefaultListableBeanFactory,使得DefaultListableBeanFactory拥有了生产对象的能力。

    2.4 AnnotationConfigApplicationContext的无参构造方法

    创建两个类的实例,AnnotatedBeanDefinitionReader和ClassPathBeanDefinitionScanner

        /**
         * Create a new AnnotationConfigApplicationContext that needs to be populated
         * through {@link #register} calls and then manually {@linkplain #refresh refreshed}.
         */
        public AnnotationConfigApplicationContext() {
            this.reader = new AnnotatedBeanDefinitionReader(this);
            this.scanner = new ClassPathBeanDefinitionScanner(this);
        }
    

    2.4.1 AnnotatedBeanDefinitionReader

    该类是可以读取注解的Bean定义读取器。来看这个类的有参构造方法。

        public AnnotatedBeanDefinitionReader(BeanDefinitionRegistry registry, Environment environment) {
            Assert.notNull(registry, "BeanDefinitionRegistry must not be null");
            Assert.notNull(environment, "Environment must not be null");
            this.registry = registry;
            this.conditionEvaluator = new ConditionEvaluator(registry, environment, null);
            AnnotationConfigUtils.registerAnnotationConfigProcessors(this.registry);
        }
    

    请看AnnotationConfigUtils.registerAnnotationConfigProcessors(this.registry)这个方法。

    2.4.1.1 AnnotationConfigUtils.registerAnnotationConfigProcessors(this.registry)

    该方法注册了很多Bean定义的处理器,功能都很强大。

    • ConfigurationAnnotationProcessor:解析@Configuration、@ComponentScan、@ComponentScans以及@Import等注解
    • AutowiredAnnotationProcessor:解析@Autowired
    • RequiredAnnotationProcessor:解析@Required
    • CommonAnnotationProcessor:解析@Resource、@WebServiceRef、@EJB
    • EventListenerMethodProcessor:负责处理@EventListener
    • DefaultEventListenerFactory:负责处理@EventListener
        public static Set<BeanDefinitionHolder> registerAnnotationConfigProcessors(
                BeanDefinitionRegistry registry, @Nullable Object source) {
    
            DefaultListableBeanFactory beanFactory = unwrapDefaultListableBeanFactory(registry);
            if (beanFactory != null) {
                if (!(beanFactory.getDependencyComparator() instanceof AnnotationAwareOrderComparator)) {
                    beanFactory.setDependencyComparator(AnnotationAwareOrderComparator.INSTANCE);
                }
                if (!(beanFactory.getAutowireCandidateResolver() instanceof ContextAnnotationAutowireCandidateResolver)) {
                    beanFactory.setAutowireCandidateResolver(new ContextAnnotationAutowireCandidateResolver());
                }
            }
    
            Set<BeanDefinitionHolder> beanDefs = new LinkedHashSet<>(8);
    
            if (!registry.containsBeanDefinition(CONFIGURATION_ANNOTATION_PROCESSOR_BEAN_NAME)) {
                RootBeanDefinition def = new RootBeanDefinition(ConfigurationClassPostProcessor.class);
                def.setSource(source);
                beanDefs.add(registerPostProcessor(registry, def, CONFIGURATION_ANNOTATION_PROCESSOR_BEAN_NAME));
            }
    
            if (!registry.containsBeanDefinition(AUTOWIRED_ANNOTATION_PROCESSOR_BEAN_NAME)) {
                RootBeanDefinition def = new RootBeanDefinition(AutowiredAnnotationBeanPostProcessor.class);
                def.setSource(source);
                beanDefs.add(registerPostProcessor(registry, def, AUTOWIRED_ANNOTATION_PROCESSOR_BEAN_NAME));
            }
    
            // Check for JSR-250 support, and if present add the CommonAnnotationBeanPostProcessor.
            if (jsr250Present && !registry.containsBeanDefinition(COMMON_ANNOTATION_PROCESSOR_BEAN_NAME)) {
                RootBeanDefinition def = new RootBeanDefinition(CommonAnnotationBeanPostProcessor.class);
                def.setSource(source);
                beanDefs.add(registerPostProcessor(registry, def, COMMON_ANNOTATION_PROCESSOR_BEAN_NAME));
            }
    
            // Check for JPA support, and if present add the PersistenceAnnotationBeanPostProcessor.
            if (jpaPresent && !registry.containsBeanDefinition(PERSISTENCE_ANNOTATION_PROCESSOR_BEAN_NAME)) {
                RootBeanDefinition def = new RootBeanDefinition();
                try {
                    def.setBeanClass(ClassUtils.forName(PERSISTENCE_ANNOTATION_PROCESSOR_CLASS_NAME,
                            AnnotationConfigUtils.class.getClassLoader()));
                }
                catch (ClassNotFoundException ex) {
                    throw new IllegalStateException(
                            "Cannot load optional framework class: " + PERSISTENCE_ANNOTATION_PROCESSOR_CLASS_NAME, ex);
                }
                def.setSource(source);
                beanDefs.add(registerPostProcessor(registry, def, PERSISTENCE_ANNOTATION_PROCESSOR_BEAN_NAME));
            }
    
            if (!registry.containsBeanDefinition(EVENT_LISTENER_PROCESSOR_BEAN_NAME)) {
                RootBeanDefinition def = new RootBeanDefinition(EventListenerMethodProcessor.class);
                def.setSource(source);
                beanDefs.add(registerPostProcessor(registry, def, EVENT_LISTENER_PROCESSOR_BEAN_NAME));
            }
    
            if (!registry.containsBeanDefinition(EVENT_LISTENER_FACTORY_BEAN_NAME)) {
                RootBeanDefinition def = new RootBeanDefinition(DefaultEventListenerFactory.class);
                def.setSource(source);
                beanDefs.add(registerPostProcessor(registry, def, EVENT_LISTENER_FACTORY_BEAN_NAME));
            }
    
            return beanDefs;
        }
    
    2.4.1.1.1 ConfigurationClassPostProcessor

    这里只分析这个类,其他类同理。该类结构为


    image

    可以看到该类实现BeanDefinitionRegistryPostProcessor接口,BeanDefinitionRegistryPostProcessor接口又扩展了BeanFactoryPostProcessor接口, BeanFactoryPostProcessor是Spring的扩展点之一,ConfigurationClassPostProcessor是Spring极为重要的类。

    2.4.2 ClassPathBeanDefinitionScanner

    由于常规使用方式是不会用到AnnotationConfigApplicationContext里面的scanner的,这里的scanner 仅仅是为了程序员可以手动调用AnnotationConfigApplicationContext对象的scan方法。所以这里就不看 scanner是如何被实例化的了。

    2.5 register(componentClasses)

    接下来分析第二行代码,register(componentClasses)。最终会调用doRegisterBean方法。

    private <T> void doRegisterBean(Class<T> beanClass, @Nullable String name,
                @Nullable Class<? extends Annotation>[] qualifiers, @Nullable Supplier<T> supplier,
                @Nullable BeanDefinitionCustomizer[] customizers) {
    
            //AnnotatedGenericBeanDefinition可以理解为一种数据结构,是用来描述Bean的,这里的作用就是把传入的标记了注解的类
            //转为AnnotatedGenericBeanDefinition数据结构,里面有一个getMetadata方法,可以拿到类上的注解
            AnnotatedGenericBeanDefinition abd = new AnnotatedGenericBeanDefinition(beanClass);
            //判断是否需要跳过注解,spring中有一个@Condition注解,当不满足条件,这个bean就不会被解析
            if (this.conditionEvaluator.shouldSkip(abd.getMetadata())) {
                return;
            }
    
            abd.setInstanceSupplier(supplier);
            //解析bean的作用域,如果没有设置的话,默认为单例
            ScopeMetadata scopeMetadata = this.scopeMetadataResolver.resolveScopeMetadata(abd);
            abd.setScope(scopeMetadata.getScopeName());
            //获得beanName
            String beanName = (name != null ? name : this.beanNameGenerator.generateBeanName(abd, this.registry));
            //解析通用注解,填充到AnnotatedGenericBeanDefinition,解析的注解为Lazy,Primary,DependsOn,Role,Descri ption
            AnnotationConfigUtils.processCommonDefinitionAnnotations(abd);
            //限定符处理,不是特指@Qualifier注解,也有可能是Primary,或者是Lazy,或者是其他(理论上是任何注解,这里没有判 断注解的有效性),如果我们在外面,以类似这种
            if (qualifiers != null) {
                for (Class<? extends Annotation> qualifier : qualifiers) {
                    //Primary注解优先
                    if (Primary.class == qualifier) {
                        abd.setPrimary(true);
                    }
                    else if (Lazy.class == qualifier) {
                        abd.setLazyInit(true);
                    }
                    else {
                        abd.addQualifier(new AutowireCandidateQualifier(qualifier));
                    }
                }
            }
            if (customizers != null) {
                for (BeanDefinitionCustomizer customizer : customizers) {
                    customizer.customize(abd);
                }
            }
    
            BeanDefinitionHolder definitionHolder = new BeanDefinitionHolder(abd, beanName);
            definitionHolder = AnnotationConfigUtils.applyScopedProxyMode(scopeMetadata, definitionHolder, this.registry);
            //注册,最终会调用DefaultListableBeanFactory中的registerBeanDefinition方法去注册,
            //DefaultListableBeanFactory维护着一系列信息,比如beanDefinitionNames,beanDefinitionMap
            // beanDefinitionNames是一个List<String>,用来保存beanName
            // beanDefinitionMap是一个Map,用来保存beanName和beanDefinition
            BeanDefinitionReaderUtils.registerBeanDefinition(definitionHolder, this.registry);
        }
    

    2.6 refresh()方法

    这个方法是Spring里边很重要的一个方法,做了很多事情。本方法一共有13个小方法,本次只对跟Ioc有关的方法进行阅读,后续对其他方法进行系统分析。

    @Override
        public void refresh() throws BeansException, IllegalStateException {
            synchronized (this.startupShutdownMonitor) {
                // 刷新预处理,和主流程关系不大,就是保存了容器的启动时间,启动标志等.
                prepareRefresh();
    
                // 和主流程关系也不大,最终获得了DefaultListableBeanFactory, DefaultListableBeanFactory实现了ConfigurableListableBeanFactory
                ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
    
                ////还是一些准备工作,添加了两个后置处理器:ApplicationContextAwareProcessor,ApplicationListenerDetector
                // 还设置了 忽略自动装配 和 允许自动装配 的接口,如果不存在某个bean的时候,spring就自动注册singleton bean
                // 还设置了bean表达式解析器 等
                prepareBeanFactory(beanFactory);
    
                try {
                    //这是一个空方法,给子类实现
                    postProcessBeanFactory(beanFactory);
    
                    // 调用我们的bean工厂的后置处理器,做了两件事
                    // 1.调用BeanFactoryPostProcessor
                    // 2.扫描class为BeanDefinition
                    invokeBeanFactoryPostProcessors(beanFactory);
    
                    // 注册BeanPostProcessor
                    registerBeanPostProcessors(beanFactory);
    
                    // 初始化信息源
                    initMessageSource();
    
                    // 创建事件多播器
                    initApplicationEventMulticaster();
    
                    // 模板方法,在容器刷新的时候可以自定义逻辑,不同的Spring容器做不同的事情
                    onRefresh();
    
                    // 把事件监听注册到多播器
                    registerListeners();
    
                    // 实例化所有剩余的(非懒加载)单例
                    finishBeanFactoryInitialization(beanFactory);
    
                    // 最后刷新
                    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();
                }
            }
        }
    

    2.6.1 prepareRefresh

    从命名来看,就知道这个方法主要做了一些刷新前的准备工作,和主流程关系不大,主要是保存了容器的启动时间,启动标志等。

    2.6.2 obtainFreshBeanFactory()

    这个方法和主流程关系也不是很大,可以简单的认为,就是把beanFactory取出来而已。XML模式下会在这里读取BeanDefinition

    2.6.3 prepareBeanFactory

    主要做了如下的操作:

    1. 设置了一个类加载器
    2. 设置了bean表达式解析器
    3. 添加了属性编辑器的支持
    4. 添加了一个后置处理器:ApplicationContextAwareProcessor,此后置处理器实现了BeanPostProcessor接口
    5. 设置了一些忽略自动装配的接口
    6. 设置了一些允许自动装配的接口,并且进行了赋值操作
    7. 在容器中还没有XX的bean的时候,帮我们注册beanName为XX的singleton bean
    protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory) {
            // Tell the internal bean factory to use the context's class loader etc.
            beanFactory.setBeanClassLoader(getClassLoader());
            beanFactory.setBeanExpressionResolver(new StandardBeanExpressionResolver(beanFactory.getBeanClassLoader()));
            beanFactory.addPropertyEditorRegistrar(new ResourceEditorRegistrar(this, getEnvironment()));
    
            // Configure the bean factory with context callbacks.
            beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this));
            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.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.
            beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(this));
    
            // Detect a LoadTimeWeaver and prepare for weaving, if found.
            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.
            if (!beanFactory.containsLocalBean(ENVIRONMENT_BEAN_NAME)) {
                beanFactory.registerSingleton(ENVIRONMENT_BEAN_NAME, getEnvironment());
            }
            if (!beanFactory.containsLocalBean(SYSTEM_PROPERTIES_BEAN_NAME)) {
                beanFactory.registerSingleton(SYSTEM_PROPERTIES_BEAN_NAME, getEnvironment().getSystemProperties());
            }
            if (!beanFactory.containsLocalBean(SYSTEM_ENVIRONMENT_BEAN_NAME)) {
                beanFactory.registerSingleton(SYSTEM_ENVIRONMENT_BEAN_NAME, getEnvironment().getSystemEnvironment());
            }
        }
    
    
    ### 2.6.4  postProcessBeanFactory(beanFactory)
    空方法
    
    ### 2.6.5 invokeBeanFactoryPostProcessors(beanFactory)
    重要方法,下次分析后置处理器时,进行分析。
    
    做了两件事:
    1. 调用BeanFactoryPostProcessor
    2. 扫描class为BeanDefinition
    
    ### 2.6.6 registerBeanPostProcessors(beanFactory)
    实例化和注册beanFactory中扩展了BeanPostProcessor的bean
    ### 2.6.7 initMessageSource()
    初始化国际化资源处理器
    ### 2.6.8 initApplicationEventMulticaster()
    创建事件多播器
    ### 2.6.9 onRefresh()
    模板方法,在容器刷新的时候可以自定义逻辑,不同的Spring容器做不同的事情
    ### 2.6.10 registerListeners()
    注册监听器
    ### 2.6.11 finishBeanFactoryInitialization(beanFactory)
    实例化所有剩余的(非懒加载)单例 比如invokeBeanFactoryPostProcessors方法中根据各种注解解析出来的类,在这个时候都会被初始化。 实例化的过程各种BeanPostProcessor开始起作用。
    

    相关文章

      网友评论

          本文标题:SpringIoc源码阅读之整体流程

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