spring IoC autowiring(自动依赖装配)的实现

Posted 持续在更新的博客

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring IoC autowiring(自动依赖装配)的实现相关的知识,希望对你有一定的参考价值。

  在前面对IoC容器实现原理的分析中,一直是通过BeanDefinition的属性值和构造函数以显式的方式对Bean

的依赖关系进行管理的。在Spring中,相对这种显式的依赖管理方式,IoC容器还提供了自动依赖装配的方式,

为应用使用容器提供更大的方便。在自动装配中,不需要对Bean属性做显式的依赖关系声明,只需要配置好

autowiring属性,IoC容器会根据这个属性的配置,使用反射自动查找属性的类型或者名字,然后基于属性的

类型或名字来自动匹配IoC容器中的Bean,从而自动的完成依赖注入。

  在AbstractAutowireCapableBeanFactory.populateBean(String, RootBeanDefinition, BeanWrapper)方法中

 1     protected void populateBean(String beanName, RootBeanDefinition mbd, BeanWrapper bw) {
 2         PropertyValues pvs = mbd.getPropertyValues();
 3 
 4         if (bw == null) {
 5             if (!pvs.isEmpty()) {
 6                 throw new BeanCreationException(
 7                         mbd.getResourceDescription(), beanName, "Cannot apply property values to null instance");
 8             }
 9             else {
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         boolean continueWithPropertyPopulation = true;
19 
20         if (!mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) {
21             for (BeanPostProcessor bp : getBeanPostProcessors()) {
22                 if (bp instanceof InstantiationAwareBeanPostProcessor) {
23                     InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp;
24                     if (!ibp.postProcessAfterInstantiation(bw.getWrappedInstance(), beanName)) {
25                         continueWithPropertyPopulation = false;
26                         break;
27                     }
28                 }
29             }
30         }
31 
32         if (!continueWithPropertyPopulation) {
33             return;
34         }
35 
36         if (mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_BY_NAME ||
37                 mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_BY_TYPE) {
38             MutablePropertyValues newPvs = new MutablePropertyValues(pvs);
39 
40             // Add property values based on autowire by name if applicable.
41             if (mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_BY_NAME) {
42                 autowireByName(beanName, mbd, bw, newPvs);
43             }
44 
45             // Add property values based on autowire by type if applicable.
46             if (mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_BY_TYPE) {
47                 autowireByType(beanName, mbd, bw, newPvs);
48             }
49 
50             pvs = newPvs;
51         }
52 
53         boolean hasInstAwareBpps = hasInstantiationAwareBeanPostProcessors();
54         boolean needsDepCheck = (mbd.getDependencyCheck() != RootBeanDefinition.DEPENDENCY_CHECK_NONE);
55 
56         if (hasInstAwareBpps || needsDepCheck) {
57             PropertyDescriptor[] filteredPds = filterPropertyDescriptorsForDependencyCheck(bw, mbd.allowCaching);
58             if (hasInstAwareBpps) {
59                 for (BeanPostProcessor bp : getBeanPostProcessors()) {
60                     if (bp instanceof InstantiationAwareBeanPostProcessor) {
61                         InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp;
62                         pvs = ibp.postProcessPropertyValues(pvs, filteredPds, bw.getWrappedInstance(), beanName);
63                         if (pvs == null) {
64                             return;
65                         }
66                     }
67                 }
68             }
69             if (needsDepCheck) {
70                 checkDependencies(beanName, mbd, filteredPds, pvs);
71             }
72         }
73 
74         applyPropertyValues(beanName, mbd, bw, pvs);
75     }

如果当前的Bean配置了autowire_by_name和autowire_by_type属性,那么调用相应的autowireByName和

autowireByType方法。

以上是关于spring IoC autowiring(自动依赖装配)的实现的主要内容,如果未能解决你的问题,请参考以下文章

spring之IOC

Spring IOC设计原理解析

Spring依赖注入 DI

Spring自动装配Beans

Spring处理自动装配歧义

Spring 依赖注入