自言自语Spring依赖注入(XML配置)

首先说一点,因为Spring中的对象默认为单例,想要获取它默认init_lazy默认为false。

下面的图是整个流程的流程图,下面跟的源码解析就是按照这个流程来的。

 

至于基于XML依赖注入的过程,首先要找一个比较合适的入口,那就是getBean。那么具体是怎么实现的呢?首先写个测试方法:

ClassPathXmlApplicationContext app = new ClassPathXmlApplicationContext("application-common.xml","application-beans.xml","application_jdbc.xml");
Object obj = app.getBean("member");

第一句在我上篇文章---Spring-BeanFactory基本工作流程中就用到了,主要是作为一个初始化IOC容器的入口,那么我们现在的场景是:IOC容器已经创建好,我们在XML文件中配置的信息已经加载到BeanDefinition中了,那么我们现在的目的就是去获得它。第二句就是我们本篇文章的入口:getBean。

@Override
    public Object getBean(String name) throws BeansException {
        assertBeanFactoryActive();
        return getBeanFactory().getBean(name);
    }

首先执行的是assertBeanFactoryActive方法,本人在上一篇分析Spring源码的时候就已经赞誉过Spring方法的命名,我们可以通过这个名字来判断它到底有什么意思,首先说明它是一个断言方法,然后判断的事BeanFactory是不是Active的,如果不是就直接抛异常了。

protected void assertBeanFactoryActive() {
        if (!this.active.get()) {
            if (this.closed.get()) {
                throw new IllegalStateException(getDisplayName() + " has been closed already");
            }
            else {
                throw new IllegalStateException(getDisplayName() + " has not been refreshed yet");
            }
        }
    }

然后我们接着看getBean方法,可以看到,它会调getBeanFactory方法,他返回的是一个BeanFactory,然后调BeanFactory中的getBean方法:

在之前的文章中提到过,Spring中做实事的方法都是以do开头的,我们可以看到,在getBean方法中调用了一个doGetBean方法,看名字可以了解到是真正拿到Bean的方法,在doGetBean方法中,首先先将我们传进来的指定的名字转化为管理Bean的名字,然后再创建一个名为bean的Object对象,作为我们要返回的实例。由于我们依赖注入的对象为单例,所以我们要做的就是首先在cache中检查有没有已经创建好的实例。(Spring中从BeanDefinition创建的Bean不是存放在IOC中,而是存放在Cache容器中,IOC只是存放Bean关系),如果有Bean存在,就直接返回,如果Cache中没有这个Bean,那么就要创建它。

在我们要自己创建Bean的时候,首先先检查这个Bean有没有相关的BeanDefinition,首先要解析出Bean的原始名称,然后现在当前BeanFactory里检查,如果没有,就去父BeanFactory里面找,如果还是找不到则沿着容器的继承体系向父级容器查找。当当前容器的父亲容器存在而且在当前容器中找不到这个bean时,就开始在父容器里找,会找父级BeanFactory的getBean方法。

如果在当前的Bean里面有,则首先向容器中标记这个Bean已经被创建了,然后根据指定Bean名称获取其父级的Bean定义,主要解决Bean继承时子类合并父类公共属性问题。接着获取该Bean所有依赖Bean的名称,如果有依赖Bean存在,那么就递归获取依赖Bean,并将依赖Bean注册给当前的Bean。

针对于Bean的类型(单例还是原型),Spring在创建Bean的过程都不一样,先看如果创建单例Bean的方法,首先看一下Spring在这是怎么处理的,它先使用一个内部匿名类,就是一个SingletonFactory类,然后将Bean实际名、Bean的BeanDefinition和Bean参数传入createBean方法(在下面会分析细节分析这个方法,这边只是大致过一下doGetBean方法)。并返回创建出的Bean实例。同样的,如果是一个原型Bean,因为每次都会建立一个新的实例,然后将获得的实例返回给之前创建的bean。如果Bean既不是单例,也不是原型的话,那么就要根据Bean定义资源中配置的生命周期范围来选择合适的实例化Bean方法(这种情况出现在web那块比较多,如session,reques等)。

最后要对创建好的Bean进行检查,如果符合规范,就认为创建好了并且返回。

//AbstractBeanFactory.class
//获取IOC容器中指定名称的Bean
    @Override
    public Object getBean(String name) throws BeansException {
        //doGetBean才是真正向IoC容器获取被管理Bean的过程
        return doGetBean(name, null, null, false);
    }
  1
  2 //真正实现向IOC容器获取Bean的功能,也是触发依赖注入功能的地方
  3     protected <T> T doGetBean(final String name, @Nullable final Class<T> requiredType,
  4             @Nullable final Object[] args, boolean typeCheckOnly) throws BeansException {
  5 
  6         //根据指定的名称获取被管理Bean的名称,剥离指定名称中对容器的相关依赖
  7         //如果指定的是别名,将别名转换为规范的Bean名称
  8         final String beanName = transformedBeanName(name);
  9         Object bean;
 10 
 11         // Eagerly check singleton cache for manually registered singletons.
 12         //先从缓存中取是否已经有被创建过的单态类型的Bean
 13         //对于单例模式的Bean整个IOC容器中只创建一次,不需要重复创建
 14         Object sharedInstance = getSingleton(beanName);
 15         //IOC容器创建单例模式Bean实例对象
 16         if (sharedInstance != null && args == null) {
 17             if (logger.isDebugEnabled()) {
 18                 //如果指定名称的Bean在容器中已有单例模式的Bean被创建
 19                 //直接返回已经创建的Bean
 20                 if (isSingletonCurrentlyInCreation(beanName)) {
 21                     logger.debug("Returning eagerly cached instance of singleton bean '" + beanName +
 22                             "' that is not fully initialized yet - a consequence of a circular reference");
 23                 }
 24                 else {
 25                     logger.debug("Returning cached instance of singleton bean '" + beanName + "'");
 26                 }
 27             }
 28             //获取给定Bean的实例对象,主要是完成FactoryBean的相关处理
 29             //注意:BeanFactory是管理容器中Bean的工厂,而FactoryBean是
 30             //创建创建对象的工厂Bean,两者之间有区别
 31             bean = getObjectForBeanInstance(sharedInstance, name, beanName, null);
 32         }
 33 
 34         else {
 35             // Fail if we're already creating this bean instance:
 36             // We're assumably within a circular reference.
 37             //缓存没有正在创建的单例模式Bean
 38             //缓存中已经有已经创建的原型模式Bean
 39             //但是由于循环引用的问题导致实例化对象失败
 40             if (isPrototypeCurrentlyInCreation(beanName)) {
 41                 throw new BeanCurrentlyInCreationException(beanName);
 42             }
 43 
 44             // Check if bean definition exists in this factory.
 45             //对IOC容器中是否存在指定名称的BeanDefinition进行检查,首先检查是否
 46             //能在当前的BeanFactory中获取的所需要的Bean,如果不能则委托当前容器
 47             //的父级容器去查找,如果还是找不到则沿着容器的继承体系向父级容器查找
 48             BeanFactory parentBeanFactory = getParentBeanFactory();
 49             //当前容器的父级容器存在,且当前容器中不存在指定名称的Bean
 50             if (parentBeanFactory != null && !containsBeanDefinition(beanName)) {
 51                 // Not found -> check parent.
 52                 //解析指定Bean名称的原始名称
 53                 String nameToLookup = originalBeanName(name);
 54                 if (parentBeanFactory instanceof AbstractBeanFactory) {
 55                     return ((AbstractBeanFactory) parentBeanFactory).doGetBean(
 56                             nameToLookup, requiredType, args, typeCheckOnly);
 57                 }
 58                 else if (args != null) {
 59                     // Delegation to parent with explicit args.
 60                     //委派父级容器根据指定名称和显式的参数查找
 61                     return (T) parentBeanFactory.getBean(nameToLookup, args);
 62                 }
 63                 else {
 64                     // No args -> delegate to standard getBean method.
 65                     //委派父级容器根据指定名称和类型查找
 66                     return parentBeanFactory.getBean(nameToLookup, requiredType);
 67                 }
 68             }
 69 
 70             //创建的Bean是否需要进行类型验证,一般不需要
 71             if (!typeCheckOnly) {
 72                 //向容器标记指定的Bean已经被创建
 73                 markBeanAsCreated(beanName);
 74             }
 75 
 76             try {
 77                 //根据指定Bean名称获取其父级的Bean定义
 78                 //主要解决Bean继承时子类合并父类公共属性问题
 79                 final RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName);
 80                 checkMergedBeanDefinition(mbd, beanName, args);
 81 
 82                 // Guarantee initialization of beans that the current bean depends on.
 83                 //获取当前Bean所有依赖Bean的名称
 84                 String[] dependsOn = mbd.getDependsOn();
 85                 //如果当前Bean有依赖Bean
 86                 if (dependsOn != null) {
 87                     for (String dep : dependsOn) {
 88                         if (isDependent(beanName, dep)) {
 89                             throw new BeanCreationException(mbd.getResourceDescription(), beanName,
 90                                     "Circular depends-on relationship between '" + beanName + "' and '" + dep + "'");
 91                         }
 92                         //递归调用getBean方法,获取当前Bean的依赖Bean
 93                         registerDependentBean(dep, beanName);
 94                         //把被依赖Bean注册给当前依赖的Bean
 95                         getBean(dep);
 96                     }
 97                 }
 98 
 99                 // Create bean instance.
100                 //创建单例模式Bean的实例对象
101                 if (mbd.isSingleton()) {
102                     //这里使用了一个匿名内部类,创建Bean实例对象,并且注册给所依赖的对象
103                     sharedInstance = getSingleton(beanName, () -> {
104                         try {
105                             //创建一个指定Bean实例对象,如果有父级继承,则合并子类和父类的定义
106                             return createBean(beanName, mbd, args);
107                         }
108                         catch (BeansException ex) {
109                             // Explicitly remove instance from singleton cache: It might have been put there
110                             // eagerly by the creation process, to allow for circular reference resolution.
111                             // Also remove any beans that received a temporary reference to the bean.
112                             //显式地从容器单例模式Bean缓存中清除实例对象
113                             destroySingleton(beanName);
114                             throw ex;
115                         }
116                     });
117                     //获取给定Bean的实例对象
118                     bean = getObjectForBeanInstance(sharedInstance, name, beanName, mbd);
119                 }
120 
121                 //IOC容器创建原型模式Bean实例对象
122                 else if (mbd.isPrototype()) {
123                     // It's a prototype -> create a new instance.
124                     //原型模式(Prototype)是每次都会创建一个新的对象
125                     Object prototypeInstance = null;
126                     try {
127                         //回调beforePrototypeCreation方法,默认的功能是注册当前创建的原型对象
128                         beforePrototypeCreation(beanName);
129                         //创建指定Bean对象实例
130                         prototypeInstance = createBean(beanName, mbd, args);
131                     }
132                     finally {
133                         //回调afterPrototypeCreation方法,默认的功能告诉IOC容器指定Bean的原型对象不再创建
134                         afterPrototypeCreation(beanName);
135                     }
136                     //获取给定Bean的实例对象
137                     bean = getObjectForBeanInstance(prototypeInstance, name, beanName, mbd);
138                 }
139 
140                 //要创建的Bean既不是单例模式,也不是原型模式,则根据Bean定义资源中
141                 //配置的生命周期范围,选择实例化Bean的合适方法,这种在Web应用程序中
142                 //比较常用,如:request、session、application等生命周期
143                 else {
144                     String scopeName = mbd.getScope();
145                     final Scope scope = this.scopes.get(scopeName);
146                     //Bean定义资源中没有配置生命周期范围,则Bean定义不合法
147                     if (scope == null) {
148                         throw new IllegalStateException("No Scope registered for scope name '" + scopeName + "'");
149                     }
150                     try {
151                         //这里又使用了一个匿名内部类,获取一个指定生命周期范围的实例
152                         Object scopedInstance = scope.get(beanName, () -> {
153                             beforePrototypeCreation(beanName);
154                             try {
155                                 return createBean(beanName, mbd, args);
156                             }
157                             finally {
158                                 afterPrototypeCreation(beanName);
159                             }
160                         });
161                         //获取给定Bean的实例对象
162                         bean = getObjectForBeanInstance(scopedInstance, name, beanName, mbd);
163                     }
164                     catch (IllegalStateException ex) {
165                         throw new BeanCreationException(beanName,
166                                 "Scope '" + scopeName + "' is not active for the current thread; consider " +
167                                 "defining a scoped proxy for this bean if you intend to refer to it from a singleton",
168                                 ex);
169                     }
170                 }
171             }
172             catch (BeansException ex) {
173                 cleanupAfterBeanCreationFailure(beanName);
174                 throw ex;
175             }
176         }
177 
178         // Check if required type matches the type of the actual bean instance.
179         //对创建的Bean实例对象进行类型检查
180         if (requiredType != null && !requiredType.isInstance(bean)) {
181             try {
182                 T convertedBean = getTypeConverter().convertIfNecessary(bean, requiredType);
183                 if (convertedBean == null) {
184                     throw new BeanNotOfRequiredTypeException(name, requiredType, bean.getClass());
185                 }
186                 return convertedBean;
187             }
188             catch (TypeMismatchException ex) {
189                 if (logger.isDebugEnabled()) {
190                     logger.debug("Failed to convert bean '" + name + "' to required type '" +
191                             ClassUtils.getQualifiedName(requiredType) + "'", ex);
192                 }
193                 throw new BeanNotOfRequiredTypeException(name, requiredType, bean.getClass());
194             }
195         }
196         return (T) bean;
197     }

上面比较详细的介绍了Bean实例是如何创建的,那么接下来主要是重点分析一下几个比较重要的方法的细节。先说一下如果Cache容器中有之前已经创建过的该Bean的实例,在31行,我们进入getObjectForBeanInstance方法。

我们已经拿到了在Cache中拿到了该Bean的FactoryBean,在这说一下FactoryBean和BeanFactory的区别,FactoryBean是用来创建生产Bean的工厂的Bean(有点绕)而BeanFactory是管理Bean的工厂。然后进入getObjectForBeanInstance方法,首先我们会去判断这个Bean是不是一个工厂Bean,如果不是工厂Bean,或者说我们想要得到的就是一个工厂,那么就直接返回它。如果是工厂Bean并且我们要得到的是一个Bean实例,那么首先看一下工厂Bean的缓存中有木有实例,如果有就返回,如果没有,就会调用getObjectFromFactoryBean方法来获得Bean实例。

    protected Object getObjectForBeanInstance(
            Object beanInstance, String name, String beanName, @Nullable RootBeanDefinition mbd) {

        // Don't let calling code try to dereference the factory if the bean isn't a factory.
        //容器已经得到了Bean实例对象,这个实例对象可能是一个普通的Bean,
        //也可能是一个工厂Bean,如果是一个工厂Bean,则使用它创建一个Bean实例对象,
        //如果调用本身就想获得一个容器的引用,则指定返回这个工厂Bean实例对象
        //如果指定的名称是容器的解引用(dereference,即是对象本身而非内存地址),
        //且Bean实例也不是创建Bean实例对象的工厂Bean
        if (BeanFactoryUtils.isFactoryDereference(name) && !(beanInstance instanceof FactoryBean)) {
            throw new BeanIsNotAFactoryException(transformedBeanName(name), beanInstance.getClass());
        }

        // Now we have the bean instance, which may be a normal bean or a FactoryBean.
        // If it's a FactoryBean, we use it to create a bean instance, unless the
        // caller actually wants a reference to the factory.
        //如果Bean实例不是工厂Bean,或者指定名称是容器的解引用,
        //调用者向获取对容器的引用,则直接返回当前的Bean实例
        if (!(beanInstance instanceof FactoryBean) || BeanFactoryUtils.isFactoryDereference(name)) {
            return beanInstance;
        }

        //处理指定名称不是容器的解引用,或者根据名称获取的Bean实例对象是一个工厂Bean
        //使用工厂Bean创建一个Bean的实例对象
        Object object = null;
        if (mbd == null) {
            //从Bean工厂缓存中获取给定名称的Bean实例对象
            object = getCachedObjectForFactoryBean(beanName);
        }
        //让Bean工厂生产给定名称的Bean对象实例
        if (object == null) {
            // Return bean instance from factory.
            FactoryBean<?> factory = (FactoryBean<?>) beanInstance;
            // Caches object obtained from FactoryBean if it is a singleton.
            //如果从Bean工厂生产的Bean是单态模式的,则缓存
            if (mbd == null && containsBeanDefinition(beanName)) {
                //从容器中获取指定名称的Bean定义,如果继承基类,则合并基类相关属性
                mbd = getMergedLocalBeanDefinition(beanName);
            }
            //如果从容器得到Bean定义信息,并且Bean定义信息不是虚构的,
            //则让工厂Bean生产Bean实例对象
            boolean synthetic = (mbd != null && mbd.isSynthetic());
            //调用FactoryBeanRegistrySupport类的getObjectFromFactoryBean方法,
            //实现工厂Bean生产Bean对象实例的过程
            object = getObjectFromFactoryBean(factory, beanName, !synthetic);
        }
        return object;
    }

接下来我们看一看是怎么从FactoryBean里拿到Bean实例的,先进入getObjectFromFactoryBean方法。

//Bean工厂生产Bean实例对象
    protected Object getObjectFromFactoryBean(FactoryBean<?> factory, String beanName, boolean shouldPostProcess) {
        //Bean工厂是单态模式,并且Bean工厂缓存中存在指定名称的Bean实例对象
        if (factory.isSingleton() && containsSingleton(beanName)) {
            //多线程同步,以防止数据不一致
            synchronized (getSingletonMutex()) {
                //直接从Bean工厂缓存中获取指定名称的Bean实例对象
                Object object = this.factoryBeanObjectCache.get(beanName);
                //Bean工厂缓存中没有指定名称的实例对象,则生产该实例对象
                if (object == null) {
                    //调用Bean工厂的getObject方法生产指定Bean的实例对象
                    object = doGetObjectFromFactoryBean(factory, beanName);
                    // Only post-process and store if not put there already during getObject() call above
                    // (e.g. because of circular reference processing triggered by custom getBean calls)
                    Object alreadyThere = this.factoryBeanObjectCache.get(beanName);
                    if (alreadyThere != null) {
                        object = alreadyThere;
                    }
                    else {
                        if (shouldPostProcess) {
                            try {
                                object = postProcessObjectFromFactoryBean(object, beanName);
                            }
                            catch (Throwable ex) {
                                throw new BeanCreationException(beanName,
                                        "Post-processing of FactoryBean's singleton object failed", ex);
                            }
                        }
                        //将生产的实例对象添加到Bean工厂缓存中
                        this.factoryBeanObjectCache.put(beanName, object);
                    }
                }
                return object;
            }
        }
        //调用Bean工厂的getObject方法生产指定Bean的实例对象
        else {
            Object object = doGetObjectFromFactoryBean(factory, beanName);
            if (shouldPostProcess) {
                try {
                    object = postProcessObjectFromFactoryBean(object, beanName);
                }
                catch (Throwable ex) {
                    throw new BeanCreationException(beanName, "Post-processing of FactoryBean's object failed", ex);
                }
            }
            return object;
        }
    }

由于我们现在创建的Bean为单例模式,所以要保证线程安全,首先先判断在FactoryBean里有没有该Bean的缓存,如果没有就自己创建,方法为doGetBeanFromFactoryBean,并且将创建好的Bean存到Cache里。那么我们到现在又看到了一个做实事的方法,看名字这个方法应该是生产Bean的方法。

//调用Bean工厂的getObject方法生产指定Bean的实例对象
    private Object doGetObjectFromFactoryBean(final FactoryBean<?> factory, final String beanName)
            throws BeanCreationException {

        Object object;
        try {
            if (System.getSecurityManager() != null) {
                AccessControlContext acc = getAccessControlContext();
                try {
                    //实现PrivilegedExceptionAction接口的匿名内置类
                    //根据JVM检查权限,然后决定BeanFactory创建实例对象
                    object = AccessController.doPrivileged((PrivilegedExceptionAction<Object>) () ->
                            factory.getObject(), acc);
                }
                catch (PrivilegedActionException pae) {
                    throw pae.getException();
                }
            }
            else {
                //调用BeanFactory接口实现类的创建对象方法
                object = factory.getObject();
            }
        }
        catch (FactoryBeanNotInitializedException ex) {
            throw new BeanCurrentlyInCreationException(beanName, ex.toString());
        }
        catch (Throwable ex) {
            throw new BeanCreationException(beanName, "FactoryBean threw exception on object creation", ex);
        }

        // Do not accept a null value for a FactoryBean that's not fully
        // initialized yet: Many FactoryBeans just return null then.
        //创建出来的实例对象为null,或者因为单态对象正在创建而返回null
        if (object == null) {
            if (isSingletonCurrentlyInCreation(beanName)) {
                throw new BeanCurrentlyInCreationException(
                        beanName, "FactoryBean which is currently in creation returned null from getObject");
            }
            object = new NullBean();
        }
        return object;
    }

通过这个方法,我们就得到了单例Bean的实例(不管它有没有在Cache里存在)。

同样,如果缓存中没有被创建的Bean,那么在之前那段代码中(doGetBean),就不会走31行那一块,而是接着往下走,到101行,执行到createBean方法,这里用到了lamda表达式,new了一个内部类objectFactory。

//AbstractAutowireCapableBeanFactory.class
//创建Bean实例对象
    @Override
    protected Object createBean(String beanName, RootBeanDefinition mbd, @Nullable Object[] args)
            throws BeanCreationException {

        if (logger.isDebugEnabled()) {
            logger.debug("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.
        //判断需要创建的Bean是否可以实例化,即是否可以通过当前的类加载器加载
        Class<?> resolvedClass = resolveBeanClass(mbd, beanName);
        if (resolvedClass != null && !mbd.hasBeanClass() && mbd.getBeanClassName() != null) {
            mbdToUse = new RootBeanDefinition(mbd);
            mbdToUse.setBeanClass(resolvedClass);
        }

        // Prepare method overrides.
        //校验和准备Bean中的方法覆盖
        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.
            //如果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 ex) {
            // A previously detected exception with proper bean creation context already...
            throw ex;
        }
        catch (ImplicitlyAppearedSingletonException ex) {
            // An IllegalStateException to be communicated up to DefaultSingletonBeanRegistry...
            throw ex;
        }
        catch (Throwable ex) {
            throw new BeanCreationException(
                    mbdToUse.getResourceDescription(), beanName, "Unexpected exception during bean creation", ex);
        }
    }

在这里,会调用doCreateBean方法

//AbstractAutowireCapableBeanFactory.class
//真正创建Bean的方法
    protected Object doCreateBean(final String beanName, final RootBeanDefinition mbd, final @Nullable Object[] args)
            throws BeanCreationException {

        // Instantiate the bean.
        //封装被创建的Bean对象
        BeanWrapper instanceWrapper = null;
        if (mbd.isSingleton()) {
            instanceWrapper = this.factoryBeanInstanceCache.remove(beanName);
        }
        if (instanceWrapper == null) {
            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.
        //调用PostProcessor后置处理器
        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.
        //向容器中缓存单例模式的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(beanName, () -> getEarlyBeanReference(beanName, mbd, bean));
        }

        // Initialize the bean instance.
        //Bean对象的初始化,依赖注入在此触发
        //这个exposedObject在初始化完成之后返回作为依赖注入完成后的Bean
        Object exposedObject = bean;
        try {
            //将Bean实例对象封装,并且Bean定义中配置的属性值赋值给实例对象
            populateBean(beanName, mbd, instanceWrapper);
            //初始化Bean对象
            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) {
            //获取指定名称的已注册的单例模式Bean对象
            Object earlySingletonReference = getSingleton(beanName, false);
            if (earlySingletonReference != null) {
                //根据名称获取的已注册的Bean和正在实例化的Bean是同一个
                if (exposedObject == bean) {
                    //当前实例化的Bean初始化完成
                    exposedObject = earlySingletonReference;
                }
                //当前Bean依赖其他Bean,并且当发生循环引用时不允许新创建实例对象
                else if (!this.allowRawInjectionDespiteWrapping && hasDependentBean(beanName)) {
                    String[] dependentBeans = getDependentBeans(beanName);
                    Set<String> actualDependentBeans = new LinkedHashSet<>(dependentBeans.length);
                    //获取当前Bean所依赖的其他Bean
                    for (String dependentBean : dependentBeans) {
                        //对依赖Bean进行类型检查
                        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 " +
                                "'getBeanNamesOfType' with the 'allowEagerInit' flag turned off, for example.");
                    }
                }
            }
        }

        // Register bean as disposable.
        //注册完成依赖注入的Bean
        try {
            registerDisposableBeanIfNecessary(beanName, bean, mbd);
        }
        catch (BeanDefinitionValidationException ex) {
            throw new BeanCreationException(
                    mbd.getResourceDescription(), beanName, "Invalid destruction signature", ex);
        }

        return exposedObject;
    }

这里首先创建一个包装类,用到了BeanWrapper来包装Bean,然后到createBeanInstance方法使得生成一个Bean的Wrapper类。并保证容器缓存中有此Bean对象的单例模式。然后调用populateBean方法,将BeanDefinition的属性赋值给实例对象,并调用initializeBean方法初始化Bean对象。并对Bean对象进行一系列的检查,然后返回生成的Bean。

首先我们先看一下createBeanInstance方法,这个方法返回的是一个BeanWrapper对象,首先确定Bean是可以实例化的,然后就对Bean进行实例化。实例化的方法有三种:(1)工厂方法 (2)容器自动装配 (3)Bean的构造方法

protected BeanWrapper createBeanInstance(String beanName, RootBeanDefinition mbd, @Nullable Object[] args) {
        // Make sure bean class is actually resolved at this point.
        //检查确认Bean是可实例化的
        Class<?> beanClass = resolveBeanClass(mbd, beanName);

        //使用工厂方法对Bean进行实例化
        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) {
                //配置了自动装配属性,使用容器的自动装配实例化
                //容器的自动装配是根据参数类型匹配Bean的构造方法
                return autowireConstructor(beanName, mbd, null, null);
            }
            else {
                //使用默认的无参构造方法实例化
                return instantiateBean(beanName, mbd);
            }
        }

        // Need to determine the constructor...
        //使用Bean的构造方法进行实例化
        Constructor<?>[] ctors = determineConstructorsFromBeanPostProcessors(beanClass, beanName);
        if (ctors != null ||
                mbd.getResolvedAutowireMode() == RootBeanDefinition.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);
    }

首先是工厂方法实例化Bean,调用的是instantiateUsingFactoryMethod方法,这边不详细分析了。

之后是使用容器自动装配来进行实例化,如果匹配了自动装配属性,那么久使用容器自动装配实例化,如果没有,则用构造方法进行实例化,这里说一下用构造方法进行实例化的过程

无参构造方法instantiateBean方法,这里就是实例Bean的方法。

//使用默认的无参构造方法实例化Bean对象
    protected BeanWrapper instantiateBean(final String beanName, final RootBeanDefinition mbd) {
        try {
            Object beanInstance;
            final BeanFactory parent = this;
            //获取系统的安全管理接口,JDK标准的安全管理API
            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);
        }
    }

首先实例对象,用到的是instantiate方法,然后再将其包装成BeanWrapper

然后到instantiate方法。

    //使用初始化策略实例化Bean对象
    @Override
    public Object instantiate(RootBeanDefinition bd, @Nullable String beanName, BeanFactory owner) {
        // Don't override the class with CGLIB if no overrides.
        //如果Bean定义中没有方法覆盖,则就不需要CGLIB父类类的方法
        if (!bd.hasMethodOverrides()) {
            Constructor<?> constructorToUse;
            synchronized (bd.constructorArgumentLock) {
                //获取对象的构造方法或工厂方法
                constructorToUse = (Constructor<?>) bd.resolvedConstructorOrFactoryMethod;
                //如果没有构造方法且没有工厂方法
                if (constructorToUse == null) {
                    //使用JDK的反射机制,判断要实例化的Bean是否是接口
                    final Class<?> clazz = bd.getBeanClass();
                    if (clazz.isInterface()) {
                        throw new BeanInstantiationException(clazz, "Specified class is an interface");
                    }
                    try {
                        if (System.getSecurityManager() != null) {
                            //这里是一个匿名内置类,使用反射机制获取Bean的构造方法
                            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);
                    }
                }
            }
            //使用BeanUtils实例化,通过反射机制调用”构造方法.newInstance(arg)”来进行实例化
            return BeanUtils.instantiateClass(constructorToUse);
        }
        else {
            // Must generate CGLIB subclass.
            //使用CGLIB来实例化对象
            return instantiateWithMethodInjection(bd, beanName, owner);
        }
    }

首先检查Bean中有没有覆盖父类方法,如果没有的话,就不用CGLIB父类类的方法,然后通过反射获得Bean的构造方法,之后通过BeanUtils使用构造方法进行实例化。如果有覆盖方法,那么就要使用CGLIB来进行实例化

然后调用instantiateWithMethodInjection方法

@Override
    protected Object instantiateWithMethodInjection(RootBeanDefinition bd, @Nullable String beanName, BeanFactory owner) {
        return instantiateWithMethodInjection(bd, beanName, owner, null);
    }
@Override
    protected Object instantiateWithMethodInjection(RootBeanDefinition bd, @Nullable String beanName, BeanFactory owner,
            @Nullable Constructor<?> ctor, @Nullable Object... args) {

        // Must generate CGLIB subclass...
        return new CglibSubclassCreator(bd, owner).instantiate(ctor, args);
    }

到这里可以看到调用使用CGLIB的instantiate方法。

public Object instantiate(@Nullable Constructor<?> ctor, @Nullable Object... args) {
            //创建代理子类
            Class<?> subclass = createEnhancedSubclass(this.beanDefinition);
            Object instance;
            if (ctor == null) {
                instance = BeanUtils.instantiateClass(subclass);
            }
            else {
                try {
                    Constructor<?> enhancedSubclassConstructor = subclass.getConstructor(ctor.getParameterTypes());
                    instance = enhancedSubclassConstructor.newInstance(args);
                }
                catch (Exception ex) {
                    throw new BeanInstantiationException(this.beanDefinition.getBeanClass(),
                            "Failed to invoke constructor for CGLIB enhanced subclass [" + subclass.getName() + "]", ex);
                }
            }
            // SPR-10785: set callbacks directly on the instance instead of in the
            // enhanced class (via the Enhancer) in order to avoid memory leaks.
            Factory factory = (Factory) instance;
            factory.setCallbacks(new Callback[] {NoOp.INSTANCE,
                    new LookupOverrideMethodInterceptor(this.beanDefinition, this.owner),
                    new ReplaceOverrideMethodInterceptor(this.beanDefinition, this.owner)});
            return instance;
        }

然后调用instantiateClass方法

public static <T> T instantiateClass(Class<T> clazz) throws BeanInstantiationException {
        Assert.notNull(clazz, "Class must not be null");
        if (clazz.isInterface()) {
            throw new BeanInstantiationException(clazz, "Specified class is an interface");
        }
        try {
            Constructor<T> ctor = (KotlinDetector.isKotlinType(clazz) ?
                    KotlinDelegate.findPrimaryConstructor(clazz) : clazz.getDeclaredConstructor());
            if (ctor == null) {
                throw new BeanInstantiationException(clazz, "No default constructor found");
            }
            return instantiateClass(ctor);
        }
        catch (NoSuchMethodException ex) {
            throw new BeanInstantiationException(clazz, "No default constructor found", ex);
        }
    }
public static <T> T instantiateClass(Constructor<T> ctor, Object... args) throws BeanInstantiationException {
        Assert.notNull(ctor, "Constructor must not be null");
        try {
            ReflectionUtils.makeAccessible(ctor);
            return (KotlinDetector.isKotlinType(ctor.getDeclaringClass()) ?
                    KotlinDelegate.instantiateClass(ctor, args) : ctor.newInstance(args));
        }
        catch (InstantiationException ex) {
            throw new BeanInstantiationException(ctor, "Is it an abstract class?", ex);
        }
        catch (IllegalAccessException ex) {
            throw new BeanInstantiationException(ctor, "Is the constructor accessible?", ex);
        }
        catch (IllegalArgumentException ex) {
            throw new BeanInstantiationException(ctor, "Illegal arguments for constructor", ex);
        }
        catch (InvocationTargetException ex) {
            throw new BeanInstantiationException(ctor, "Constructor threw exception", ex.getTargetException());
        }
    }

回到instantiateBean方法,我们得到了实例化的Bean对象,那么接下来就是将他封装起来。调用了BeanWrapperImpl的构造方法,并初始化这个BeanWrapper然后将其返回。

之后回到createBeanInstance方法,刚才介绍的是用无参构造函数方法将其进行实例化封装,接下来如果有参数,那么就会使用容器的自动装配特性,调用匹配的构造方法进行实例化。

再回到之前调用createBeanInstance方法的时候,也就是在AbstractAutowireCapableBeanFactory这个类里的doCreateBean方法中,会调用populateBean方法,这里就是真正的依赖注入。

 1 //将Bean属性设置到生成的实例对象上
 2     protected void populateBean(String beanName, RootBeanDefinition mbd, @Nullable BeanWrapper bw) {
 3         if (bw == null) {
 4             if (mbd.hasPropertyValues()) {
 5                 throw new BeanCreationException(
 6                         mbd.getResourceDescription(), beanName, "Cannot apply property values to null instance");
 7             }
 8             else {
 9                 // Skip property population phase for null instance.
10                 return;
11             }
12         }
13 
14         // Give any InstantiationAwareBeanPostProcessors the opportunity to modify the
15         // state of the bean before properties are set. This can be used, for example,
16         // to support styles of field injection.
17         boolean continueWithPropertyPopulation = true;
18 
19         if (!mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) {
20             for (BeanPostProcessor bp : getBeanPostProcessors()) {
21                 if (bp instanceof InstantiationAwareBeanPostProcessor) {
22                     InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp;
23                     if (!ibp.postProcessAfterInstantiation(bw.getWrappedInstance(), beanName)) {
24                         continueWithPropertyPopulation = false;
25                         break;
26                     }
27                 }
28             }
29         }
30 
31         if (!continueWithPropertyPopulation) {
32             return;
33         }
34         //获取容器在解析Bean定义资源时为BeanDefiniton中设置的属性值
35         PropertyValues pvs = (mbd.hasPropertyValues() ? mbd.getPropertyValues() : null);
36 
37         //对依赖注入处理,首先处理autowiring自动装配的依赖注入
38         if (mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_BY_NAME ||
39                 mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_BY_TYPE) {
40             MutablePropertyValues newPvs = new MutablePropertyValues(pvs);
41 
42             // Add property values based on autowire by name if applicable.
43             //根据Bean名称进行autowiring自动装配处理
44             if (mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_BY_NAME) {
45                 autowireByName(beanName, mbd, bw, newPvs);
46             }
47 
48             // Add property values based on autowire by type if applicable.
49             //根据Bean类型进行autowiring自动装配处理
50             if (mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_BY_TYPE) {
51                 autowireByType(beanName, mbd, bw, newPvs);
52             }
53 
54             pvs = newPvs;
55         }
56 
57         //对非autowiring的属性进行依赖注入处理
58 
59         boolean hasInstAwareBpps = hasInstantiationAwareBeanPostProcessors();
60         boolean needsDepCheck = (mbd.getDependencyCheck() != RootBeanDefinition.DEPENDENCY_CHECK_NONE);
61 
62         if (hasInstAwareBpps || needsDepCheck) {
63             if (pvs == null) {
64                 pvs = mbd.getPropertyValues();
65             }
66             PropertyDescriptor[] filteredPds = filterPropertyDescriptorsForDependencyCheck(bw, mbd.allowCaching);
67             if (hasInstAwareBpps) {
68                 for (BeanPostProcessor bp : getBeanPostProcessors()) {
69                     if (bp instanceof InstantiationAwareBeanPostProcessor) {
70                         InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp;
71                         pvs = ibp.postProcessPropertyValues(pvs, filteredPds, bw.getWrappedInstance(), beanName);
72                         if (pvs == null) {
73                             return;
74                         }
75                     }
76                 }
77             }
78             if (needsDepCheck) {
79                 checkDependencies(beanName, mbd, filteredPds, pvs);
80             }
81         }
82 
83         if (pvs != null) {
84             //对属性进行注入
85             applyPropertyValues(beanName, mbd, bw, pvs);
86         }
87     }

首先会检查这个包装类是否为空,在保证不为空的前提下,首先获取BeanDefinition中的资源属性值,然后对其进行自动装配处理,最后对属性进行注入。

在populateBean方法的最后,会对属性进行注入,调用applyPropertyValues方法

//解析并注入依赖属性的过程
    protected void applyPropertyValues(String beanName, BeanDefinition mbd, BeanWrapper bw, PropertyValues pvs) {
        if (pvs.isEmpty()) {
            return;
        }

        //封装属性值
        MutablePropertyValues mpvs = null;
        List<PropertyValue> original;

        if (System.getSecurityManager() != null) {
            if (bw instanceof BeanWrapperImpl) {
                //设置安全上下文,JDK安全机制
                ((BeanWrapperImpl) bw).setSecurityContext(getAccessControlContext());
            }
        }

        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;
        }
        //创建一个Bean定义属性值解析器,将Bean定义中的属性值解析为Bean实例对象的实际值
        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();
                //转换属性值,例如将引用转换为IOC容器中实例化对象引用
                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);
        }
    }

在这里核心调用的是resolveValueIfNecessary方法,将BeanDefinition里的一些属性解析出来,通过反射new出来一个对象。BeanDefinition相当于保存在内存中的配置文件,保存着所有跟这个类属性相关的信息,依赖注入就是把BeanDefinition这个信息读出来,通过反射机制或者代理等机制创建对象。一个Bean对应着一个BeanDefinition

新创建的对象不会放在IOC容器里,而是会存入到另外一个cache容器。IOC容器存放的是一个关系。

//BeanDefinitionValueResolver.class
//
解析属性值,对注入类型进行转换 @Nullable public Object resolveValueIfNecessary(Object argName, @Nullable Object value) { // We must check each value to see whether it requires a runtime reference // to another bean to be resolved. //对引用类型的属性进行解析 if (value instanceof RuntimeBeanReference) { RuntimeBeanReference ref = (RuntimeBeanReference) value; //调用引用类型属性的解析方法 return resolveReference(argName, ref); } //对属性值是引用容器中另一个Bean名称的解析 else if (value instanceof RuntimeBeanNameReference) { String refName = ((RuntimeBeanNameReference) value).getBeanName(); refName = String.valueOf(doEvaluate(refName)); //从容器中获取指定名称的Bean if (!this.beanFactory.containsBean(refName)) { throw new BeanDefinitionStoreException( "Invalid bean name '" + refName + "' in bean reference for " + argName); } return refName; } //对Bean类型属性的解析,主要是Bean中的内部类 else if (value instanceof BeanDefinitionHolder) { // Resolve BeanDefinitionHolder: contains BeanDefinition with name and aliases. BeanDefinitionHolder bdHolder = (BeanDefinitionHolder) value; return resolveInnerBean(argName, bdHolder.getBeanName(), bdHolder.getBeanDefinition()); } else if (value instanceof BeanDefinition) { // Resolve plain BeanDefinition, without contained name: use dummy name. BeanDefinition bd = (BeanDefinition) value; String innerBeanName = "(inner bean)" + BeanFactoryUtils.GENERATED_BEAN_NAME_SEPARATOR + ObjectUtils.getIdentityHexString(bd); return resolveInnerBean(argName, innerBeanName, bd); } //对集合数组类型的属性解析 else if (value instanceof ManagedArray) { // May need to resolve contained runtime references. ManagedArray array = (ManagedArray) value; //获取数组的类型 Class<?> elementType = array.resolvedElementType; if (elementType == null) { //获取数组元素的类型 String elementTypeName = array.getElementTypeName(); if (StringUtils.hasText(elementTypeName)) { try { //使用反射机制创建指定类型的对象 elementType = ClassUtils.forName(elementTypeName, this.beanFactory.getBeanClassLoader()); array.resolvedElementType = elementType; } catch (Throwable ex) { // Improve the message by showing the context. throw new BeanCreationException( this.beanDefinition.getResourceDescription(), this.beanName, "Error resolving array type for " + argName, ex); } } //没有获取到数组的类型,也没有获取到数组元素的类型 //则直接设置数组的类型为Object else { elementType = Object.class; } } //创建指定类型的数组 return resolveManagedArray(argName, (List<?>) value, elementType); } //解析list类型的属性值 else if (value instanceof ManagedList) { // May need to resolve contained runtime references. return resolveManagedList(argName, (List<?>) value); } //解析set类型的属性值 else if (value instanceof ManagedSet) { // May need to resolve contained runtime references. return resolveManagedSet(argName, (Set<?>) value); } //解析map类型的属性值 else if (value instanceof ManagedMap) { // May need to resolve contained runtime references. return resolveManagedMap(argName, (Map<?, ?>) value); } //解析props类型的属性值,props其实就是key和value均为字符串的map else if (value instanceof ManagedProperties) { Properties original = (Properties) value; //创建一个拷贝,用于作为解析后的返回值 Properties copy = new Properties(); original.forEach((propKey, propValue) -> { if (propKey instanceof TypedStringValue) { propKey = evaluate((TypedStringValue) propKey); } if (propValue instanceof TypedStringValue) { propValue = evaluate((TypedStringValue) propValue); } if (propKey == null || propValue == null) { throw new BeanCreationException( this.beanDefinition.getResourceDescription(), this.beanName, "Error converting Properties key/value pair for " + argName + ": resolved to null"); } copy.put(propKey, propValue); }); return copy; } //解析字符串类型的属性值 else if (value instanceof TypedStringValue) { // Convert value to target type here. TypedStringValue typedStringValue = (TypedStringValue) value; Object valueObject = evaluate(typedStringValue); try { //获取属性的目标类型 Class<?> resolvedTargetType = resolveTargetType(typedStringValue); if (resolvedTargetType != null) { //对目标类型的属性进行解析,递归调用 return this.typeConverter.convertIfNecessary(valueObject, resolvedTargetType); } //没有获取到属性的目标对象,则按Object类型返回 else { return valueObject; } } catch (Throwable ex) { // Improve the message by showing the context. throw new BeanCreationException( this.beanDefinition.getResourceDescription(), this.beanName, "Error converting typed String value for " + argName, ex); } } else if (value instanceof NullBean) { return null; } else { return evaluate(value); } }

同时,在上个方法中(applyPropertyValues),会调用到bw.setPropertyValues方法,他的作用是为实例化的对象设置属性

@Override
    public void setPropertyValues(PropertyValues pvs, boolean ignoreUnknown, boolean ignoreInvalid)
            throws BeansException {

        List<PropertyAccessException> propertyAccessExceptions = null;
        List<PropertyValue> propertyValues = (pvs instanceof MutablePropertyValues ?
                ((MutablePropertyValues) pvs).getPropertyValueList() : Arrays.asList(pvs.getPropertyValues()));
        for (PropertyValue pv : propertyValues) {
            try {
                // This method may throw any BeansException, which won't be caught
                // here, if there is a critical failure such as no matching field.
                // We can attempt to deal only with less serious exceptions.
                setPropertyValue(pv);
            }
            catch (NotWritablePropertyException ex) {
                if (!ignoreUnknown) {
                    throw ex;
                }
                // Otherwise, just ignore it and continue...
            }
            catch (NullValueInNestedPathException ex) {
                if (!ignoreInvalid) {
                    throw ex;
                }
                // Otherwise, just ignore it and continue...
            }
            catch (PropertyAccessException ex) {
                if (propertyAccessExceptions == null) {
                    propertyAccessExceptions = new LinkedList<>();
                }
                propertyAccessExceptions.add(ex);
            }
        }

        // If we encountered individual exceptions, throw the composite exception.
        if (propertyAccessExceptions != null) {
            PropertyAccessException[] paeArray =
                    propertyAccessExceptions.toArray(new PropertyAccessException[propertyAccessExceptions.size()]);
            throw new PropertyBatchUpdateException(paeArray);
        }
    }

主要一点是弄清楚wrapper是怎么来的。Wrapper对原生对象进行包装,通过构造方法存储原始对象,然后放入cache的是Wrapper。

考虑一下为什么要用到包装模式:减少代码侵入,能够在原生的基础之上再进行扩展,他可以覆盖、调用方法,甚至可以在原来的方法之上增加监听器、回调函数等。包装模式相当于静态代理的一种额外模式。

到这儿,Bean的依赖注入就搞定了。总结一下,在我们之前操作的IOC容器初始化后,将XML上的内容转化为BeanDefinition中的内容,然后通过传入指定Bean的名字,首先判断Chache中有没有,也要查看其父级BeanFactory,如果有,就通过限定名字得到实例,如果没有,就去创建,创建的方式也分单例和原型两种。

 

posted @ 2018-06-28 20:25  jolivan  阅读(816)  评论(1编辑  收藏  举报