Spring IOC的深入理解(五)重新写一下

2019-04-16  本文已影响0人  chengcongyue

引言

bean的加载相对于xml文件的解析更加复杂,阅读一遍源码还是云里雾里,同时为了后天的阿里二面,重新看一下bean的加载过程,这次写的争取条理清晰一点.
实例代码如下:

bf.getBean("myTestBean");

我们还是分析一下这一段代码,getBean这个方法实际上就是执行doGetBean这个方法,这个方法就是实现了bean的加载,加载之后并取出这个实例.我们来看一下这个方法的大概过程,代码量过多,我通过注解和代码的方式清晰明了的展示一下

protected <T> T doGetBean(final String name, @Nullable final Class<T> requiredType,
            @Nullable final Object[] args, boolean typeCheckOnly) throws BeansException {
                //提取beanName
        final String beanName = transformedBeanName(name);
        Object bean;

        
                 //尝试直接从缓存中获取
        Object sharedInstance = getSingleton(beanName);
                //如果从缓存中获取到了代码
        if (sharedInstance != null && args == null) {
             忽略代码
            bean = getObjectForBeanInstance(sharedInstance, name, beanName, null);
        }
        else {//如果缓冲中没有需要的代码
            忽略代码
                        //获取当当前bean的父工厂
            BeanFactory parentBeanFactory = getParentBeanFactory();
            if (parentBeanFactory != null && !containsBeanDefinition(beanName)) {
                //如果父工程中有,并且当前的工作中没有,那就递归调用doGetBean
                String nameToLookup = originalBeanName(name);
                if (parentBeanFactory instanceof AbstractBeanFactory) {
                    return ((AbstractBeanFactory) parentBeanFactory).doGetBean(
                            nameToLookup, requiredType, args, typeCheckOnly);
                }
                else if (args != null) {
                    // Delegation to parent with explicit args.
                    return (T) parentBeanFactory.getBean(nameToLookup, args);
                }
                else if (requiredType != null) {
                    // No args -> delegate to standard getBean method.
                    return parentBeanFactory.getBean(nameToLookup, requiredType);
                }
                else {
                    return (T) parentBeanFactory.getBean(nameToLookup);
                }
            }

            if (!typeCheckOnly) {
                markBeanAsCreated(beanName);
            }
//这个时候通过BeanName从获取BeanDefinition
            try {
                final RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName);
                checkMergedBeanDefinition(mbd, beanName, args);

                // Guarantee initialization of beans that the current bean depends on.
                String[] dependsOn = mbd.getDependsOn();
                if (dependsOn != null) {
                    for (String dep : dependsOn) {
                        if (isDependent(beanName, dep)) {
                            throw new BeanCreationException(mbd.getResourceDescription(), beanName,
                                    "Circular depends-on relationship between '" + beanName + "' and '" + dep + "'");
                        }
                        registerDependentBean(dep, beanName);
                        try {
                            getBean(dep);
                        }
                        catch (NoSuchBeanDefinitionException ex) {
                            throw new BeanCreationException(mbd.getResourceDescription(), beanName,
                                    "'" + beanName + "' depends on missing bean '" + dep + "'", ex);
                        }
                    }
                }

                //根据不同的策略都来创建bean的实例
                                //其中调用getSingleton是一个重载的方法,传入BeanName,和一个匿名内部类,这个实际上是一个new ObjectFactory,也就是实际上就是传入一个ObjectFactory
                if (mbd.isSingleton()) {
                    sharedInstance = getSingleton(beanName, () -> {
                        try {
                            return createBean(beanName, mbd, args);
                        }
                        catch (BeansException ex) {
                            // Explicitly remove instance from singleton cache: It might have been put there
                            // eagerly by the creation process, to allow for circular reference resolution.
                            // Also remove any beans that received a temporary reference to the bean.
                            destroySingleton(beanName);
                            throw ex;
                        }
                    });
                    bean = getObjectForBeanInstance(sharedInstance, name, beanName, mbd);
                }

                else if (mbd.isPrototype()) {
                    // It's a prototype -> create a new instance.
                    Object prototypeInstance = null;
                    try {
                        beforePrototypeCreation(beanName);
                        prototypeInstance = createBean(beanName, mbd, args);
                    }
                    finally {
                        afterPrototypeCreation(beanName);
                    }
                    bean = getObjectForBeanInstance(prototypeInstance, name, beanName, mbd);
                }

                else {
                    String scopeName = mbd.getScope();
                    final Scope scope = this.scopes.get(scopeName);
                    if (scope == null) {
                        throw new IllegalStateException("No Scope registered for scope name '" + scopeName + "'");
                    }
                    try {
                        Object scopedInstance = scope.get(beanName, () -> {
                            beforePrototypeCreation(beanName);
                            try {
                                return createBean(beanName, mbd, args);
                            }
                            finally {
                                afterPrototypeCreation(beanName);
                            }
                        });
                        bean = getObjectForBeanInstance(scopedInstance, name, beanName, mbd);
                    }
                    catch (IllegalStateException ex) {
                        throw new BeanCreationException(beanName,
                                "Scope '" + scopeName + "' is not active for the current thread; consider " +
                                "defining a scoped proxy for this bean if you intend to refer to it from a singleton",
                                ex);
                    }
                }
            }
            catch (BeansException ex) {
                cleanupAfterBeanCreationFailure(beanName);
                throw ex;
            }
        }

        // Check if required type matches the type of the actual bean instance.
        if (requiredType != null && !requiredType.isInstance(bean)) {
            try {
                T convertedBean = getTypeConverter().convertIfNecessary(bean, requiredType);
                if (convertedBean == null) {
                    throw new BeanNotOfRequiredTypeException(name, requiredType, bean.getClass());
                }
                return convertedBean;
            }
            catch (TypeMismatchException ex) {
                if (logger.isTraceEnabled()) {
                    logger.trace("Failed to convert bean '" + name + "' to required type '" +
                            ClassUtils.getQualifiedName(requiredType) + "'", ex);
                }
                throw new BeanNotOfRequiredTypeException(name, requiredType, bean.getClass());
            }
        }
        return (T) bean;
    }

bean = getObjectForBeanInstance(sharedInstance, name, beanName, mbd);这个方法贯穿整个代码,我们稍后对于这个代码,进行分析.

FactoryBean

我们再来说说FactoryBean,这个指的是一种bean的创建方式,和传统的bean便签的创建方式不同,这样的创建方式更加的灵活,然后它创建的bean实际上是继承这个接口的FactoryBean的实现类的getObject()方法,如果想获得这个工厂的本身,要getBean("&car");

从缓存中获取单例的bean

在doGetBean中的核心的第一句就是从缓存中获取bean

Object sharedInstance = getSingleton(beanName);

那现在我们进入到这个方法中一探究竟

    protected Object getSingleton(String beanName, boolean allowEarlyReference) {
        Object singletonObject = this.singletonObjects.get(beanName);
        if (singletonObject == null && isSingletonCurrentlyInCreation(beanName)) {
            synchronized (this.singletonObjects) {
                singletonObject = this.earlySingletonObjects.get(beanName);
                if (singletonObject == null && allowEarlyReference) {
                    ObjectFactory<?> singletonFactory = this.singletonFactories.get(beanName);
                    if (singletonFactory != null) {
                        singletonObject = singletonFactory.getObject();
                        this.earlySingletonObjects.put(beanName, singletonObject);
                        this.singletonFactories.remove(beanName);
                    }
                }
            }
        }
        return singletonObject;
    }

在其中有多个map,也按照一定的顺序对这些map,进行操作.
首先最开始的就是从singletonObjects根据BeanName获取实例,这样大概能够推测出singletonObjects的这个map是BeanName和bean的对应map.如果从这个map中没有获取到,那么就从earlySingletonObjects中获取,这个map的意思是也是beanName和bean的对应关系,这个map是用来测试循环引用的,如果这样也没有获取到,那么只能通过singletonFactories获取了,这个map的意思是指BeanName和对应bean的ObjectFactory的工厂的对应关系,将这个创建好的bean放入到earlySingletonObjects,然后在清除singletonFactories中的信息,这两个map是互斥的,还是有些难理解他们的内部是怎么样的,不过这个时候已经对如何从缓存中获得单例有了大概的了解.

从bean中获取对象

像我们在开始说的bean = getObjectForBeanInstance(sharedInstance, name, beanName, mbd)这个代码的频率太高了,这个代码的意思就是说,从通过bean的单例获取bean对应的对象.
其中的方法,也大多是修饰的方法,如果是普通的bean这个时候就可以直接返回了

if (!(beanInstance instanceof FactoryBean) || BeanFactoryUtils.isFactoryDereference(name)) {
            return beanInstance;
        }

,但如果是FactoryBean的话,就要进行进一步的处理,我们来到这个代码的最后一步

object = getObjectFromFactoryBean(factory, beanName, !synthetic);

又委托给了另一个方法

protected Object getObjectFromFactoryBean(FactoryBean<?> factory, String beanName, boolean shouldPostProcess) {
        if (factory.isSingleton() && containsSingleton(beanName)) {
                synchronized (getSingletonMutex()) {
                    Object object = this.factoryBeanObjectCache.get(beanName);
                    if (object == null) {
                        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) {
                                if (isSingletonCurrentlyInCreation(beanName)) {
                                    // Temporarily return non-post-processed object, not storing it yet..
                                    return object;
                                }
                                beforeSingletonCreation(beanName);
                                try {
                                    object = postProcessObjectFromFactoryBean(object, beanName);
                                }
                                catch (Throwable ex) {
                                    throw new BeanCreationException(beanName,
                                            "Post-processing of FactoryBean's singleton object failed", ex);
                                }
                                finally {
                                    afterSingletonCreation(beanName);
                                }
                            }
                            if (containsSingleton(beanName)) {
                                this.factoryBeanObjectCache.put(beanName, object);
                            }
                        }
                    }
                    return object;
                }
        }
        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;
        }
    }

这个方法中还是没有我们的核心方法,然后我们进入到这个里面的方法doGetObjectFromFactoryBean,终于我们看到我们想要的了

private Object doGetObjectFromFactoryBean(final FactoryBean<?> factory, final String beanName)
            throws BeanCreationException {

        Object object;
        try {
            if (System.getSecurityManager() != null) {
                AccessControlContext acc = getAccessControlContext();
                try {
                    object = AccessController.doPrivileged((PrivilegedExceptionAction<Object>) factory::getObject, acc);
                }
                catch (PrivilegedActionException pae) {
                    throw pae.getException();
                }
            }
            else {
                object = factory.getObject();
            }
        }

object = factory.getObject();也验证了我们说的,FactoryBean是通过调用它本身的getObject()来创建的.上面的截得代码并不是全部,在创建了object之后,我们要执行的方法object = postProcessObjectFromFactoryBean(object, beanName);
这个就是对后处理器的使用.后处理的规则就是一条:尽可能保证所有的bean在初始化后都会调用Object current = processor.postProcessAfterInitialization(result, beanName);这个方法,我们稍后在说.
上面我们解决了从缓存中获取实例和创建实例的过程,然后我们看看在单例模式策略下是如何的获取bean对象的

if (mbd.isSingleton()) {
                    sharedInstance = getSingleton(beanName, () -> {
                        try {
                            return createBean(beanName, mbd, args);
                        }
                        catch (BeansException ex) {
                            // Explicitly remove instance from singleton cache: It might have been put there
                            // eagerly by the creation process, to allow for circular reference resolution.
                            // Also remove any beans that received a temporary reference to the bean.
                            destroySingleton(beanName);
                            throw ex;
                        }
                    });
                    bean = getObjectForBeanInstance(sharedInstance, name, beanName, mbd);
                }

以上代码是在单例模式创建一个实例,我们来看看这个getSingleton方法,虽然代码量很多,但是核心的代码就如下几条:

beforeSingletonCreation(beanName);
singletonObject = singletonFactory.getObject();
afterSingletonCreation(beanName);
addSingleton(beanName, singletonObject);

然后我们来分析一下,这四条代码的含义吧
我们先明确一点,核心代码不是在其中完成的,然后是一个回调函数,回调函数传入singletonFactory.在此起之前我们会调用上面代码的beforeSingletonCreation(beanName);对加载状态的记录,将正在创建的对象加入到缓存中;afterSingletonCreation(beanName);则就是bean加载之后移除缓存对bean的加载状态.最后将结果加入到缓存中,然后删除各种辅助状态.我们回到上一层实际上实现bean创建的是匿名内部类的return createBean(beanName, mbd, args);这条语句

   mbdToUse.prepareMethodOverrides();

这个就是验证即准备覆盖的方法,这句话的作用大概就是,Spring中有两个属性lookup-method和replace-method的,而这两个属性在BeanDefinition中对应的就是methods-override,这个方法就是如果检验过有这个属性的时候,生成代理并产生拦截器为bean做增强处理.
然后就是实例化的前置处理.

Object bean = resolveBeforeInstantiation(beanName, mbdToUse);

还会做一个短路判断

if (bean != null) {
                return bean;
            }

AOP就是在这里进行判断的,如果前处理完成之后,这时候不为空,就直接返回.

protected Object resolveBeforeInstantiation(String beanName, RootBeanDefinition mbd) {
        Object bean = null;
        if (!Boolean.FALSE.equals(mbd.beforeInstantiationResolved)) {
            // Make sure bean class is actually resolved at this point.
            if (!mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) {
                Class<?> targetType = determineTargetType(beanName, mbd);
                if (targetType != null) {
                    bean = applyBeanPostProcessorsBeforeInstantiation(targetType, beanName);
                    if (bean != null) {
                        bean = applyBeanPostProcessorsAfterInitialization(bean, beanName);
                    }
                }
            }
            mbd.beforeInstantiationResolved = (bean != null);
        }
        return bean;
    }

其中是两个方法都是对后处理器的调用
实例化前后处理器的应用

protected Object applyBeanPostProcessorsBeforeInstantiation(Class<?> beanClass, String beanName) {
        for (BeanPostProcessor bp : getBeanPostProcessors()) {
            if (bp instanceof InstantiationAwareBeanPostProcessor) {
                InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp;
                Object result = ibp.postProcessBeforeInstantiation(beanClass, beanName);
                if (result != null) {
                    return result;
                }
            }
        }
        return null;
    }

这里就是将BeanDefinition转化成BeanWrapper的处理
实例化后后处理器的应用
Object current = processor.postProcessAfterInitialization(result, beanName);尽量保证所有的bean在初始化完成后调用postProcessAfterInitialization方法

上一篇下一篇

猜你喜欢

热点阅读