getFactoryMethodName

小说:20棋牌游戏平台作者:文丁辛马更新时间:2019-01-22字数:50177

“哎呀,那可太好了。只是这样,不影响姐姐休息吗?”王小民小嘴很甜地说道。

在线德州扑克现金

“什么人?就算是宙斯也无法找到我这一片空间,这个人居然能找到?怎么可能?难道他是众神那一边的?不,不可能,如果是众神找到我的空间早就大举杀过来了。
如来道:“你体内生机流失,故而难以自制,做错了事,我不怪你。”

可是刘皓的摄心术却能做到这一点,而且最重要的是用多少次都没问题,而且看他的样子就知道没什么消耗,而且还能同时操纵很多人,只要刘皓的真气,精神足够强大支撑得起来就没问题了。

getFactoryMethodName


首先说一点,因为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在初始化完成之后返回??

当前文章:http://0477auto.com/list_56596.html

发布时间:2019-01-22 06:38:41

波克棋牌怎么更新版本 赖子山庄手机版官网站 快三软件苹果版 飞行棋棋盘怎么画 扑克牌四人能玩啥 四人麻将单机版apk 大麻将游戏下载手机版 打鱼游戏可以下分

编辑:公文密公

我要说两句: (0人参与)

发布