【spring源码分析】IOC容器初始化(十一)
前言:前面分析了doCreateBean中的createBeanInstance函数,接下来分析其剩余流程。
首先贴上doCreateBean函数:
1 // AbstractAutowireCapableBeanFactory 2 protected Object doCreateBean(final String beanName, final RootBeanDefinition mbd, final @Nullable Object[] args) 3 throws BeanCreationException { 4 5 // Instantiate the bean. 6 // BeanWrapper是对Bean的包装,其接口中所定义的功能很简单包括设置获取被包装的对象、获取被包装bean的属性描述器 7 BeanWrapper instanceWrapper = null; 8 // 如果是单例模型,则从未完成的FactoryBean缓存中删除 9 if (mbd.isSingleton()) { 10 instanceWrapper = this.factoryBeanInstanceCache.remove(beanName); 11 } 12 // 使用合适的实例化策略来创建新的实例:工厂方法、构造函数自动注入、简单初始化 13 if (instanceWrapper == null) { 14 instanceWrapper = createBeanInstance(beanName, mbd, args); 15 } 16 // 包装的对象实例 17 final Object bean = instanceWrapper.getWrappedInstance(); 18 // 包装的实例对象的类型 19 Class<?> beanType = instanceWrapper.getWrappedClass(); 20 if (beanType != NullBean.class) { 21 mbd.resolvedTargetType = beanType; 22 } 23 24 // Allow post-processors to modify the merged bean definition. 25 // 先做同步,然后判断是否有后置处理 26 synchronized (mbd.postProcessingLock) { 27 if (!mbd.postProcessed) { 28 try { 29 // 后置处理修改BeanDefinition 30 applyMergedBeanDefinitionPostProcessors(mbd, beanType, beanName); 31 } catch (Throwable ex) { 32 throw new BeanCreationException(mbd.getResourceDescription(), beanName, 33 "Post-processing of merged bean definition failed", ex); 34 } 35 mbd.postProcessed = true; 36 } 37 } 38 39 // Eagerly cache singletons to be able to resolve circular references 40 // even when triggered by lifecycle interfaces like BeanFactoryAware. 41 // 解决单例模式的循环依赖 // 单例模式 运行循环依赖 42 boolean earlySingletonExposure = (mbd.isSingleton() && this.allowCircularReferences && 43 isSingletonCurrentlyInCreation(beanName));// 当前单例bean是否正在被创建 44 if (earlySingletonExposure) { 45 if (logger.isDebugEnabled()) { 46 logger.debug("Eagerly caching bean '" + beanName + 47 "' to allow for resolving potential circular references"); 48 } 49 // 提前将创建的bean实例加入到singletonFactories中 50 // 为了后期避免循环依赖 51 addSingletonFactory(beanName, () -> getEarlyBeanReference(beanName, mbd, bean)); 52 } 53 54 // Initialize the bean instance. 55 // 开始初始化bean实例对象 56 Object exposedObject = bean; 57 try { 58 // 对bean进行填充,主要是进行属性注入,其中,可能存在依赖于其他bean的属性,则会递归初始化依赖bean 59 populateBean(beanName, mbd, instanceWrapper); 60 // 进行bean初始化 61 exposedObject = initializeBean(beanName, exposedObject, mbd); 62 } catch (Throwable ex) { 63 if (ex instanceof BeanCreationException && beanName.equals(((BeanCreationException) ex).getBeanName())) { 64 throw (BeanCreationException) ex; 65 } else { 66 throw new BeanCreationException( 67 mbd.getResourceDescription(), beanName, "Initialization of bean failed", ex); 68 } 69 } 70 71 // 循环依赖处理 72 if (earlySingletonExposure) { 73 // 获取earlySingletonReference 74 Object earlySingletonReference = getSingleton(beanName, false); 75 // 只有在循环依赖的情况下,earlySingletonReference才不会为null 76 if (earlySingletonReference != null) { 77 // 如果exposedObject没有在初始化方法中改变,也就是没有被增强 78 if (exposedObject == bean) { 79 exposedObject = earlySingletonReference; 80 } else if (!this.allowRawInjectionDespiteWrapping && hasDependentBean(beanName)) { // 处理依赖 81 String[] dependentBeans = getDependentBeans(beanName); 82 Set<String> actualDependentBeans = new LinkedHashSet<>(dependentBeans.length); 83 for (String dependentBean : dependentBeans) { 84 if (!removeSingletonIfCreatedForTypeCheckOnly(dependentBean)) { 85 actualDependentBeans.add(dependentBean); 86 } 87 } 88 if (!actualDependentBeans.isEmpty()) { 89 throw new BeanCurrentlyInCreationException(beanName, 90 "Bean with name '" + beanName + "' has been injected into other beans [" + 91 StringUtils.collectionToCommaDelimitedString(actualDependentBeans) + 92 "] in its raw version as part of a circular reference, but has eventually been " + 93 "wrapped. This means that said other beans do not use the final version of the " + 94 "bean. This is often the result of over-eager type matching - consider using " + 95 "'getBeanNamesOfType' with the 'allowEagerInit' flag turned off, for example."); 96 } 97 } 98 } 99 } 100 101 // Register bean as disposable. 102 try { 103 // 注册bean 104 registerDisposableBeanIfNecessary(beanName, bean, mbd); 105 } catch (BeanDefinitionValidationException ex) { 106 throw new BeanCreationException( 107 mbd.getResourceDescription(), beanName, "Invalid destruction signature", ex); 108 } 109 110 return exposedObject; 111 }
分析:
- 首先判断是否有后置处理器,如果存在在先执行后置处理器(applyMergedBeanDefinitionPostProcessors)。
- 接下来是为处理循环依赖做前期准备,这部分后面会单独进行分析。
- 开始初始化bean对象,首先对bean进行填充,然后进行初始化。
- 处理循环依赖。
- 注册bean对象。
AbstractAutowireCapableBeanFactory#populateBean
1 protected void populateBean(String beanName, RootBeanDefinition mbd, @Nullable BeanWrapper bw) { 2 // 没有实例化对象 3 if (bw == null) { 4 // 有属性,则抛出异常 5 if (mbd.hasPropertyValues()) { 6 throw new BeanCreationException( 7 mbd.getResourceDescription(), beanName, "Cannot apply property values to null instance"); 8 } else { 9 // 没有属性,则直接返回 10 // Skip property population phase for null instance. 11 return; 12 } 13 } 14 15 // Give any InstantiationAwareBeanPostProcessors the opportunity to modify the 16 // state of the bean before properties are set. This can be used, for example, 17 // to support styles of field injection. 18 // 在设置属性之前给InstantiationAwareBeanPostProcessors最后一次改变bean的机会 19 boolean continueWithPropertyPopulation = true; 20 21 // bean不是合成的,即未由应用程序本身定义 22 // 是否持有InstantiationAwareBeanPostProcessors 23 if (!mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) { 24 // 迭代所有的BeanPostProcessor 25 for (BeanPostProcessor bp : getBeanPostProcessors()) { 26 // 如果为InstantiationAwareBeanPostProcessors 27 if (bp instanceof InstantiationAwareBeanPostProcessor) { 28 InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp; 29 // 返回值为是否继续填充bean 30 // postProcessAfterInstantiation:如果应该在bean上面设置属性,则返回true,否则返回false 31 // 一般情况下,应该返回true 32 // 返回false的话,将会阻止此bean实例上调用任何后续的InstantiationAwareBeanPostProcessors实例 33 if (!ibp.postProcessAfterInstantiation(bw.getWrappedInstance(), beanName)) { 34 continueWithPropertyPopulation = false; 35 break; 36 } 37 } 38 } 39 } 40 41 // 如果后续处理器发出停止填充命令,则终止后续操作 42 if (!continueWithPropertyPopulation) { 43 return; 44 } 45 46 // bean的属性值 47 PropertyValues pvs = (mbd.hasPropertyValues() ? mbd.getPropertyValues() : null); 48 49 // 自动注入 50 if (mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_BY_NAME || 51 mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_BY_TYPE) { 52 // 将PropertyValues封装成MutablePropertyValues对象 53 // MutablePropertyValues对象对属性进行简单的操作,并提供构造函数以支持Map的深度复制和构造 54 MutablePropertyValues newPvs = new MutablePropertyValues(pvs); 55 56 // 根据属性名称自动注入 57 // Add property values based on autowire by name if applicable. 58 if (mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_BY_NAME) { 59 autowireByName(beanName, mbd, bw, newPvs); 60 } 61 62 // 根据属性类型自动注入 63 // Add property values based on autowire by type if applicable. 64 if (mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_BY_TYPE) { 65 autowireByType(beanName, mbd, bw, newPvs); 66 } 67 68 pvs = newPvs; 69 } 70 71 // 是否已经注册了InstantiationAwareBeanPostProcessors 72 boolean hasInstAwareBpps = hasInstantiationAwareBeanPostProcessors(); 73 // 是否需要进行依赖检查 74 boolean needsDepCheck = (mbd.getDependencyCheck() != RootBeanDefinition.DEPENDENCY_CHECK_NONE); 75 76 // BeanPostProcessor处理 77 if (hasInstAwareBpps || needsDepCheck) { 78 if (pvs == null) { 79 pvs = mbd.getPropertyValues(); 80 } 81 // 从BeanWrapper中提取PropertyDescriptor结果集 82 PropertyDescriptor[] filteredPds = filterPropertyDescriptorsForDependencyCheck(bw, mbd.allowCaching); 83 if (hasInstAwareBpps) { 84 for (BeanPostProcessor bp : getBeanPostProcessors()) { 85 if (bp instanceof InstantiationAwareBeanPostProcessor) { 86 InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp; 87 pvs = ibp.postProcessPropertyValues(pvs, filteredPds, bw.getWrappedInstance(), beanName); 88 if (pvs == null) { 89 return; 90 } 91 } 92 } 93 } 94 // 依赖检查 95 if (needsDepCheck) { 96 checkDependencies(beanName, mbd, filteredPds, pvs); 97 } 98 } 99 100 // 将属性引用到bean中 101 if (pvs != null) { 102 applyPropertyValues(beanName, mbd, bw, pvs); 103 } 104 }
分析:
该函数的主要作用是将BeanDefinition中的属性值赋值给BeanWrapper对象。
- 如果BeanWrapper为null,并且有属性值,则抛出异常,否则直接返回。
- 如果存在PostProcessor,则在这里给其最后一次改变bean的机会。
- 如果在后置处理器组织bean实例继续初始化,则直接返回。
- 获取bean的PropertyValues属性,根据不同情况进行属性注入:根据属性名称或根据属性类型。
- 再次进行后置处理,这里主要对属性值进行操作。
- 进行依赖检查。
- 调用applyPropertyValues方法将属性填充到BeanWrapper中。
自动注入
Spring会根据注入类型(byName/byType)的不同,调用不同的方法来注入属性值。
AbstractAutowireCapableBeanFactory#autowireByName
1 protected void autowireByName( 2 String beanName, AbstractBeanDefinition mbd, BeanWrapper bw, MutablePropertyValues pvs) { 3 // 获取bean对象中的非简单属性 4 // 非简单属性:类型为对象类型的属性,但这里并不是将所有的对象类型都会找到,比如8个原始类型,String类型、Number类型、Date类型等会被忽略 5 String[] propertyNames = unsatisfiedNonSimpleProperties(mbd, bw); 6 // 遍历propertyName数组 7 for (String propertyName : propertyNames) { 8 // 如果容器中包含指定名称的bean,则将该bean注入到bean中 9 if (containsBean(propertyName)) { 10 // 初始化相关bean 11 Object bean = getBean(propertyName); 12 // 为指定名称的属性赋值 13 pvs.add(propertyName, bean); 14 // 属性依赖注入 15 registerDependentBean(propertyName, beanName); 16 if (logger.isDebugEnabled()) { 17 logger.debug("Added autowiring by name from bean name '" + beanName + 18 "' via property '" + propertyName + "' to bean named '" + propertyName + "'"); 19 } 20 } else { 21 if (logger.isTraceEnabled()) { 22 logger.trace("Not autowiring property '" + propertyName + "' of bean '" + beanName + 23 "' by name: no matching bean found"); 24 } 25 } 26 } 27 }
分析:
该函数为根据属性名称完成自动依赖注入。
- 首先获取bean对象的非简单属性。非简单属性:类型为对象类型的属性,但并不是将所有的对象类型都会找到,比如8个原始类型:String、Number等类型都会被忽略。
- 循环遍历属性名,然后为指定名称的属性赋值。
- 然后进行属性依赖注入。
AbstractAutowireCapableBeanFactory#unsatisfiedNonSimpleProperties
1 protected String[] unsatisfiedNonSimpleProperties(AbstractBeanDefinition mbd, BeanWrapper bw) { 2 // 创建结果集 3 Set<String> result = new TreeSet<>(); 4 PropertyValues pvs = mbd.getPropertyValues(); 5 // 遍历PropertyDescriptor数组 6 PropertyDescriptor[] pds = bw.getPropertyDescriptors(); 7 for (PropertyDescriptor pd : pds) { 8 if (pd.getWriteMethod() != null // 有可写方法 9 && !isExcludedFromDependencyCheck(pd) // 依赖检测中没有被忽略 10 && !pvs.contains(pd.getName()) // pvs中不包含该属性名 11 && !BeanUtils.isSimpleProperty(pd.getPropertyType())) { // 不是简单属性类型 12 result.add(pd.getName()); // 添加到result集合中 13 } 14 } 15 return StringUtils.toStringArray(result); 16 }
分析:
过滤条件:有可写方法、依赖检测中没有被忽略、pvs中不包含该属性名、不为简单类型。
过滤结果:获取需要依赖注入的属性。
BeanUtils#isSimpleValueType
1 public static boolean isSimpleValueType(Class<?> clazz) { 2 return (ClassUtils.isPrimitiveOrWrapper(clazz) || 3 Enum.class.isAssignableFrom(clazz) || 4 CharSequence.class.isAssignableFrom(clazz) || 5 Number.class.isAssignableFrom(clazz) || 6 Date.class.isAssignableFrom(clazz) || 7 URI.class == clazz || URL.class == clazz || 8 Locale.class == clazz || Class.class == clazz); 9 }
分析:
该方法就是获取简单类型的对象。
MutablePropertyValues#add
1 /** 2 * 存储属性对象 PropertyValue:name->value 3 */ 4 private final List<PropertyValue> propertyValueList; 5 6 public MutablePropertyValues add(String propertyName, @Nullable Object propertyValue) { 7 addPropertyValue(new PropertyValue(propertyName, propertyValue)); 8 return this; 9 } 10 11 public MutablePropertyValues addPropertyValue(PropertyValue pv) { 12 for (int i = 0; i < this.propertyValueList.size(); i++) { 13 PropertyValue currentPv = this.propertyValueList.get(i); 14 if (currentPv.getName().equals(pv.getName())) { 15 pv = mergeIfRequired(pv, currentPv); 16 setPropertyValueAt(pv, i); 17 return this; 18 } 19 } 20 this.propertyValueList.add(pv); 21 return this; 22 }
分析:
属性注入逻辑简单,就是通过propertyName进行对比,从这里也可以发现,如果存在相同的属性名会发生覆盖的情况,以最后的属性为主。
AbstractAutowireCapableBeanFactory#autowireByType
1 protected void autowireByType( 2 String beanName, AbstractBeanDefinition mbd, BeanWrapper bw, MutablePropertyValues pvs) { 3 4 // 获取TypeConverter实例 5 // 使用自定义的TypeConverter,用于取代默认的PropertyEditor机制 6 TypeConverter converter = getCustomTypeConverter(); 7 if (converter == null) { 8 converter = bw; 9 } 10 11 Set<String> autowiredBeanNames = new LinkedHashSet<>(4); 12 // 获取非简单属性 13 String[] propertyNames = unsatisfiedNonSimpleProperties(mbd, bw); 14 for (String propertyName : propertyNames) { 15 try { 16 // 获取PropertyDescriptor实例 17 PropertyDescriptor pd = bw.getPropertyDescriptor(propertyName); 18 // Don't try autowiring by type for type Object: never makes sense, 19 // even if it technically is a unsatisfied, non-simple property. 20 // 不要尝试按类型 21 if (Object.class != pd.getPropertyType()) { 22 // 探测指定属性的set方法 23 MethodParameter methodParam = BeanUtils.getWriteMethodParameter(pd); 24 // Do not allow eager init for type matching in case of a prioritized post-processor. 25 boolean eager = !PriorityOrdered.class.isInstance(bw.getWrappedInstance()); 26 DependencyDescriptor desc = new AutowireByTypeDependencyDescriptor(methodParam, eager); 27 // 解析指定beanName的属性所匹配的值,并把解析到的属性名称存储在autowiredBeanNames中 28 29 // 当属性存在封装的bean时,会找到所有匹配的bean并将其注入 30 Object autowiredArgument = resolveDependency(desc, beanName, autowiredBeanNames, converter); 31 if (autowiredArgument != null) { 32 pvs.add(propertyName, autowiredArgument); 33 } 34 // 遍历autowiredBeanNames数组 35 for (String autowiredBeanName : autowiredBeanNames) { 36 // 属性依赖注入 37 registerDependentBean(autowiredBeanName, beanName); 38 if (logger.isDebugEnabled()) { 39 logger.debug("Autowiring by type from bean name '" + beanName + "' via property '" + 40 propertyName + "' to bean named '" + autowiredBeanName + "'"); 41 } 42 } 43 // 清空autowiredBeanNames数组 44 autowiredBeanNames.clear(); 45 } 46 } catch (BeansException ex) { 47 throw new UnsatisfiedDependencyException(mbd.getResourceDescription(), beanName, propertyName, ex); 48 } 49 } 50 }
分析:
该方法根据属性类型完成自动注入依赖。
整体流程与根据属性名称自动注入差不多:
- 获取非简单属性名集合,开始遍历,过滤属于对象型的属性。
- 通过resolveDependency解析属性,找出正确的匹配(核心函数)。
- 属性注入,并进行依赖处理。
DefaultListableBeanFactory#resolveDependency
1 public Object resolveDependency(DependencyDescriptor descriptor, @Nullable String requestingBeanName, 2 @Nullable Set<String> autowiredBeanNames, @Nullable TypeConverter typeConverter) throws BeansException { 3 4 // 初始化参数名称发现器,该方法并不会在这个时候尝试检索参数名称 5 // getParameterNameDiscoverer返回ParameterNameDiscoverer,ParameterNameDiscoverer为方法参数名称解析器 6 descriptor.initParameterNameDiscovery(getParameterNameDiscoverer()); 7 // 依赖类型为Optional类型 8 if (Optional.class == descriptor.getDependencyType()) { 9 return createOptionalDependency(descriptor, requestingBeanName); 10 // 依赖类型为ObjectFactory、ObjectProvider 11 } else if (ObjectFactory.class == descriptor.getDependencyType() || 12 ObjectProvider.class == descriptor.getDependencyType()) { 13 return new DependencyObjectProvider(descriptor, requestingBeanName); 14 // javaxInjectProviderClass类注入的特殊处理 15 } else if (javaxInjectProviderClass == descriptor.getDependencyType()) { 16 return new Jsr330Factory().createDependencyProvider(descriptor, requestingBeanName); 17 } else { 18 // 为实际依赖关系目标的延迟解析构建代理 19 // 默认实现返回null 20 Object result = getAutowireCandidateResolver().getLazyResolutionProxyIfNecessary( 21 descriptor, requestingBeanName); 22 if (result == null) { 23 // 通用处理逻辑 24 result = doResolveDependency(descriptor, requestingBeanName, autowiredBeanNames, typeConverter); 25 } 26 return result; 27 } 28 }
分析:
这里关注通用处理doResolveDependency:
1 // DefaultListableBeanFactory 2 public Object doResolveDependency(DependencyDescriptor descriptor, @Nullable String beanName, 3 @Nullable Set<String> autowiredBeanNames, @Nullable TypeConverter typeConverter) throws BeansException { 4 5 // 注入点 6 InjectionPoint previousInjectionPoint = ConstructorResolver.setCurrentInjectionPoint(descriptor); 7 try { 8 // 针对给定的工厂给定一个快捷的实现方式,例如考虑一些预先解析的信息 9 // 在进入所有bean常规类型匹配算法之前,解析算法将首次尝试通过此方法解析快捷方式 10 // 子类可以覆盖此方法 11 Object shortcut = descriptor.resolveShortcut(this); 12 if (shortcut != null) { 13 // 返回快捷的解析信息 14 return shortcut; 15 } 16 17 // 依赖的类型 18 Class<?> type = descriptor.getDependencyType(); 19 // 支持Spring的注解@Value 20 Object value = getAutowireCandidateResolver().getSuggestedValue(descriptor); 21 if (value != null) { 22 if (value instanceof String) { 23 String strVal = resolveEmbeddedValue((String) value); 24 BeanDefinition bd = (beanName != null && containsBean(beanName) ? 25 getMergedBeanDefinition(beanName) : null); 26 value = evaluateBeanDefinitionString(strVal, bd); 27 } 28 TypeConverter converter = (typeConverter != null ? typeConverter : getTypeConverter()); 29 try { 30 return converter.convertIfNecessary(value, type, descriptor.getTypeDescriptor()); 31 } catch (UnsupportedOperationException ex) { 32 // A custom TypeConverter which does not support TypeDescriptor resolution... 33 return (descriptor.getField() != null ? 34 converter.convertIfNecessary(value, type, descriptor.getField()) : 35 converter.convertIfNecessary(value, type, descriptor.getMethodParameter())); 36 } 37 } 38 39 // 解析复合bean,其实就是对bean的属性进行解析 40 // 包括:数组、Collection、Map类型 41 Object multipleBeans = resolveMultipleBeans(descriptor, beanName, autowiredBeanNames, typeConverter); 42 if (multipleBeans != null) { 43 return multipleBeans; 44 } 45 46 // 查找与类型相匹配的bean 47 // 返回值构成:key=匹配的beanName,value=beanName对应的实例化bean 48 Map<String, Object> matchingBeans = findAutowireCandidates(beanName, type, descriptor); 49 // 没有找到,检验@autowire的require是否为true 50 if (matchingBeans.isEmpty()) { 51 // 如果@autowire的require属性为true,但是没有找到相应的匹配项,则抛出异常 52 if (isRequired(descriptor)) { 53 raiseNoMatchingBeanFound(type, descriptor.getResolvableType(), descriptor); 54 } 55 return null; 56 } 57 58 String autowiredBeanName; 59 Object instanceCandidate; 60 61 if (matchingBeans.size() > 1) { 62 // 确定给定bean autowire的候选者 63 // 按照@Primary和@priority的顺序 64 autowiredBeanName = determineAutowireCandidate(matchingBeans, descriptor); 65 if (autowiredBeanName == null) { 66 if (isRequired(descriptor) || !indicatesMultipleBeans(type)) { 67 // 唯一性处理 68 return descriptor.resolveNotUnique(descriptor.getResolvableType(), matchingBeans); 69 } else { 70 // In case of an optional Collection/Map, silently ignore a non-unique case: 71 // possibly it was meant to be an empty collection of multiple regular beans 72 // (before 4.3 in particular when we didn't even look for collection beans). 73 return null; 74 } 75 } 76 instanceCandidate = matchingBeans.get(autowiredBeanName); 77 } else { 78 // We have exactly one match. 79 Map.Entry<String, Object> entry = matchingBeans.entrySet().iterator().next(); 80 autowiredBeanName = entry.getKey(); 81 instanceCandidate = entry.getValue(); 82 } 83 84 if (autowiredBeanNames != null) { 85 autowiredBeanNames.add(autowiredBeanName); 86 } 87 if (instanceCandidate instanceof Class) { 88 instanceCandidate = descriptor.resolveCandidate(autowiredBeanName, type, this); 89 } 90 Object result = instanceCandidate; 91 if (result instanceof NullBean) { 92 if (isRequired(descriptor)) { 93 raiseNoMatchingBeanFound(type, descriptor.getResolvableType(), descriptor); 94 } 95 result = null; 96 } 97 if (!ClassUtils.isAssignableValue(type, result)) { 98 throw new BeanNotOfRequiredTypeException(autowiredBeanName, type, instanceCandidate.getClass()); 99 } 100 return result; 101 } finally { 102 ConstructorResolver.setCurrentInjectionPoint(previousInjectionPoint); 103 } 104 }
分析:
- 首先尝试从工厂的快捷实现方式进行解析,如果存在,则直接返回。
- 支持@Value注解的解析。
- 解析复合Bean,数组、Map类型的属性。
- 然后找出类型相匹配的属性。
整体逻辑其实不复杂,可debug进行查看。
到此属性注入已分析得差不多了,接下来是对BeanPostProcessor的处理,后面再分析。这里先看applyPropertyValues函数,将属性注入到bean中。
AbstractAutowireCapableBeanFactory#applyPropertyValues
1 protected void applyPropertyValues(String beanName, BeanDefinition mbd, BeanWrapper bw, PropertyValues pvs) { 2 if (pvs.isEmpty()) { 3 return; 4 } 5 // 设置BeanWrapperImpl的SecurityContext属性 6 if (System.getSecurityManager() != null && bw instanceof BeanWrapperImpl) { 7 ((BeanWrapperImpl) bw).setSecurityContext(getAccessControlContext()); 8 } 9 // MutablePropertyValues类型属性 10 MutablePropertyValues mpvs = null; 11 // 原始类型 12 List<PropertyValue> original; 13 14 // 获得original 15 if (pvs instanceof MutablePropertyValues) { 16 mpvs = (MutablePropertyValues) pvs; 17 // 属性已转换 18 if (mpvs.isConverted()) { 19 // Shortcut: use the pre-converted values as-is. 20 try { 21 // 为实例化对象设置属性值,依赖注入真正的实现,就在这个地方 22 bw.setPropertyValues(mpvs); 23 return; 24 } catch (BeansException ex) { 25 throw new BeanCreationException( 26 mbd.getResourceDescription(), beanName, "Error setting property values", ex); 27 } 28 } 29 original = mpvs.getPropertyValueList(); 30 } else { 31 // 如果pvs不是MutablePropertyValues类型,则直接使用原始类型 32 original = Arrays.asList(pvs.getPropertyValues()); 33 } 34 35 // 获得TypeConverter=获取用户自定义的类型转换 36 TypeConverter converter = getCustomTypeConverter(); 37 if (converter == null) { 38 converter = bw; 39 } 40 // 获取对应的解析器 41 BeanDefinitionValueResolver valueResolver = new BeanDefinitionValueResolver(this, beanName, mbd, converter); 42 43 // Create a deep copy, resolving any references for values. 44 List<PropertyValue> deepCopy = new ArrayList<>(original.size()); 45 boolean resolveNecessary = false; 46 // 遍历属性,将属性转换为对应类的对应属性的类型 47 for (PropertyValue pv : original) { 48 // 属性值不需要转换 49 if (pv.isConverted()) { 50 deepCopy.add(pv); 51 // 属性值需要转换 52 } else { 53 String propertyName = pv.getName(); 54 // 原始的属性值,即转换之前的属性值 55 Object originalValue = pv.getValue(); 56 Object resolvedValue = valueResolver.resolveValueIfNecessary(pv, originalValue); 57 // 转换属性值,例如将引用转换为IoC容器中实例化对象引用 对属性值的解析 58 Object convertedValue = resolvedValue; // 转换之后的属性值 59 boolean convertible = bw.isWritableProperty(propertyName) && 60 !PropertyAccessorUtils.isNestedOrIndexedProperty(propertyName); // 属性值是否可以转换 61 // 使用用户自定义的类型转换器转换属性值 62 if (convertible) { 63 convertedValue = convertForProperty(resolvedValue, propertyName, bw, converter); 64 } 65 // Possibly store converted value in merged bean definition, 66 // in order to avoid re-conversion for every created bean instance. 67 // 存储转换后的属性值,避免每次属性注入时进行转换 68 if (resolvedValue == originalValue) { 69 if (convertible) { 70 // 设置属性转换后的值 71 pv.setConvertedValue(convertedValue); 72 } 73 deepCopy.add(pv); 74 // 属性是可转换的,且属性原始值是字符串类型,且属性的原始类型值不是动态生成的字符串,且属性的原始值不是集合或者数组类型 75 } else if (convertible && originalValue instanceof TypedStringValue && 76 !((TypedStringValue) originalValue).isDynamic() && 77 !(convertedValue instanceof Collection || ObjectUtils.isArray(convertedValue))) { 78 pv.setConvertedValue(convertedValue); 79 deepCopy.add(pv); 80 } else { 81 resolveNecessary = true; 82 // 重新封装属性的值 83 deepCopy.add(new PropertyValue(pv, convertedValue)); 84 } 85 } 86 } 87 // 标记属性值已经转换过 88 if (mpvs != null && !resolveNecessary) { 89 mpvs.setConverted(); 90 } 91 92 // Set our (possibly massaged) deep copy. 93 // 进行属性依赖注入,依赖注入的核心思想在此 94 try { 95 bw.setPropertyValues(new MutablePropertyValues(deepCopy)); 96 } catch (BeansException ex) { 97 throw new BeanCreationException( 98 mbd.getResourceDescription(), beanName, "Error setting property values", ex); 99 } 100 }
分析:
上面的属性注入只是完成了属性的获取,将获取的属性封装在PropertyValues对象中,并没有应用到已经实例化的bean上,而applyPropertyValues方法就是完成这一操作的。
该函数主要判断属性是否需要进行转换,如果不需要,则直接进行注入即可,如果需要,则需要进行相应解析,然后进行属性注入,主要关注setProperty和resolveValueIfNecessary函数(后面再进行分析)。
bean初始化
AbstractAutowireCapableBeanFactory#initializeBean
1 protected Object initializeBean(final String beanName, final Object bean, @Nullable RootBeanDefinition mbd) { 2 // 安全模式 3 if (System.getSecurityManager() != null) { 4 AccessController.doPrivileged((PrivilegedAction<Object>) () -> { 5 // 激活Aware方法,对特殊bean处理:Aware、BeanClassLoaderAware、BeanFactoryAware 6 invokeAwareMethods(beanName, bean); 7 return null; 8 }, getAccessControlContext()); 9 } else { 10 // 非安全模式下激活Aware方法,对特殊bean处理:Aware、BeanClassLoaderAware、BeanFactoryAware 11 invokeAwareMethods(beanName, bean); 12 } 13 14 // 后置处理器 before 15 Object wrappedBean = bean; 16 if (mbd == null || !mbd.isSynthetic()) { 17 wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName); 18 } 19 20 // 处理初始化方法 21 try { 22 invokeInitMethods(beanName, wrappedBean, mbd); 23 } catch (Throwable ex) { 24 throw new BeanCreationException( 25 (mbd != null ? mbd.getResourceDescription() : null), 26 beanName, "Invocation of init method failed", ex); 27 } 28 // 后置处理器 after 29 if (mbd == null || !mbd.isSynthetic()) { 30 wrappedBean = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName); 31 } 32 33 return wrappedBean; 34 }
分析:
初始化Bean分为三大步骤:
- 激活Aware方法。
- 后置处理器应用(before/after)。
- 激活自定义的init方法。
激活Aware方法
1 // AbstractAutowireCapableBeanFactory 2 private void invokeAwareMethods(final String beanName, final Object bean) { 3 if (bean instanceof Aware) { 4 // BeanNameAware 5 if (bean instanceof BeanNameAware) { 6 ((BeanNameAware) bean).setBeanName(beanName); 7 } 8 // BeanClassLoaderAware 9 if (bean instanceof BeanClassLoaderAware) { 10 ClassLoader bcl = getBeanClassLoader(); 11 if (bcl != null) { 12 ((BeanClassLoaderAware) bean).setBeanClassLoader(bcl); 13 } 14 } 15 // BeanFactoryAware 16 if (bean instanceof BeanFactoryAware) { 17 ((BeanFactoryAware) bean).setBeanFactory(AbstractAutowireCapableBeanFactory.this); 18 } 19 } 20 }
分析:
这里主要处理BeanNameAware、BeanClassLoaderAware、BeanFactoryAware,这里是对Aware接口的处理,后续再进行查漏补缺。
后置处理器应用
BeanPostProcessor 的作用是:如果我们想要在 Spring 容器完成 Bean 的实例化,配置和其他的初始化后添加一些自己的逻辑处理,那么就使用该接口,这个接口给与了用户充足的权限去更改或者扩展 Spring,是我们对 Spring 进行扩展和增强处理一个必不可少的接口。
1 // AbatractAutowireCapableBeanFactory 2 public Object applyBeanPostProcessorsBeforeInitialization(Object existingBean, String beanName) 3 throws BeansException { 4 5 Object result = existingBean; 6 // 遍历BeanPostProcessor数组 7 for (BeanPostProcessor beanProcessor : getBeanPostProcessors()) { 8 // 处理 9 Object current = beanProcessor.postProcessBeforeInitialization(result, beanName); 10 if (current == null) { 11 return result; 12 } 13 // 修改result 14 result = current; 15 } 16 return result; 17 } 18 19 public Object applyBeanPostProcessorsAfterInitialization(Object existingBean, String beanName) 20 throws BeansException { 21 22 Object result = existingBean; 23 // 遍历BeanPostProcessor 24 for (BeanPostProcessor beanProcessor : getBeanPostProcessors()) { 25 // 进行处理 26 // TODO: 2019/4/2 具体处理过程需详细查看,这里先走大流程 27 Object current = beanProcessor.postProcessAfterInitialization(result, beanName); 28 // 返回为空,则返回传入的Object对象 29 if (current == null) { 30 return result; 31 } 32 // 修改result 33 result = current; 34 } 35 return result; 36 }
分析:
其实这里就是获取自定义的BeanPostProcessor,然后调用其postProcessBeforeInitialization和postProcessAfterInitialization方法进行自定义的业务处理。
激活自定义的init方法
1 // AbstractAutowireCapableBeanFactory 2 protected void invokeInitMethods(String beanName, final Object bean, @Nullable RootBeanDefinition mbd) 3 throws Throwable { 4 5 // 首先先检查是否是InitializingBean,如果是,则需要调用afterPropertiesSet() 6 boolean isInitializingBean = (bean instanceof InitializingBean); 7 if (isInitializingBean && (mbd == null || !mbd.isExternallyManagedInitMethod("afterPropertiesSet"))) { 8 if (logger.isDebugEnabled()) { 9 logger.debug("Invoking afterPropertiesSet() on bean with name '" + beanName + "'"); 10 } 11 // 安全模式 12 if (System.getSecurityManager() != null) { 13 try { 14 AccessController.doPrivileged((PrivilegedExceptionAction<Object>) () -> { 15 ((InitializingBean) bean).afterPropertiesSet(); 16 return null; 17 }, getAccessControlContext()); 18 } catch (PrivilegedActionException pae) { 19 throw pae.getException(); 20 } 21 } else { 22 // 属性初始化处理 23 ((InitializingBean) bean).afterPropertiesSet(); 24 } 25 } 26 27 if (mbd != null && bean.getClass() != NullBean.class) { 28 String initMethodName = mbd.getInitMethodName(); 29 if (StringUtils.hasLength(initMethodName) && 30 !(isInitializingBean && "afterPropertiesSet".equals(initMethodName)) && 31 !mbd.isExternallyManagedInitMethod(initMethodName)) { 32 // 激活用户自定义的初始化方法 33 invokeCustomInitMethod(beanName, bean, mbd); 34 } 35 } 36 }
分析:
在bean标签中有一个init-method的属性,该方法的执行就是在这里。
首先检查bean是否为InitializingBean,如果是,则需要执行afterPropertiesSet方法,因为除了可以使用init-method来自定义初始化方法外,还可以实现InitializingBean接口,该接口只有一个afterPropertiesSet方法。
两者执行顺序是:先afterPropertiesSet方法,然后是init-method方法。
至此,初始化bean的过程除了循环依赖外,其他已经大致分析完成。
总结
本篇主要分析了创建bean实例的剩余流程,主要关注populateBean和initializeBean方法。
by Shawn Chen,2019.04.28,中午。