先看段代码:
@Overrideprotected Object createBean(String beanName, RootBeanDefinition mbd, @Nullable Object[] args)throws BeanCreationException {if (logger.isDebugEnabled()) {logger.debug("Creating instance of bean '" + beanName + "'");}RootBeanDefinition mbdToUse = mbd;Class<?> resolvedClass = resolveBeanClass(mbd, beanName);if (resolvedClass != null && !mbd.hasBeanClass() && mbd.getBeanClassName() != null) {mbdToUse = new RootBeanDefinition(mbd);mbdToUse.setBeanClass(resolvedClass);}try {mbdToUse.prepareMethodOverrides();}catch (BeanDefinitionValidationException ex) {throw new BeanDefinitionStoreException(mbdToUse.getResourceDescription(),beanName, "Validation of method overrides failed", ex);}try {/**1:当经历过resolveBeforeInstantiation方法后,程序有两个选择,如果创建了代理或者说重写了InstantiationAwareBeanPostProcessor的postProcessBeforeInstantiation方法并在方法postProcessBeforeInstantiation中改变了bean,则直接返回就可以了,否则需要进行常规bean的创建。*/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 {//常规bean的创建Object beanInstance = doCreateBean(beanName, mbdToUse, args);if (logger.isDebugEnabled()) {logger.debug("Finished creating instance of bean '" + beanName + "'");}return beanInstance;}catch (BeanCreationException | ImplicitlyAppearedSingletonException ex) {throw ex;}catch (Throwable ex) {throw new BeanCreationException(mbdToUse.getResourceDescription(), beanName, "Unexpected exception during bean creation", ex);}}
本篇文章主要分析doCreateBean方法,这个是常规bean的创建,代码注释安排:
protected Object doCreateBean(final String beanName, final RootBeanDefinition mbd, final @Nullable Object[] args)throws BeanCreationException {// Instantiate the bean.BeanWrapper instanceWrapper = null;if (mbd.isSingleton()) {//1.如果是单例则需要首先清除缓存instanceWrapper = this.factoryBeanInstanceCache.remove(beanName);}if (instanceWrapper == null) {//2.根据指定bean使用对应的策略创建新的实例,例如:工厂方法、构造函数自动注入、简单初始化instanceWrapper = createBeanInstance(beanName, mbd, args);}final Object bean = instanceWrapper.getWrappedInstance();Class<?> beanType = instanceWrapper.getWrappedClass();if (beanType != NullBean.class) {mbd.resolvedTargetType = beanType;}// Allow post-processors to modify the merged bean definition.synchronized (mbd.postProcessingLock) {if (!mbd.postProcessed) {try {//3.bean合并后的处理,Autowired注解正是通过此方法实现诸如类型的预解析applyMergedBeanDefinitionPostProcessors(mbd, beanType, beanName);}catch (Throwable ex) {throw new BeanCreationException(mbd.getResourceDescription(), beanName,"Post-processing of merged bean definition failed", ex);}mbd.postProcessed = true;}}//4.是否需要提早曝光:是单例 并且 允许循环依赖 并且 当前bean正在创建中,检测循环依赖boolean earlySingletonExposure = (mbd.isSingleton() && this.allowCircularReferences &&isSingletonCurrentlyInCreation(beanName));if (earlySingletonExposure) {if (logger.isDebugEnabled()) {logger.debug("Eagerly caching bean '" + beanName +"' to allow for resolving potential circular references");}//addSingletonFactory:为了避免后期循环依赖,可以在bean初始化完成前将创建实例的ObjectFactory加入工厂缓存//getEarlyBeanReference:对bean再一次依赖引用,SmartInstantiationAwareBeanPostProcessor,其中我们熟知的AOP就是在这里将advice动态织入bean中,若没有则直接返回bean,不做任何处理addSingletonFactory(beanName, () -> getEarlyBeanReference(beanName, mbd, bean));}// Initialize the bean instance.Object exposedObject = bean;try {//5.对bean进行填充,将各个属性值注入,其中,可能存在依赖于其他bean的属性,则会递归初始化依赖的beanpopulateBean(beanName, mbd, instanceWrapper);//5下.调用初始化方法,比如init-method,初始化beanexposedObject = 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) {//6.earlySingtonReference只有在检测到有循环依赖的情况下才会不为空Object earlySingletonReference = getSingleton(beanName, false);if (earlySingletonReference != null) {if (exposedObject == bean) {//如果exposedObject没有在初始化方法中被改变,也就是没有被增强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);}}//因为bean创建后其所依赖的bean一定是已经创建的,actualDependentBeans不为空则表示当前bean创建后其依赖的bean却没有全部创建完,也就是说存在循环依赖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 " +"'getBeanNamesOfType' with the 'allowEagerInit' flag turned off, for example.");}}}}// Register bean as disposable.try {//7.根据scope注册DisposableBeanregisterDisposableBeanIfNecessary(beanName, bean, mbd);}catch (BeanDefinitionValidationException ex) {throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Invalid destruction signature", ex);}//8.返回创建好的beanreturn exposedObject;}
接下来看下doCreateBean方法的概要思路:
(1)如果是单例则需要首先清除缓存(至于什么时候进行put,现在说为时尚早,以后更到那一篇的时候会说,这里留意下)。
(2)实例化bean,将BeanDefinition转换为BeanWrapper。转换是一个复杂的过程,大概分为这几步:
1.如果存在工厂方法则使用工厂方法进行初始化。
2.一个类有多个构造函数,每个构造函数都有不同的参数,所以需要根据参数锁定构造函数并进行初始化。
3.如果既不存在工厂方法也不存在带有参数的构造函数,则使用默认的构造函数进行bean的实例化。
(3)MergedBeanDefinitionPostProcessor的应用,bean合并后的处理,Autowired注解正是通过此方法实现诸如类型的预解析。
(4)循环依赖处理。
(5)属性填充,将所有的属性填充到bean的实例中。
(6)循环依赖检查。
(7)注册DisposableBean,如果配置了destroy-method,这里需要注册以便于在销毁的时候调用。
(8)完成创建并返回。
接下来对doCreateBean方法中重要的几步进行分析:
一、首先看createBeanInstance
protected BeanWrapper createBeanInstance(String beanName, RootBeanDefinition mbd, @Nullable Object[] args) {// 解析classClass<?> 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);}}// 需要根据参数解析构造函数Constructor<?>[] ctors = determineConstructorsFromBeanPostProcessors(beanClass, beanName);if (ctors != null || mbd.getResolvedAutowireMode() == AUTOWIRE_CONSTRUCTOR ||mbd.hasConstructorArgumentValues() || !ObjectUtils.isEmpty(args)) {//构造函数自动注入return autowireConstructor(beanName, mbd, ctors, args);}// No special handling: simply use no-arg constructor.//翻一下,没有特殊的处理,简单使用无参构造return instantiateBean(beanName, mbd);}
概括下createBeanInstance方法做了什么:
(1)mbd.getFactoryMethodName() != null意思就是如果在RootBeanDefinition中存在factoryMethodName属性,或者说在配置文件中配置了factory-method,那么spring会根据RootBeanDefinition中的配置使用instantiateUsingFactoryMethod方法生成bean的实例。
(2)解析构造函数并进行构造函数的实例化,一个bean可能对应多个构造函数,每个构造函数的参数不同,spring会根据参数个数及类型去判断最终会使用哪个构造函数进行实例化,但是这个判断过程很耗性能,所以这里用了缓存的机制,也就是判断resolvedConstructorOrFactoryMethod(看字面意思就知道:已经被解析的构造函数或者工厂方法)不为空就说明解析过了,直接取出来用就行了,如果缓存中没取到,就需要去解析构造函数,然后存入缓存以便下次使用。
看完createBeanInstance方法后你觉得里面哪个方法比较重要?其中有两个方法肯定给你留下了很深的印象,一个是autowireConstructor,另外一个是instantiateBean,因为最后不管构造函数解析过没解析过,都是走的这俩方法中的其中一个,发现了没?autowireConstructor这个方法是带有参数的实例化,意思就是用有参构造的进行实例化,而instantiateBean这个走的就是默认的无参构造了。
1.1、首先看下autowireConstructor方法
public BeanWrapper autowireConstructor(String beanName, RootBeanDefinition mbd,@Nullable Constructor<?>[] chosenCtors, @Nullable Object[] explicitArgs) {BeanWrapperImpl bw = new BeanWrapperImpl();this.beanFactory.initBeanWrapper(bw);Constructor<?> constructorToUse = null;ArgumentsHolder argsHolderToUse = null;Object[] argsToUse = null;//如果getBean方法调用的时候指定方法参数那么直接使用if (explicitArgs != null) {argsToUse = explicitArgs;}else {//如果在getBean方法调用的时候没有指定则尝试从配置文件中解析Object[] argsToResolve = null;//尝试从缓存中获取synchronized (mbd.constructorArgumentLock) {constructorToUse = (Constructor<?>) mbd.resolvedConstructorOrFactoryMethod;if (constructorToUse != null && mbd.constructorArgumentsResolved) {// Found a cached constructor...//从缓存中取argsToUse = mbd.resolvedConstructorArguments;if (argsToUse == null) {//配置的构造函数参数argsToResolve = mbd.preparedConstructorArguments;}}}//如果缓存中存在if (argsToResolve != null) {/**解析参数类型,如给定方法的构造函数是A("1","1"),那么通过此方法后就会把配置中的("1","1")转换为(1,1),当然,缓存中的值可能是原始值也可能是最终值*/argsToUse = resolvePreparedArguments(beanName, mbd, bw, constructorToUse, argsToResolve);}}//没有被缓存if (constructorToUse == null) {// 需要解析构造函数boolean autowiring = (chosenCtors != null ||mbd.getResolvedAutowireMode() == AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR);ConstructorArgumentValues resolvedValues = null;int minNrOfArgs;if (explicitArgs != null) {minNrOfArgs = explicitArgs.length;}else {//提取配置文件中配置的构造函数参数ConstructorArgumentValues cargs = mbd.getConstructorArgumentValues();//用户承载解析后的构造函数参数的值resolvedValues = new ConstructorArgumentValues();//能解析到的参数的个数minNrOfArgs = resolveConstructorArguments(beanName, mbd, bw, cargs, resolvedValues);}// 获取所有的构造函数Constructor<?>[] candidates = chosenCtors;if (candidates == null) {Class<?> beanClass = mbd.getBeanClass();try {candidates = (mbd.isNonPublicAccessAllowed() ?beanClass.getDeclaredConstructors() : beanClass.getConstructors());}catch (Throwable ex) {throw new BeanCreationException(mbd.getResourceDescription(), beanName,"Resolution of declared constructors on bean Class [" + beanClass.getName() +"] from ClassLoader [" + beanClass.getClassLoader() + "] failed", ex);}}/**将给定的构造函数排序,先public类型的构造函数根据参数数量降序排序,然后不是public的构造函数按参数数量降序排序*/AutowireUtils.sortConstructors(candidates);int minTypeDiffWeight = Integer.MAX_VALUE;Set<Constructor<?>> ambiguousConstructors = null;LinkedList<UnsatisfiedDependencyException> causes = null;for (Constructor<?> candidate : candidates) {Class<?>[] paramTypes = candidate.getParameterTypes();if (constructorToUse != null && argsToUse.length > paramTypes.length) {/**如果已经找到选用的构造函数或者需要的参数个数小于当前的构造函数参数个数则终止,因为已经按照参数个数降序排列*/break;}if (paramTypes.length < minNrOfArgs) {/**当前的构造函数个数小于需要的参数个数,就继续,因为构造函数肯定得让这些参数全部传进去啊*/continue;}ArgumentsHolder argsHolder;if (resolvedValues != null) {//有参数则根据值构造对应参数类型的参数try {//注释上获取参数的名称String[] paramNames = ConstructorPropertiesChecker.evaluate(candidate, paramTypes.length);if (paramNames == null) {//获取参数名称探索器ParameterNameDiscoverer pnd = this.beanFactory.getParameterNameDiscoverer();if (pnd != null) {//获取指定构造函数的参数名称paramNames = pnd.getParameterNames(candidate);}}//根据名称和数据类型创建参数持有者argsHolder = createArgumentArray(beanName, mbd, resolvedValues, bw, paramTypes, paramNames,getUserDeclaredConstructor(candidate), autowiring);}catch (UnsatisfiedDependencyException ex) {if (logger.isTraceEnabled()) {logger.trace("Ignoring constructor [" + candidate + "] of bean '" + beanName + "': " + ex);}// Swallow and try next constructor.if (causes == null) {causes = new LinkedList<>();}causes.add(ex);continue;}}else {// Explicit arguments given -> arguments length must match exactly.if (paramTypes.length != explicitArgs.length) {continue;}//构造函数没有参数的情况argsHolder = new ArgumentsHolder(explicitArgs);}//探测是否有不确定性的构造函数存在,例如不同构造函数的参数为父子关系int typeDiffWeight = (mbd.isLenientConstructorResolution() ?argsHolder.getTypeDifferenceWeight(paramTypes) : argsHolder.getAssignabilityWeight(paramTypes));// Choose this constructor if it represents the closest match.//如果它代表着当前最接近的匹配则选择作为构造函数if (typeDiffWeight < minTypeDiffWeight) {constructorToUse = candidate;argsHolderToUse = argsHolder;argsToUse = argsHolder.arguments;minTypeDiffWeight = typeDiffWeight;ambiguousConstructors = null;}else if (constructorToUse != null && typeDiffWeight == minTypeDiffWeight) {if (ambiguousConstructors == null) {ambiguousConstructors = new LinkedHashSet<>();ambiguousConstructors.add(constructorToUse);}ambiguousConstructors.add(candidate);}}if (constructorToUse == null) {if (causes != null) {UnsatisfiedDependencyException ex = causes.removeLast();for (Exception cause : causes) {this.beanFactory.onSuppressedException(cause);}throw ex;}throw new BeanCreationException(mbd.getResourceDescription(), beanName,"Could not resolve matching constructor " +"(hint: specify index/type/name arguments for simple parameters to avoid type ambiguities)");}else if (ambiguousConstructors != null && !mbd.isLenientConstructorResolution()) {throw new BeanCreationException(mbd.getResourceDescription(), beanName,"Ambiguous constructor matches found in bean '" + beanName + "' " +"(hint: specify index/type/name arguments for simple parameters to avoid type ambiguities): " +ambiguousConstructors);}if (explicitArgs == null) {//将解析的构造函数加入缓存argsHolderToUse.storeCache(mbd, constructorToUse);}}try {final InstantiationStrategy strategy = beanFactory.getInstantiationStrategy();Object beanInstance;if (System.getSecurityManager() != null) {final Constructor<?> ctorToUse = constructorToUse;final Object[] argumentsToUse = argsToUse;beanInstance = AccessController.doPrivileged((PrivilegedAction<Object>) () ->strategy.instantiate(mbd, beanName, beanFactory, ctorToUse, argumentsToUse),beanFactory.getAccessControlContext());}else {beanInstance = strategy.instantiate(mbd, beanName, this.beanFactory, constructorToUse, argsToUse);}//将创建的实例加入BeanWrapper中bw.setBeanInstance(beanInstance);return bw;}catch (Throwable ex) {throw new BeanCreationException(mbd.getResourceDescription(), beanName,"Bean instantiation via constructor failed", ex);}}
autowireConstructor有点长,接下来总结下这个方法的大概运行流程:
(1)确定构造函数的参数,分为三步:
1.如果传入的explicitArgs不为空,就可以直接确定参数,因为explicitArgs参数是在调用Bean的时候用户指定的。
2.如果构造函数参数已经记录在缓存里的话,那么可以直接拿来用,这里要注意一点就是缓存中的参数类型不一定是最终类型,如果不是最终类型的话需要转换下类型。
3.如果前两步都获取不到的话就从配置文件配置的构造函数获取,通过BeanDefinition中的getConstructorArgumentValues获取后,再调用resolveConstructorArguments解析得到参数。
(2)确定构造函数,上一步已经确定了构造函数的参数,这一步需要根据这些参数锁定对应的构造函数,匹配的方法就是根据参数个数匹配。
(3)根据确定好的构造函数转换对应的参数类型,比如你的参数是Integer的,但是构造函数需要String的,这时候你得转换成String的才能传入构造函数。
(4)再次验证构造函数,因为有时候就算构造函数,参数名称和类型以及值都确定后也不一定直接锁定构造函数,不同构造函数的参数为父子关系,所以Spring在最后又做了一步验证。
(5)根据实例化策略以及得到得构造函数及构造函数参数实例化Bean。
1.2、然后看下instantiateBean方法
protected BeanWrapper instantiateBean(final String beanName, final RootBeanDefinition mbd) {try {Object beanInstance;final BeanFactory parent = this;if (System.getSecurityManager() != null) {beanInstance = AccessController.doPrivileged((PrivilegedAction<Object>) () ->getInstantiationStrategy().instantiate(mbd, beanName, parent),getAccessControlContext());}else {beanInstance = getInstantiationStrategy().instantiate(mbd, beanName, parent);}BeanWrapper bw = new BeanWrapperImpl(beanInstance);initBeanWrapper(bw);return bw;}catch (Throwable ex) {throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Instantiation of bean failed", ex);}}
这个方法其实就是利用无参构造对bean进行实例化的,无参构造和有参
构造哪个简单?肯定是无参的了,所以这个方法里就直接对bean进行实例
了。其实仔细看下实例化的方法,是这么一段代码:
beanInstance = getInstantiationStrategy().instantiate(mbd, beanName, parent);
接下来就看看这个instantiate方法具体是怎么进行实例化的:
@Overridepublic Object instantiate(RootBeanDefinition bd, @Nullable String beanName, BeanFactory owner) {/**如果有需要覆盖或者动态替换的方法则当然需要使用cglib进行动态代理,因为可以在创建代理的同时将动态方法织入类中,但是如果没有需要动态改变的方法,为了方便直接使用反射就可以了*/if (!bd.hasMethodOverrides()) {Constructor<?> constructorToUse;synchronized (bd.constructorArgumentLock) {constructorToUse = (Constructor<?>) bd.resolvedConstructorOrFactoryMethod;if (constructorToUse == null) {final Class<?> clazz = bd.getBeanClass();if (clazz.isInterface()) {throw new BeanInstantiationException(clazz, "Specified class is an interface");}try {if (System.getSecurityManager() != null) {constructorToUse = AccessController.doPrivileged((PrivilegedExceptionAction<Constructor<?>>) clazz::getDeclaredConstructor);}else {constructorToUse = clazz.getDeclaredConstructor();}bd.resolvedConstructorOrFactoryMethod = constructorToUse;}catch (Throwable ex) {throw new BeanInstantiationException(clazz, "No default constructor found", ex);}}}return BeanUtils.instantiateClass(constructorToUse);}else {// Must generate CGLIB subclass.return instantiateWithMethodInjection(bd, beanName, owner);}}
大概总结下instantiate这个方法,首先判断
!bd.hasMethodOverrides(),如果是true,说明用户没有使用
replace或lookup的配置方法,这时候就直接使用反射,简单快捷,但是
如果使用了这两个特性,就需要使用动态代理的方式将包含两个特性所对应
的逻辑拦截增强器设置进去。
二、接下来看doCreateBean方法中的第四个注释
boolean earlySingletonExposure = (mbd.isSingleton() && this.allowCircularReferences &&isSingletonCurrentlyInCreation(beanName));if (earlySingletonExposure) {if (logger.isDebugEnabled()) {logger.debug("Eagerly caching bean '" + beanName +"' to allow for resolving potential circular references");}//addSingletonFactory:为了避免后期循环依赖,可以在bean初始化完成前将创建实例的ObjectFactory加入工厂缓存//getEarlyBeanReference:对bean再一次依赖引用,SmartInstantiationAwareBeanPostProcessor,其中我们熟知的AOP就是在这里将advice动态织入bean中,若没有则直接返回bean,不做任何处理addSingletonFactory(beanName, () -> getEarlyBeanReference(beanName, mbd, bean));}
earlySingletonExposure表示是否提前曝光,有三个条件决定这个
变量的值,当前bean的scope是单例并且允许循环依赖并且当前单例bean正
在被创建。然后往下看,最关键的一句代码来了,getEarlyBeanReference这个方法是什么?你还记得循环依赖怎么解决的
吗?是不是从singletonFactories中取出来一个singletonFactory
,然后调用了singletonFactory的getObject方法,其中这个getObject
方法就是这个getEarlyBeanReference,这里java8的lambda表达式有点
迷惑性,addSingletonFactory用jdk7表示如下:
addSingletonFactory(beanName, new ObjectFactory(){public Object getObject() throws BeansException{return getEarlyBeanReference(beanName, mbd, bean);}});
然后看下getEarlyReference方法的代码:
protected Object getEarlyBeanReference(String beanName, RootBeanDefinition mbd, Object bean) {Object exposedObject = bean;if (!mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) {for (BeanPostProcessor bp : getBeanPostProcessors()) {if (bp instanceof SmartInstantiationAwareBeanPostProcessor) {SmartInstantiationAwareBeanPostProcessor ibp = (SmartInstantiationAwareBeanPostProcessor) bp;exposedObject = ibp.getEarlyBeanReference(exposedObject, beanName);}}}return exposedObject;}
看见没,循环依赖能解决的根本原理就是返回了一个未创建完成的A,
,而且这个A还是一开始创建时候那个半成品的A,简单画个图:

三、继续看populateBean方法
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 {return;}}/**给InstantiationAwareBeanPostProcessors最后一次机会在属性设置前来改变bean*/boolean continueWithPropertyPopulation = true;if (!mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) {for (BeanPostProcessor bp : getBeanPostProcessors()) {if (bp instanceof InstantiationAwareBeanPostProcessor) {InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp;//返回值为是否继续填充beanif (!ibp.postProcessAfterInstantiation(bw.getWrappedInstance(), beanName)) {continueWithPropertyPopulation = false;break;}}}}//如果后处理器发出停止填充命令则终止后续的执行if (!continueWithPropertyPopulation) {return;}PropertyValues pvs = (mbd.hasPropertyValues() ? mbd.getPropertyValues() : null);if (mbd.getResolvedAutowireMode() == AUTOWIRE_BY_NAME || mbd.getResolvedAutowireMode() == AUTOWIRE_BY_TYPE) {MutablePropertyValues newPvs = new MutablePropertyValues(pvs);// Add property values based on autowire by name if applicable.if (mbd.getResolvedAutowireMode() == AUTOWIRE_BY_NAME) {//根据名称注入autowireByName(beanName, mbd, bw, newPvs);}// Add property values based on autowire by type if applicable.if (mbd.getResolvedAutowireMode() == AUTOWIRE_BY_TYPE) {//根据类型注入autowireByType(beanName, mbd, bw, newPvs);}pvs = newPvs;}//后处理器已经初始化boolean hasInstAwareBpps = hasInstantiationAwareBeanPostProcessors();//需要依赖检查boolean needsDepCheck = (mbd.getDependencyCheck() != AbstractBeanDefinition.DEPENDENCY_CHECK_NONE);if (hasInstAwareBpps || needsDepCheck) {if (pvs == null) {pvs = mbd.getPropertyValues();}PropertyDescriptor[] filteredPds = filterPropertyDescriptorsForDependencyCheck(bw, mbd.allowCaching);if (hasInstAwareBpps) {for (BeanPostProcessor bp : getBeanPostProcessors()) {if (bp instanceof InstantiationAwareBeanPostProcessor) {InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp;//对所有需要依赖检查的属性进行后处理pvs = ibp.postProcessPropertyValues(pvs, filteredPds, bw.getWrappedInstance(), beanName);if (pvs == null) {return;}}}}if (needsDepCheck) {//依赖检查,对应的depends-on属性checkDependencies(beanName, mbd, filteredPds, pvs);}}if (pvs != null) {//将属性应用到bean中applyPropertyValues(beanName, mbd, bw, pvs);}}
总结下populateBean中的大概逻辑:
1.InstantiationAwareBeanPostProcessor处理器的
postProcessAfterInstantiation函数的应用,此函数可以控制程序
是否继续进行属性填充。
2.根据注入类型(byName/byType),提取依赖的bean,并统一存入
propertyValues中。
3.应用InstantiationAwareBeanPostProcessor处理器的
postProcessPropertyValues方法,对属性获取完毕填充前对属性的
再次处理。
4.将所有PropertyValues中的属性填充至BeanWrapper中。
四、继续看initializeBean方法
protected Object initializeBean(final String beanName, final Object bean, @Nullable RootBeanDefinition mbd) {if (System.getSecurityManager() != null) {AccessController.doPrivileged((PrivilegedAction<Object>) () -> {invokeAwareMethods(beanName, bean);return null;}, getAccessControlContext());}else {//对诸如Aware、BeanClassLoaderAware、BeanFactoryAware进行处理invokeAwareMethods(beanName, bean);}Object wrappedBean = bean;if (mbd == null || !mbd.isSynthetic()) {//应用后处理器wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);}try {//激活用户自定义的init方法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;}
五、注册DisposableBean
protected void registerDisposableBeanIfNecessary(String beanName, Object bean, RootBeanDefinition mbd) {AccessControlContext acc = (System.getSecurityManager() != null ? getAccessControlContext() : null);if (!mbd.isPrototype() && requiresDestruction(bean, mbd)) {if (mbd.isSingleton()) {/** 单例模式下注册需要销毁的bean,此方法中会处理实现DisposableBean的bean,并且对所有的bean使用DestructionAwareBeanPostProcessors处理,DisposableBean DestructionAwareBeanPostProcessors*/registerDisposableBean(beanName,new DisposableBeanAdapter(bean, beanName, mbd, getBeanPostProcessors(), acc));}else {//自定义scope处理Scope scope = this.scopes.get(mbd.getScope());if (scope == null) {throw new IllegalStateException("No Scope registered for scope name '" + mbd.getScope() + "'");}scope.registerDestructionCallback(beanName,new DisposableBeanAdapter(bean, beanName, mbd, getBeanPostProcessors(), acc));}}}
spring不仅提供了初始化方法的扩展入口,同样也提供了销毁方法的扩展入口,
可以通过配置destroy-method方法实现,也可以注册后处理器
DestructionAwareBeanPostProcessor来统一处理bean的销毁。
随着这篇文章的结束,spring的bean的加载流程算是写完了,但这只是
冰山一角,每个方法里面调用的方法还可以继续解析,内容实在是太多了,但
是如果你能对bean的加载流程有个大概的了解,主要是那几个关键的方法能说
出来,一般面试的时候有这问题基本上都能答上了!



![[附源码]java毕业设计酒店管理系统](https://img-blog.csdnimg.cn/07310cd38519468e8c4f7148edb20ed9.png)














