乐趣区

关于spring:Bean-的初始化过程

本文次要介绍 Bean 的初始化过程,在介绍 Bean 的初始化之前先介绍 IOC 容器的相干设计,因为 Bean 是在 IOC 容器中初始化的。

Spring IOC 容器

IOC 容器是 Spring 的根本组件,IoC 容器(又名 DI 容器)是 Spring 实现主动依赖注入的组件,Spring 通过 IOC 容器治理对象的创立及其生命周期,并向类注入依赖项。IoC 容器能够创立指定类的对象,并在运行时通过构造函数、属性或办法注入所有依赖对象,并在适当的时候销毁它。

个别 IOC 容器须要具备一下性能:

  1. Bean 的注册,须要通知 IOC 容器须要治理哪些对象。
  2. Bean 的解析,IOC 容器须要解析出对象的依赖并依据配置进行依赖注入。
  3. Bean 的生命周期治理,IOC 容器须要治理 Bean 的生命周期。

Spring 的 IOC 容器设计

在 Spring IOC 容器的设计中,能够看到两个次要的容器系列,一个是实现 BeanFactory 接口的简略容器系列,实现了容器的基本功能;另一个是 ApplicationContext 利用上下文,是更高级的容器,在简略容器的根底上减少了许多个性。

上图是 Spring IOC 容器的接口设计,不蕴含类之间的继承关系。

上面别离来看看各个接口定义的性能:

上面提到的办法都省去了入参和返回值。

  • BeanFactory: 次要定义了 getBean()办法以及 getBean()的重载办法用来获取某个 Bean,除此之外还有 getType()办法获取某个 Bean 的 Class, containsBean()判断 IOC 容器是否含有某个 Bean, isTypeMatch()用来判断 Bean 的类型是否与某个类型相匹配, isSingleton(), isPrototype()办法用来判读 Bean 的类型。
  • AutowireCapableBeanFactory: 次要定义了 Autowire 的性能。AutowireCapableBeanFactory 继承了 BeanFactory,同时新增了 applyBeanPostProcessorsBeforeInitialization()和 applyBeanPostProcessorsAfterInitialization()用于在 Bean 的初始化前后利用 BeanPostProcessors,autowire() 用于 Bean 的主动绑定,createBean() 依据 autowire 模式创立一个残缺的 Bean,resolveDependency() 用于解析某个 Bean 的依赖,resolveNamedBean() 依据 Bean 的 Class 去获取一个 Bean,包含 Bean 的名字,resolveBeanByName() 依据名字去获取一个 Bean,destroyBean(), initializeBean()别离在 Bean 被销毁和创立实现调用,applyBeanPropertyValues() 将 beanDefinition 的属性利用到某个 Bean 中。
  • HierarchicalBeanFactory: 在 BeanFactory 的根底上新增了 getParentBeanFactory()用于获取父 BeanFactory, containsLocalBean() 用于判断以后的 BeanFactory 是否蕴含某个 Bean,不包含父 BeanFactoy。
  • ListableBeanFactory: 在 BeanFactory 的根底上减少了获取依据 bean 的 class,注解获取 beanName, bean 的办法,返回的 beanName 或者 bean 可能有多个,因为满足条件的 bean 有多个,同时新增了跟 beanDefinition 无关的办法,containsBeanDefinition()判断是否含有某个 beanName 的 BeanDefiniton, getBeanDefinitionCount()获取 BeanDefinition 的数量,getBeanDefinitionNames()获取 BeanDefinition 的名字。
  • ConfigurableBeanFactory:继承了 HierarchicalBeanFactory,提供了对 beanFactory 的配置性能,包含 scope, parentBeanFactory, beanClassLoader, tempClassLoader, beanExpressionResolver, conversionService, propertyEditorRegister, typeConverter, beanPostProcessor 等的设置。
  • ResourceLoader: 定义了加载资源(classPath 下或者文件系统的资源)的办法,getResource()依据给定的 location 返回相应的 Resource, getClassLoader()返回 ResourceLoader 的 classLoader。
  • ResourcePatternResolver: 继承自 ResourceLoader,定义了 getResources(),通过解析 location, 返回多个 Resource。
  • MessageSource: 定义了音讯解析的构造,三个参数不同的 getMessage()的办法示意通过不同的形式进行音讯的解析。
  • EnvironmentCapable: 定义了一个裸露 Environment 的办法 getEnvironment()。
  • ApplicationEventPublisher: 定义了跟事件公布的办法 publishEvent()。
  • ApplicationContext: 继承了 EnvironmentCapable, ListableBeanFactory, HierarchicalBeanFactory,MessageSource, ApplicationEventPublisher, ResourcePatternResolver,意味着 ApplicationContext 是一个 BeanFactory, 同时反对设置 parentBeanFactory, 事件公布,音讯解析,资源解析等性能,除此之外还聚合了 AutowireCapableBeanFactory,因而 ApplicationContext 比简略的 BeanFactory 领有更多的性能。
  • ConfigurableApplicationContext: 在 ApplicationContext 的根底上继承了 Lifecycle, 反对 start, stop 的生命周期的管制,同时定义了 refresh()办法用于 IOC 容器的初始化。
  • WebApplicationContext: 在 ApplicationContext 的根底上新增了 getServletContext()办法获取 ServletContext。

以上就是 Spring IOC 容器次要的接口设计,能够看到右边的为 BeanFactory 继承关系,左边的为 Application 的继承关系。

从上述的接口设计中,能够看到,在 Spring 框架中提供了多个 IOC 容器,这里以 DefaultListableBeanFactory 为例来看 Bean 的初始化,DefaultListableBeanFactory 提供了 IOC 的基本功能,更多高级的 IOC 容器也是基于 DefaultListableBeanFactory 实现的。Bean 的注册实际上就是 BeanDefinition 的加载过程,曾经记录在 BeanDefinition 的加载过程。

Bean 初始化过程

同样先来看一下 DefaultListableBeanFactory 的整个继承关系,整个继承关系能够分为两局部,一部分于 BeanFactory 相干,一部分是 Bean 和 BeanDefinition 的相干。对于 BeanFactory 的后面曾经介绍过了一部分,这里次要介绍没有介绍过的。

最顶层的接口是 AliasRegistry, 次要用来治理的 Alias(中文翻译为别名)的。SimpleAliasRegistry 类实现了 AliasRegistry,并应用一个 map 保留了 alias 到 canonical name(中文翻译为规范名)的映射。BeanDefinitionRegistry 接口继承 AliasRegistry,次要用来治理 BeanDefinition。SingletonBeanRegistry 接口次要用来治理 singleton 类型的 bean。DefaultSingletonBeanRegistry 继承 SimpleAliasRegistry 和实现了 SingletonBeanRegistry,提供了 alias 和 bean 的治理性能,保留了 beanName 到 bean 实例的映射,以及 bean 之间的依赖关系。FactoryBeanRegistrySupport 在 DefaultSingletonBeanRegistry 提供了与 FactoryBean 集成的性能。AbstractBeanFactory 继承 FactoryBeanRegistrySupport 实现了 ConfigurableBeanFactory,意味着 AbstractBeanFactory 领有了 bean 的注册,获取,移除,beanFactory 的配置的性能。AbstractAutowireCapableBeanFactory 继承了 AbstractBeanFactory 实现了 AutowireCapableBeanFactory, 实现了主动绑定 (依赖注入) 的性能。最初的 DefaultListableBeanFactor 继承自 AbstractAutowireCapableBeanFactory,同时实现了 ConfigurableListableBeanFactory, BeanDefinitionRegistry,集 BeanDefinition 的治理,Bean 的治理,BeanFactory 的配置,依赖注入于一身。

同样以 Spring 中单元测试为例开始剖析 IOC 容器初始化的过程:

@Test
void reregisterBeanDefinition() {DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    RootBeanDefinition bd1 = new RootBeanDefinition(TestBean.class);
    bd1.setScope(BeanDefinition.SCOPE_PROTOTYPE);
    lbf.registerBeanDefinition("testBean", bd1);
    assertThat(lbf.getBean("testBean")).isInstanceOf(TestBean.class);
    RootBeanDefinition bd2 = new RootBeanDefinition(NestedTestBean.class);
    bd2.setScope(BeanDefinition.SCOPE_SINGLETON);
    lbf.registerBeanDefinition("testBean", bd2);
    assertThat(lbf.getBean("testBean")).isInstanceOf(NestedTestBean.class);
}

上述代码能够概括为以下几步:

  1. 创立一个 RootBeanDefinition,并填充相应的属性,BeanDefinition 的加载过程就是将 xml 文件或者注解转换为 BeanDefinition。
  2. 将 BeanDefinition 注册到 BeanFactory 外面。
  3. 获取一个 Bean,真正开始 Bean 的初始化与注册。

后面两步很简略,就是创立一个 TestBean 对应的 BeanDefinition, 而后将 BeanDefinition 注册到 DefaultListableBeanFactory 中去。DefaultListableBeanFactory 实现了 BeanDefinitionRegistry 接口。

在剖析 getBean 的办法之前,先来看一下 DefaultListableBeanFactory 的属性。

/** Whether to allow re-registration of a different definition with the same name. */
/** 是否容许注册雷同的名称的不同的 BeanDefinition */
private boolean allowBeanDefinitionOverriding = true;

/** Whether to allow eager class loading even for lazy-init beans. */
/**  对于懒加载的 bean 是否容许提前初始化  */
private boolean allowEagerClassLoading = true;

/** Optional OrderComparator for dependency Lists and arrays. */
@Nullable
private Comparator<Object> dependencyComparator;

/** Resolver to use for checking if a bean definition is an autowire candidate. */
/** 查看 BeanDefinition 是否是 autowire 的候选的解析器  */
private AutowireCandidateResolver autowireCandidateResolver = SimpleAutowireCandidateResolver.INSTANCE;

/** Map from dependency type to corresponding autowired value. */
/** 依赖类型到依赖注入的 bean 的映射 */
private final Map<Class<?>, Object> resolvableDependencies = new ConcurrentHashMap<>(16);

/** Map of bean definition objects, keyed by bean name. */
/** beanName -> beanDefinition 的映射,保留 BeanDefinition */
private final Map<String, BeanDefinition> beanDefinitionMap = new ConcurrentHashMap<>(256);

/** Map from bean name to merged BeanDefinitionHolder. */
/** beanName 到 merged BeanDefinition 的映射,merged BeanDefinition 个别是 RootBeanDefinition,如果一个 BeanDefiniiton 是 ChildBeanDefinition, 那么 mergedBeanDefinition 就是它的 paraent BeanDefinition  */
private final Map<String, BeanDefinitionHolder> mergedBeanDefinitionHolders = new ConcurrentHashMap<>(256);

/** Map of singleton and non-singleton bean names, keyed by dependency type. */
/** bean 的依赖类型到 bean 的 beanName 的映射 */
private final Map<Class<?>, String[]> allBeanNamesByType = new ConcurrentHashMap<>(64);

/** Map of singleton-only bean names, keyed by dependency type. */
/** bean Name 到 singletion 类型 beanName 的映射 */
private final Map<Class<?>, String[]> singletonBeanNamesByType = new ConcurrentHashMap<>(64);

/** List of bean definition names, in registration order. */
/** BeanDefiniiton 的名字 */
private volatile List<String> beanDefinitionNames = new ArrayList<>(256);

/** List of names of manually registered singletons, in registration order. */
/** 保留通过 registerSingleton()办法手动注入的单例的 Bean 的名字,即不是 Spring 主动注入的 */
private volatile Set<String> manualSingletonNames = new LinkedHashSet<>(16);

能够看到 DefaultListableBeanFactory 的属性次要保留了 beanName 到 BeanDefinition 的映射,以及 class type 到 beanName 的映射。

接着看 getBean()的实现。

@Override
public Object getBean(String name) throws BeansException {return doGetBean(name, null, null, false);
}

protected <T> T doGetBean(String name, @Nullable Class<T> requiredType, @Nullable Object[] args, boolean typeCheckOnly)
        throws BeansException {
    // 如果 name 带有 "&" 前缀,则去掉前缀
    // 尝试在 bean 的别名映射中依据 name 找到相应的相应的规范名
    // 如果没有,则就是原始的 name
    String beanName = transformedBeanName(name);
    Object beanInstance;

    // Eagerly check singleton cache for manually registered singletons.
    Object sharedInstance = getSingleton(beanName);
    if (sharedInstance != null && args == null) {if (logger.isTraceEnabled()) {if (isSingletonCurrentlyInCreation(beanName)) {
                logger.trace("Returning eagerly cached instance of singleton bean'" + beanName +
                        "'that is not fully initialized yet - a consequence of a circular reference");
            }
            else {logger.trace("Returning cached instance of singleton bean'" + beanName + "'");
            }
        }
        beanInstance = getObjectForBeanInstance(sharedInstance, name, beanName, null);
    }

    else {
        // Fail if we're already creating this bean instance:
        // We're assumably within a circular reference.
        if (isPrototypeCurrentlyInCreation(beanName)) {throw new BeanCurrentlyInCreationException(beanName);
        }

        // Check if bean definition exists in this factory.
        BeanFactory parentBeanFactory = getParentBeanFactory();
        if (parentBeanFactory != null && !containsBeanDefinition(beanName)) {
            // Not found -> check parent.
            String nameToLookup = originalBeanName(name);
            if (parentBeanFactory instanceof AbstractBeanFactory) {return ((AbstractBeanFactory) parentBeanFactory).doGetBean(nameToLookup, requiredType, args, typeCheckOnly);
            }
            else if (args != null) {
                // Delegation to parent with explicit args.
                return (T) parentBeanFactory.getBean(nameToLookup, args);
            }
            else if (requiredType != null) {
                // No args -> delegate to standard getBean method.
                return parentBeanFactory.getBean(nameToLookup, requiredType);
            }
            else {return (T) parentBeanFactory.getBean(nameToLookup);
            }
        }

        if (!typeCheckOnly) {markBeanAsCreated(beanName);
        }

        StartupStep beanCreation = this.applicationStartup.start("spring.beans.instantiate")
                .tag("beanName", name);
        try {if (requiredType != null) {beanCreation.tag("beanType", requiredType::toString);
            }
            RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName);
            checkMergedBeanDefinition(mbd, beanName, args);

            // Guarantee initialization of beans that the current bean depends on.  保障以后 bean 依赖的其余 bean 曾经初始化
            String[] dependsOn = mbd.getDependsOn();
            if (dependsOn != null) {for (String dep : dependsOn) {if (isDependent(beanName, dep)) {
                        // 如果存在循环依赖
                        throw new BeanCreationException(mbd.getResourceDescription(), beanName,
                                "Circular depends-on relationship between'" + beanName + "'and'" + dep + "'");
                    }
                    // 保留依赖关系
                    registerDependentBean(dep, beanName);
                    try {
                        // 先初始化依赖的 Bean
                        getBean(dep);
                    }
                    catch (NoSuchBeanDefinitionException ex) {throw new BeanCreationException(mbd.getResourceDescription(), beanName,
                                "'"+ beanName +"' depends on missing bean '"+ dep +"'", ex);
                    }
                }
            }

            // Create bean instance.
            if (mbd.isSingleton()) {sharedInstance = getSingleton(beanName, () -> {
                    try {return createBean(beanName, mbd, args);
                    }
                    catch (BeansException ex) {
                        // Explicitly remove instance from singleton cache: It might have been put there
                        // eagerly by the creation process, to allow for circular reference resolution.
                        // Also remove any beans that received a temporary reference to the bean.
                        destroySingleton(beanName);
                        throw ex;
                    }
                });
                beanInstance = getObjectForBeanInstance(sharedInstance, name, beanName, mbd);
            }

            else if (mbd.isPrototype()) {
                // It's a prototype -> create a new instance.
                Object prototypeInstance = null;
                try {beforePrototypeCreation(beanName);
                    prototypeInstance = createBean(beanName, mbd, args);
                }
                finally {afterPrototypeCreation(beanName);
                }
                beanInstance = getObjectForBeanInstance(prototypeInstance, name, beanName, mbd);
            }

            else {String scopeName = mbd.getScope();
                if (!StringUtils.hasLength(scopeName)) {throw new IllegalStateException("No scope name defined for bean ´" + beanName + "'");
                }
                Scope scope = this.scopes.get(scopeName);
                if (scope == null) {throw new IllegalStateException("No Scope registered for scope name'" + scopeName + "'");
                }
                try {Object scopedInstance = scope.get(beanName, () -> {beforePrototypeCreation(beanName);
                        try {return createBean(beanName, mbd, args);
                        }
                        finally {afterPrototypeCreation(beanName);
                        }
                    });
                    beanInstance = getObjectForBeanInstance(scopedInstance, name, beanName, mbd);
                }
                catch (IllegalStateException ex) {throw new ScopeNotActiveException(beanName, scopeName, ex);
                }
            }
        }
        catch (BeansException ex) {beanCreation.tag("exception", ex.getClass().toString());
            beanCreation.tag("message", String.valueOf(ex.getMessage()));
            cleanupAfterBeanCreationFailure(beanName);
            throw ex;
        }
        finally {beanCreation.end();
        }
    }

    return adaptBeanInstance(name, beanInstance, requiredType);
}

下面的代码大抵能够分为以下几个局部:

  1. 将 name 转换为布局名,如果带有 & 前缀,则去掉,如果 name 是别名,则尝试从 aliasMap 中返回相应的规范名,如果都不是,则间接返回。
  2. getSingleton()尝试获取曾经初始化的 bean,如果获取到了就间接返回,否则下一步。
  3. 初始化 Bean,分为 prototype 类型的 bean,singletion 类型的 bean,以及其余类型的 bean,三种的类型的 bean 的解决大同小异。

上面以 singleton 类型的 bean 为例持续往下看。

下面的代码中首先查看了以后的 beanName 是否处于正在创立状态的 prototype 的 bean,AbstractBeanFactory 中的 prototypesCurrentlyInCreation 以及 DefaultSingletonBeanRegistry 中的 singletonsCurrentlyInCreation 别离保留了正在创立的 prototype 类型和 singleton 类型的 bean。而后查看是否蕴含存在父 beanFactory,如果存在父 BeanFactory 且以后的 BeanFactory 不蕴含以后 beanName, 则尝试应用父 BeanFactory 的 getBean()办法初始化 bean。如果不存在父 BeanFactory, 首先将 beanName 保留在 AbstractBeanFactory 的 alreadyCreated 属性中,示意正在创立或者创立实现。接着获取 beanName 对应的 merged BeanDefinition, 如果 BeanDefinition 没有 RootBeanDefinition, 则不须要 merge, 否则须要 merge。获取到 merged BeanDefinition 会查看是不是 ”abstract”, 这里的 abstract 指的是本人不能初始化本人,而是本人作为一个 parent 去创立具体的 child BeanDefinition。而后会查看 BeanDefinition 的 depend, 防止在 BeanDefiniton 中存在循环依赖。如果以后的 BeanDefinition 存在依赖,则会先通过 getBean()初始化其依赖的 bean。如果没有则持续初始化以后 bean,持续判断以后的 BeanDefinition 是 singleton 的还是 prototype 或者其余的类型的。
上面以 singleton 为例,

// Create bean instance.
if (mbd.isSingleton()) {sharedInstance = getSingleton(beanName, () -> {
        try {return createBean(beanName, mbd, args);
        }
        catch (BeansException ex) {
            // Explicitly remove instance from singleton cache: It might have been put there
            // eagerly by the creation process, to allow for circular reference resolution.
            // Also remove any beans that received a temporary reference to the bean.
            destroySingleton(beanName);
            throw ex;
        }
    });
    beanInstance = getObjectForBeanInstance(sharedInstance, name, beanName, mbd);
}
public Object getSingleton(String beanName, ObjectFactory<?> singletonFactory) {Assert.notNull(beanName, "Bean name must not be null");
    synchronized (this.singletonObjects) {Object singletonObject = this.singletonObjects.get(beanName);
        if (singletonObject == null) {if (this.singletonsCurrentlyInDestruction) {
                throw new BeanCreationNotAllowedException(beanName,
                        "Singleton bean creation not allowed while singletons of this factory are in destruction" +
                        "(Do not request a bean from a BeanFactory in a destroy method implementation!)");
            }
            if (logger.isDebugEnabled()) {logger.debug("Creating shared instance of singleton bean'" + beanName + "'");
            }
            beforeSingletonCreation(beanName);
            boolean newSingleton = false;
            boolean recordSuppressedExceptions = (this.suppressedExceptions == null);
            if (recordSuppressedExceptions) {this.suppressedExceptions = new LinkedHashSet<>();
            }
            try {singletonObject = singletonFactory.getObject();
                newSingleton = true;
            }
            catch (IllegalStateException ex) {
                // Has the singleton object implicitly appeared in the meantime ->
                // if yes, proceed with it since the exception indicates that state.
                singletonObject = this.singletonObjects.get(beanName);
                if (singletonObject == null) {throw ex;}
            }
            catch (BeanCreationException ex) {if (recordSuppressedExceptions) {for (Exception suppressedException : this.suppressedExceptions) {ex.addRelatedCause(suppressedException);
                    }
                }
                throw ex;
            }
            finally {if (recordSuppressedExceptions) {this.suppressedExceptions = null;}
                afterSingletonCreation(beanName);
            }
            if (newSingleton) {addSingleton(beanName, singletonObject);
            }
        }
        return singletonObject;
    }
}

从下面的代码中能够看到,咱们给 getSingleton()办法传入了一个实现了 ObjectFactory 接口的匿名对象。首先依旧会从 singletonObjects 中取 bean 实例,如果没有找到,会将 beanName 退出到 singletonsCurrentlyInCreation 中示意正在创立。之后就会通过 ObjectFactory 的 getObject()办法进行创立。getObject()办法中调用 AbstractAutowireCapableBeanFactory 的 createBean(beanName, mbd, args)来创立 bean。
持续看 AbstractAutowireCapableBeanFactory 的 createBean()办法。

protected Object createBean(String beanName, RootBeanDefinition mbd, @Nullable Object[] args)
        throws BeanCreationException {if (logger.isTraceEnabled()) {logger.trace("Creating instance of bean'" + beanName + "'");
    }
    RootBeanDefinition mbdToUse = mbd;

    // Make sure bean class is actually resolved at this point, and
    // clone the bean definition in case of a dynamically resolved Class
    // which cannot be stored in the shared merged bean definition. 创立 Class 对象
    Class<?> resolvedClass = resolveBeanClass(mbd, beanName);
    if (resolvedClass != null && !mbd.hasBeanClass() && mbd.getBeanClassName() != null) {mbdToUse = new RootBeanDefinition(mbd);
        mbdToUse.setBeanClass(resolvedClass);
    }

    // Prepare method overrides.
    try {mbdToUse.prepareMethodOverrides();
    }
    catch (BeanDefinitionValidationException ex) {throw new BeanDefinitionStoreException(mbdToUse.getResourceDescription(),
                beanName, "Validation of method overrides failed", ex);
    }

    try {
        // Give BeanPostProcessors a chance to return a proxy instead of the target bean instance.
        Object bean = resolveBeforeInstantiation(beanName, mbdToUse);
        if (bean != null) {return bean;}
    }
    catch (Throwable ex) {throw new BeanCreationException(mbdToUse.getResourceDescription(), beanName,
                "BeanPostProcessor before instantiation of bean failed", ex);
    }

    try {Object beanInstance = doCreateBean(beanName, mbdToUse, args);
        if (logger.isTraceEnabled()) {logger.trace("Finished creating instance of bean'" + beanName + "'");
        }
        return beanInstance;
    }
    catch (BeanCreationException | ImplicitlyAppearedSingletonException ex) {
        // A previously detected exception with proper bean creation context already,
        // or illegal singleton state to be communicated up to DefaultSingletonBeanRegistry.
        throw ex;
    }
    catch (Throwable ex) {
        throw new BeanCreationException(mbdToUse.getResourceDescription(), beanName, "Unexpected exception during bean creation", ex);
    }
}

createBean()办法首先通过 resolveBeanClass()办法来解析 beanClass,而后筹备以后 BeanDefinition 中的 overides 办法(lookup-method 或者 replace-method),次要是查看是否能够被 override, 同时判断该办法有没有被重载(如果该办法名对应的办法只有一个,则没有被重载。),如果不存在该办法,则须要抛出异样。
接下来如果存在 InstantiationAwareBeanPostProcessor,会尝试调用 postProcessBeforeInstantiation()办法做一个前置的解决,这里可能会返回一个 bean 实例,如果这里返回了 bean 示例,意味着 bean 被初始化了,接着执行 BeanPostProcessor 的 postProcessAfterInitialization() 办法,对初始化的 bean 做一些后置解决,设置 BeanDefinition 的 beforeInstantiationResolved 属性,而后就能够返回了。

protected Object resolveBeforeInstantiation(String beanName, RootBeanDefinition mbd) {
    Object bean = null;
    if (!Boolean.FALSE.equals(mbd.beforeInstantiationResolved)) {
        // Make sure bean class is actually resolved at this point.
        if (!mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) {Class<?> targetType = determineTargetType(beanName, mbd);
            if (targetType != null) {bean = applyBeanPostProcessorsBeforeInstantiation(targetType, beanName);
                if (bean != null) {bean = applyBeanPostProcessorsAfterInitialization(bean, beanName);
                }
            }
        }
        mbd.beforeInstantiationResolved = (bean != null);
    }
    return bean;
}

如果不存在 InstantiationAwareBeanPostProcessor 则会持续往下走,走到 doCreateBean()办法,从名字能够看进去,这里开始真正的创立 bean。

protected Object doCreateBean(String beanName, RootBeanDefinition mbd, @Nullable Object[] args)
        throws BeanCreationException {

    // Instantiate the bean.
    BeanWrapper instanceWrapper = null;
    if (mbd.isSingleton()) {instanceWrapper = this.factoryBeanInstanceCache.remove(beanName);
    }
    if (instanceWrapper == null) {instanceWrapper = createBeanInstance(beanName, mbd, args); // new 出一个实例,此时还没有进行主动注入
    }
    Object bean = instanceWrapper.getWrappedInstance();
    Class<?> beanType = instanceWrapper.getWrappedClass();
    if (beanType != NullBean.class) {mbd.resolvedTargetType = beanType;  // 填充 resolvedTargetType}

    // Allow post-processors to modify the merged bean definition.
    synchronized (mbd.postProcessingLock) {if (!mbd.postProcessed) {
            try {applyMergedBeanDefinitionPostProcessors(mbd, beanType, beanName);
            }
            catch (Throwable ex) {throw new BeanCreationException(mbd.getResourceDescription(), beanName,
                        "Post-processing of merged bean definition failed", ex);
            }
            mbd.postProcessed = true;
        }
    }

    // Eagerly cache singletons to be able to resolve circular references
    // even when triggered by lifecycle interfaces like BeanFactoryAware.
    boolean earlySingletonExposure = (mbd.isSingleton() && this.allowCircularReferences &&
            isSingletonCurrentlyInCreation(beanName));
    if (earlySingletonExposure) {if (logger.isTraceEnabled()) {
            logger.trace("Eagerly caching bean'" + beanName +
                    "'to allow for resolving potential circular references");
        }
        addSingletonFactory(beanName, () -> getEarlyBeanReference(beanName, mbd, bean));
    }

    // Initialize the bean instance.
    Object exposedObject = bean;
    try {populateBean(beanName, mbd, instanceWrapper);
        exposedObject = initializeBean(beanName, exposedObject, mbd);
    }
    catch (Throwable ex) {if (ex instanceof BeanCreationException && beanName.equals(((BeanCreationException) ex).getBeanName())) {throw (BeanCreationException) ex;
        }
        else {
            throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Initialization of bean failed", ex);
        }
    }

    if (earlySingletonExposure) {Object earlySingletonReference = getSingleton(beanName, false);
        if (earlySingletonReference != null) {if (exposedObject == bean) {exposedObject = earlySingletonReference;}
            else if (!this.allowRawInjectionDespiteWrapping && hasDependentBean(beanName)) {String[] dependentBeans = getDependentBeans(beanName);
                Set<String> actualDependentBeans = new LinkedHashSet<>(dependentBeans.length);
                for (String dependentBean : dependentBeans) {if (!removeSingletonIfCreatedForTypeCheckOnly(dependentBean)) {actualDependentBeans.add(dependentBean);
                    }
                }
                if (!actualDependentBeans.isEmpty()) {
                    throw new BeanCurrentlyInCreationException(beanName,
                            "Bean with name'" + beanName + "'has been injected into other beans [" +
                            StringUtils.collectionToCommaDelimitedString(actualDependentBeans) +
                            "] in its raw version as part of a circular reference, but has eventually been" +
                            "wrapped. This means that said other beans do not use the final version of the" +
                            "bean. This is often the result of over-eager type matching - consider using" +
                            "'getBeanNamesForType' with the 'allowEagerInit' flag turned off, for example.");
                }
            }
        }
    }

    // Register bean as disposable.
    try {registerDisposableBeanIfNecessary(beanName, bean, mbd);
    }
    catch (BeanDefinitionValidationException ex) {
        throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Invalid destruction signature", ex);
    }

    return exposedObject;
}

大抵能够分为以下几步:

  1. createBeanInstance()创立一个最原始的 bean 的示例,即没有进行依赖注入,也称为 raw bean。
  2. 如果存在 MergedBeanDefinitionPostProcessor,则执行 applyMergedBeanDefinitionPostProcessors()
  3. 如果是 earlySingletonExposure,则执行 addSingletonFactory(),将 bean 示例缓存到 singletonFactories 中,须要留神的是,此时仍旧没有产生依赖注入。
  4. populateBean() 这里才是真正产生以来注入的中央,如果存在依赖,则进行注入。
  5. initializeBean() 这步做一些后置的解决,比方执行 invokeAwareMethods(), applyBeanPostProcessorsBeforeInitialization(), invokeInitMethods(),applyBeanPostProcessorsAfterInitialization()等办法。

第一步 createBeanInstance()会先判断是否有 instanceSupplier, 而后判断是否有 factoryMethod, 存在 instanceSupplier 或者 factoryMehtod,则应用相应的办法去获取一个 raw bean。如果不存在 instanceSupplier 或者 factoryMehtod,则查看是否存在曾经解析的 resolvedConstructorOrFactoryMethod,如果存在则依据 constructorArgumentsResolved 来决定通过 autowireConstructor()或者 instantiateBean()l 来创立 raw bean。如果不存在解析好的 resolvedConstructorOrFactoryMethod,则尝试通过 SmartInstantiationAwareBeanPostProcessor 的 determineCandidateConstructors()办法去确定一个构造函数,如果有返回构造函数则通过 autowireConstructor()创立 raw bean,最初还会查看 BeanDefinition 是否指定了 Preferred constructors,如果指定了,则应用 Preferred constructor 通过 autowireConstructor()进行初始化,如果仍旧没有,就没辙了,通过默认的无参构造函数创立 raw bean。

protected BeanWrapper createBeanInstance(String beanName, RootBeanDefinition mbd, @Nullable Object[] args) {
    // Make sure bean class is actually resolved at this point.
    Class<?> beanClass = resolveBeanClass(mbd, beanName);

    if (beanClass != null && !Modifier.isPublic(beanClass.getModifiers()) && !mbd.isNonPublicAccessAllowed()) {throw new BeanCreationException(mbd.getResourceDescription(), beanName,
                "Bean class isn't public, and non-public access not allowed: " + beanClass.getName());
    }

    Supplier<?> instanceSupplier = mbd.getInstanceSupplier();
    if (instanceSupplier != null) {return obtainFromSupplier(instanceSupplier, beanName);
    }

    if (mbd.getFactoryMethodName() != null) {return instantiateUsingFactoryMethod(beanName, mbd, args);
    }

    // Shortcut when re-creating the same bean...
    boolean resolved = false;
    boolean autowireNecessary = false;
    if (args == null) {synchronized (mbd.constructorArgumentLock) {if (mbd.resolvedConstructorOrFactoryMethod != null) {
                resolved = true;
                autowireNecessary = mbd.constructorArgumentsResolved;
            }
        }
    }
    if (resolved) {if (autowireNecessary) {return autowireConstructor(beanName, mbd, null, null);
        }
        else {return instantiateBean(beanName, mbd);
        }
    }

    // Candidate constructors for autowiring?
    Constructor<?>[] ctors = determineConstructorsFromBeanPostProcessors(beanClass, beanName);
    if (ctors != null || mbd.getResolvedAutowireMode() == AUTOWIRE_CONSTRUCTOR ||
            mbd.hasConstructorArgumentValues() || !ObjectUtils.isEmpty(args)) {return autowireConstructor(beanName, mbd, ctors, args);
    }

    // Preferred constructors for default construction?
    ctors = mbd.getPreferredConstructors();
    if (ctors != null) {return autowireConstructor(beanName, mbd, ctors, null);
    }

    // No special handling: simply use no-arg constructor.
    return instantiateBean(beanName, mbd);
}

创立了 raw bean 之后,第二步如果存在 MergedBeanDefinitionPostProcessor,则尝试应用 MergedBeanDefinitionPostProcessor 去批改 Merged BeanDefinition,比方 AutowiredAnnotationBeanPostProcessor 会在这一步去解析 Autowire 注解,这里能够对 Merged BeanDefiniton 进行批改。

protected void applyMergedBeanDefinitionPostProcessors(RootBeanDefinition mbd, Class<?> beanType, String beanName) {for (MergedBeanDefinitionPostProcessor processor : getBeanPostProcessorCache().mergedDefinition) {processor.postProcessMergedBeanDefinition(mbd, beanType, beanName);
    }
}

第三步,如果是 earlySingletonExposure,则执行 addSingletonFactory(),将 bean 示例缓存到 singletonFactories 中,须要留神的是,此时仍旧没有产生依赖注入。对于 Singleton 类型的 Bean 而言,个别都是容许 earlySingletonExposure 的。

第四步,可能是绝对简单的一步了,这里对 bean 的属性进行填充,会产生依赖注入。

protected void populateBean(String beanName, RootBeanDefinition mbd, @Nullable BeanWrapper bw) {if (bw == null) {if (mbd.hasPropertyValues()) {
            throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Cannot apply property values to null instance");
        }
        else {
            // Skip property population phase for null instance.
            return;
        }
    }

    // Give any InstantiationAwareBeanPostProcessors the opportunity to modify the
    // state of the bean before properties are set. This can be used, for example,
    // to support styles of field injection.
    if (!mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) {for (InstantiationAwareBeanPostProcessor bp : getBeanPostProcessorCache().instantiationAware) {if (!bp.postProcessAfterInstantiation(bw.getWrappedInstance(), beanName)) {return;}
        }
    }

    PropertyValues pvs = (mbd.hasPropertyValues() ? mbd.getPropertyValues() : null);

    int resolvedAutowireMode = mbd.getResolvedAutowireMode();
    if (resolvedAutowireMode == AUTOWIRE_BY_NAME || resolvedAutowireMode == AUTOWIRE_BY_TYPE) {MutablePropertyValues newPvs = new MutablePropertyValues(pvs);
        // Add property values based on autowire by name if applicable.
        if (resolvedAutowireMode == AUTOWIRE_BY_NAME) {autowireByName(beanName, mbd, bw, newPvs);
        }
        // Add property values based on autowire by type if applicable.
        if (resolvedAutowireMode == AUTOWIRE_BY_TYPE) {autowireByType(beanName, mbd, bw, newPvs);
        }
        pvs = newPvs;
    }

    boolean hasInstAwareBpps = hasInstantiationAwareBeanPostProcessors();
    boolean needsDepCheck = (mbd.getDependencyCheck() != AbstractBeanDefinition.DEPENDENCY_CHECK_NONE);

    PropertyDescriptor[] filteredPds = null;
    if (hasInstAwareBpps) {if (pvs == null) {pvs = mbd.getPropertyValues();
        }
        for (InstantiationAwareBeanPostProcessor bp : getBeanPostProcessorCache().instantiationAware) {PropertyValues pvsToUse = bp.postProcessProperties(pvs, bw.getWrappedInstance(), beanName);
            if (pvsToUse == null) {if (filteredPds == null) {filteredPds = filterPropertyDescriptorsForDependencyCheck(bw, mbd.allowCaching);
                }
                pvsToUse = bp.postProcessPropertyValues(pvs, filteredPds, bw.getWrappedInstance(), beanName);
                if (pvsToUse == null) {return;}
            }
            pvs = pvsToUse;
        }
    }
    if (needsDepCheck) {if (filteredPds == null) {filteredPds = filterPropertyDescriptorsForDependencyCheck(bw, mbd.allowCaching);
        }
        checkDependencies(beanName, mbd, filteredPds, pvs);
    }

    if (pvs != null) {applyPropertyValues(beanName, mbd, bw, pvs);
    }
}

首先会判断是否有 InstantiationAwareBeanPostProcessor, 如果存在则会执行 InstantiationAwareBeanPostProcessor 的 postProcessAfterInstantiation()办法,这里扭转 bean 的状态,比方对某个属性进行注入。
接下来依据 BeanDefinition 的 autowireMode 来决定 bean 的依赖注入的形式,比方通过 beanName, beanType 进行依赖注入,对于 AUTOWIRE_BY_NAME 的形式,则通过 autowireByName()办法进行注入。

protected void autowireByName(String beanName, AbstractBeanDefinition mbd, BeanWrapper bw, MutablePropertyValues pvs) {String[] propertyNames = unsatisfiedNonSimpleProperties(mbd, bw);
    for (String propertyName : propertyNames) {if (containsBean(propertyName)) {Object bean = getBean(propertyName);
            pvs.add(propertyName, bean);
            registerDependentBean(propertyName, beanName);
            if (logger.isTraceEnabled()) {
                logger.trace("Added autowiring by name from bean name'" + beanName +
                        "'via property'" + propertyName + "'to bean named'" + propertyName + "'");
            }
        }
        else {if (logger.isTraceEnabled()) {
                logger.trace("Not autowiring property'" + propertyName + "'of bean'" + beanName +
                        "'by name: no matching bean found");
            }
        }
    }
}

其中 unsatisfiedNonSimpleProperties 返回了一组不满足 (指存在存在相应的 writeMethod 然而蕴含在 BeanDefinition 的属性列表里) 的非简略的属性。对于返回的非简略属性,依据属性的名字去初始化相应的 bean, 并保留到属性列表中,同时增加依赖。这里并没有注入初始化的 bean,只是保留了下来。
autowireByType()的操作与 autowireByName 相似,只是依据属性的 Type 去初始化相应的 bean。

持续初始化的流程,此时属性列表外面曾经蕴含了后面的不满足的非简略属性,接下同样会判断有没有 InstantiationAwareBeanPostProcessor,如果有,则执行 InstantiationAwareBeanPostProcessor 的 postProcessProperties()的办法。对于 AutowiredAnnotationBeanPostProcessor,这里会进行属性的依赖注入。

最初如果属性列表不为 null, 则 applyPropertyValues(), 这一步会应用属性列表,会对简略属性赋值,也会进行依赖注入。

protected void applyPropertyValues(String beanName, BeanDefinition mbd, BeanWrapper bw, PropertyValues pvs) {if (pvs.isEmpty()) {return;}

    if (System.getSecurityManager() != null && bw instanceof BeanWrapperImpl) {((BeanWrapperImpl) bw).setSecurityContext(getAccessControlContext());
    }

    MutablePropertyValues mpvs = null;
    List<PropertyValue> original;

    if (pvs instanceof MutablePropertyValues) {mpvs = (MutablePropertyValues) pvs;
        if (mpvs.isConverted()) {
            // Shortcut: use the pre-converted values as-is.
            try {bw.setPropertyValues(mpvs);
                return;
            }
            catch (BeansException ex) {
                throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Error setting property values", ex);
            }
        }
        original = mpvs.getPropertyValueList();}
    else {original = Arrays.asList(pvs.getPropertyValues());
    }

    TypeConverter converter = getCustomTypeConverter();
    if (converter == null) {converter = bw;}
    BeanDefinitionValueResolver valueResolver = new BeanDefinitionValueResolver(this, beanName, mbd, converter);

    // Create a deep copy, resolving any references for values.
    List<PropertyValue> deepCopy = new ArrayList<>(original.size());
    boolean resolveNecessary = false;
    for (PropertyValue pv : original) {if (pv.isConverted()) {deepCopy.add(pv);
        }
        else {String propertyName = pv.getName();
            Object originalValue = pv.getValue();
            if (originalValue == AutowiredPropertyMarker.INSTANCE) {Method writeMethod = bw.getPropertyDescriptor(propertyName).getWriteMethod();
                if (writeMethod == null) {throw new IllegalArgumentException("Autowire marker for property without write method:" + pv);
                }
                originalValue = new DependencyDescriptor(new MethodParameter(writeMethod, 0), true);
            }
            Object resolvedValue = valueResolver.resolveValueIfNecessary(pv, originalValue);
            Object convertedValue = resolvedValue;
            boolean convertible = bw.isWritableProperty(propertyName) &&
                    !PropertyAccessorUtils.isNestedOrIndexedProperty(propertyName);
            if (convertible) {convertedValue = convertForProperty(resolvedValue, propertyName, bw, converter);
            }
            // Possibly store converted value in merged bean definition,
            // in order to avoid re-conversion for every created bean instance.
            if (resolvedValue == originalValue) {if (convertible) {pv.setConvertedValue(convertedValue);
                }
                deepCopy.add(pv);
            }
            else if (convertible && originalValue instanceof TypedStringValue &&
                    !((TypedStringValue) originalValue).isDynamic() &&
                    !(convertedValue instanceof Collection || ObjectUtils.isArray(convertedValue))) {pv.setConvertedValue(convertedValue);
                deepCopy.add(pv);
            }
            else {
                resolveNecessary = true;
                deepCopy.add(new PropertyValue(pv, convertedValue));
            }
        }
    }
    if (mpvs != null && !resolveNecessary) {mpvs.setConverted();
    }

    // Set our (possibly massaged) deep copy.
    try {bw.setPropertyValues(new MutablePropertyValues(deepCopy));
    }
    catch (BeansException ex) {
        throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Error setting property values", ex);
    }
}

所以会判断属性列表有没有转换后,如果曾经是转换后的属性列表,示意曾经对属性进行过解析,则间接通过 setPropertyValues()注入,否则构建一个 BeanDefinitionValueResolver,通过这个 resolver 去解析属性列表,解析过程就是对不同的数据类型进行解析,比方根本数据类型,汇合,对象利用等,解析的如果是对象援用,则会初始化相应的利用,所有的属性解析实现之后,同样通过 setPropertyValues()进行注入。

最初一步 initializeBean()对曾经进行过依赖注入的 bean 做一些后置的解决。

protected Object initializeBean(String beanName, Object bean, @Nullable RootBeanDefinition mbd) {if (System.getSecurityManager() != null) {AccessController.doPrivileged((PrivilegedAction<Object>) () -> {invokeAwareMethods(beanName, bean);
            return null;
        }, getAccessControlContext());
    }
    else {invokeAwareMethods(beanName, bean);
    }

    Object wrappedBean = bean;
    if (mbd == null || !mbd.isSynthetic()) {wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);
    }

    try {invokeInitMethods(beanName, wrappedBean, mbd);
    }
    catch (Throwable ex) {
        throw new BeanCreationException((mbd != null ? mbd.getResourceDescription() : null),
                beanName, "Invocation of init method failed", ex);
    }
    if (mbd == null || !mbd.isSynthetic()) {wrappedBean = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);
    }

    return wrappedBean;
}

如果 bean 实现了相应的 Aware 接口,比方 BeanNameAware, BeanClassLoaderAware, BeanFactoryAware 等,回调相应的接口。而后如果存在 BeanPostProcessor,则调用 BeanPostProcessor 的 postProcessBeforeInitialization()的办法,如果 bean 实现了 InitializingBean 则调用 afterPropertiesSet()办法,如果指定了 initMethod, 则调用 initMethod 办法,如果存在 BeanPostProcessor,则调用 BeanPostProcessor.postProcessAfterInitialization()办法。

最初如果 bean 指定了销毁办法,则将该 bean 退出到 disposableBeans 中,在销毁时回调。

至此,一个 bean 的创立就实现了,还有一些收尾操作,比方将 bean 从 singletonsCurrentlyInCreation,singletonFactories 中移除,增加到 singletonObjects,registeredSingletons 中。

bean 曾经寄存在容器的属性里了,间接通过 beanName 去取即可,最初如果有传入 type 的话,会尝试做一下类型转换后再返回。

总结

整个 bean 的初始化流程能够总结如下:

退出移动版