• Spring源码阅读 prepareBeanFactory


    1. 概述

    前面说到 Reader 实例化时注册了一些 BD 进入容器。
    在 refresh 过程中,也注入了一些 Bean 到容器,注意这里注入的不是 BD 而是直接注入 Bean。

    2. prepareBeanFactory

    org.springframework.context.support.AbstractApplicationContext#prepareBeanFactory

        protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory) {
            // 注意看下面的,下面要么是直接设置 BeanFactory 的属性,要么是直接向 BeanFactory 注入 Bean 实例而非注入 BeanDefinition
    
            // Tell the internal bean factory to use the context's class loader etc.
            beanFactory.setBeanClassLoader(getClassLoader());
            /// SpEL 表达式,Spring Expression Language,插值表达式?SpEL?
            if (!shouldIgnoreSpel) {
                beanFactory.setBeanExpressionResolver(new StandardBeanExpressionResolver(beanFactory.getBeanClassLoader()));
            }
            // 属性编辑器?
            beanFactory.addPropertyEditorRegistrar(new ResourceEditorRegistrar(this, getEnvironment()));
    
            // Configure the bean factory with context callbacks.
            // 处理器,处理一些接口回调,调用接口,传入一些 Spring 内部的对象, 都是一些 Aware 接口, 内部可以看到支持的接口, 最顶层都是 Aware 接口
            // 显然这些调用都是在 Bean 被实例化之后的事
            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.ignoreDependencyInterface(ApplicationStartupAware.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.
            // 继承了 ApplicationListener 接口的处理,处理了什么?
            beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(this));
    
            // Detect a LoadTimeWeaver and prepare for weaving, if found.
            // 不了解
            if (!NativeDetector.inNativeImage() && 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
            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());
            }
            // applicationStartup
            if (!beanFactory.containsLocalBean(APPLICATION_STARTUP_BEAN_NAME)) {
                beanFactory.registerSingleton(APPLICATION_STARTUP_BEAN_NAME, getApplicationStartup());
            }
        }
    

    注意到上面要么是设置 BeanFactory 属性,要么是注入单例 Bean

    3. 注入的 Bean

    1. StandardBeanExpressionResolver
      SpEL 解析器。

    2. ResourceEditorRegistrar
      属性编辑器。咱不知道作用。

    3. ApplicationContextAwareProcessor
      继承 BeanPostProcessor,参与 Bean 的实例化过程。
      当 Bean 继承了一些 Aware 接口时,回调这个接口,注入相应的 Bean。
      支持的 Aware 接口

    if (!(bean instanceof EnvironmentAware || bean instanceof EmbeddedValueResolverAware ||
            bean instanceof ResourceLoaderAware || bean instanceof ApplicationEventPublisherAware ||
            bean instanceof MessageSourceAware || bean instanceof ApplicationContextAware ||
            bean instanceof ApplicationStartupAware)) {
        return bean;
    }
    
    1. ApplicationListenerDetector
      继承 BeanPostProcessor,参与 Bean 的实例化过程。
      暂时不知道作用,暂略。

    2. 一些内部 Bean
      Environment 实例、Map 类型的 systemProperties、Map 类型的 systemEnvironment
      ApplicationStartup 实例,暂不知道作用。

    4. 续

    1. 上面注入了很多 Bean,及 Aware 接口的处理 Bean,实际后面可看到,最开始 getBean 的是 BeanFactoryPostProcessor,而这些 BeanFactoryPostProcessor 很多都是继承了一些 Aware 接口的,那么就能在 getBean 的实例化过程中被处理掉
  • 相关阅读:
    MySQL的sql_mode合理设置
    Redis
    启动Jupyter Notebook提示ImportError: DLL load failed: 找不到指定的模块。
    Linux目录结构
    修改mysql密码报错: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '
    springmvc运行原理
    博客园美化
    数据搜索
    git
    window
  • 原文地址:https://www.cnblogs.com/chenxingyang/p/16120043.html
Copyright © 2020-2023  润新知