美文网首页
Spring IOC解析

Spring IOC解析

作者: verk | 来源:发表于2018-08-22 19:45 被阅读0次

    1. 接口说明

    1.1. BeanFactory

      IOC最顶层接口,定义了IOC容器规范,提供getBean()方法。对BeanDefinition进行管理。

    1.2. HierarchicalBeanFactory

      多层BeanFactory,继承自BeanFactory,可以有父BeanFactory,提供getParentBeanFactory()方法。

    1.3. ListableBeanFactory

      可列表的BeanFactory,提供getBeanNames()方法,可以列出BeanFactory管理的Bean名称

    1.4. ApplicationEventPublisher

      该接口提供事件通知能力,提供事件发布接口

    1.5. MessageSource

      提供消息国际化接口

    1.6. ApplicationContext

      高级BeanFactory,作为应用上下文和核心接口,可以有父ApplicationContext

    
    public interface ApplicationContext extends EnvironmentCapable, ListableBeanFactory, HierarchicalBeanFactory,MessageSource, ApplicationEventPublisher, ResourcePatternResolver
    
    

    1.7. WebApplicationContext

      提供获取servletContext()方法

    1.8. BeanDefinition

      用于描述Bean,通过该接口可以获取Bean的基本信息,包括SCOPE、ClassName、isAutowireCandidate等。

    [图片上传失败...(image-82f3b9-1535966302304)]

    1.9. AnnotatedBeanDefinition

      继承BeanDefinition,提供获取Bean注解的方法

    1.10. BeanDefinitionRegistry

      作为BeanDefinition注册器,当加载BeanDefinition后可以把BeanDefinition注册到注册表中,通常由BeanFactory实现该接口。大部分ApplicationContext都实现了该接口。

    1.11. BeanPostProcessor

      提供postProcessBeforeInitialization和postProcessAfterInitialization方法,用于在bean初始化之前/之后做自定义的处理。

    1.12. BeanFactoryPostProcessor

      类似BeanPostProcessor,可以对beanDefinition做自定义处理。

    1.13. AutowiredAnnotationBeanPostProcessor

      实现了BeanPostProcessor方法,可以解析参数中的注解完成自动装配

    2. XmlBeanFactory类加载过程解析

      XmlBeanFactory继承自DefaultListableBeanFactory,已经实现BeanDefinitionRegistry接口。新建一个XmlBeanFactory时,需要传入一个Xml文件的Resource对象,在构造方法中,调用XmlBeanDefinitionReader对Resource进行解析,加载Xml文件中的BeanDefinition。

    Spring Bean声明周期.jpg

    2.1. BeanDefinition加载过程

    
    public class XmlBeanFactory extends DefaultListableBeanFactory {
    
        private final XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(this);
    
    
        /**
         * Create a new XmlBeanFactory with the given resource,
         * which must be parsable using DOM.
         * @param resource XML resource to load bean definitions from
         * @throws BeansException in case of loading or parsing errors
         */
        public XmlBeanFactory(Resource resource) throws BeansException {
            this(resource, null);
        }
    
        /**
         * Create a new XmlBeanFactory with the given input stream,
         * which must be parsable using DOM.
         * @param resource XML resource to load bean definitions from
         * @param parentBeanFactory parent bean factory
         * @throws BeansException in case of loading or parsing errors
         */
        public XmlBeanFactory(Resource resource, BeanFactory parentBeanFactory) throws BeansException {
            super(parentBeanFactory);
            this.reader.loadBeanDefinitions(resource);
        }
    
    }
    
    

    过程如下

      通过ResourceLoader来完成资源文件位置的定位,DefaultResourceLoader是默认的实现,同时上下文本身就给出了ResourceLoader的实现,可以从类路径,文件系统,URL等方式来定为资源位置。如果是XmlBeanFactory作为IoC 容器,那么需要为它指定bean定义的资源,也就是说bean定义文件时通过抽象成Resource来被IoC容器处理的,容器通过BeanDefinitionReader来完成定义信息的解析和Bean信息的注册,往往使用的是XmlBeanDefinitionReader来解析bean的xml定义文件。实际的处理过程是委托给BeanDefinitionParserDelegate来完成的,从而得到bean的定义信息,这些信息在Spring 中使用BeanDefinition对象来表示,这个名字可以让我们想到loadBeanDefinition,RegisterBeanDefinition这些相关的方法 ,他们都是为处理BeanDefinition服务的,容器解析得到BeanDefinition IoC以后,需要把它在IoC容器中注册,这由IoC实现 BeanDefinitionRegistry接口来实现。注册过程就是在IoC容器内部维护的一个HashMap 来保存得到的BeanDefinition的过程。这个HashMap是IoC容器持有bean信息的场所,以后对bean的操作都是围绕这个HashMap 来实现的。随后我们就可以使用BeanFactory提供的方法获取Bean使用了。

    2.2. IoC依赖注入

      当Spring IoC容器完成了Bean定义资源的定位、载入和解析注册以后,IoC容器中已经管理类Bean定义的相关数据,但是此时IoC容器还没有对所管理的Bean进行依赖注入,依赖注入在以下两种情况发生:

    • 用户第一次通过getBean方法向IoC容索要Bean时,IoC容器触发依赖注入。

    • 当用户在Bean定义资源中为<Bean>元素配置了lazy-init属性,即让容器在解析注册Bean定义时进行预实例化,触发依赖注入。

    2.2.1. AbstractBeanFactory的doGetBean方法解析

    调用AbstractBeanFactory的getBean方法时,实际触发的是doGetBean方法,下面对doGetBean方法源码进行解析。

    
    //name 要检索的 bean 的名称
    //requiredType 要检索的bean所需的类型
    //args 使用显式参数创建bean实例时使用的参数(仅在创建新实例时应用,而不是在检索现有实例时应用)
    //typeCheckOnly 是否为类型检查而获得实例,而不是实际使用
    @SuppressWarnings("unchecked")
        protected <T> T doGetBean(
                final String name, final Class<T> requiredType, final Object[] args, boolean typeCheckOnly)
                throws BeansException {
    
            final String beanName = transformedBeanName(name);
            Object bean;
    
            // Eagerly check singleton cache for manually registered singletons.
            //从单例注册中心中获取Bean
            Object sharedInstance = getSingleton(beanName);
            //如果单例注册中心中已经存在Bean,且没有参数,则直接返回Bean
            if (sharedInstance != null && args == null) {
                if (logger.isDebugEnabled()) {
                    if (isSingletonCurrentlyInCreation(beanName)) {
                        logger.debug("Returning eagerly cached instance of singleton bean '" + beanName +
                                "' that is not fully initialized yet - a consequence of a circular reference");
                    }
                    else {
                        logger.debug("Returning cached instance of singleton bean '" + beanName + "'");
                    }
                }
                bean = getObjectForBeanInstance(sharedInstance, name, beanName, null);
            }
    
            else {
                // Fail if we're already creating this bean instance:
                // We're assumably within a circular reference.
                if (isPrototypeCurrentlyInCreation(beanName)) {
                    throw new BeanCurrentlyInCreationException(beanName);
                }
    
                // Check if bean definition exists in this factory.
                BeanFactory parentBeanFactory = getParentBeanFactory();
                //如果本级没有该Bean的BeanDefinition,且父BeanFactory存在,则从父BeanFactory中获取Bean
                if (parentBeanFactory != null && !containsBeanDefinition(beanName)) {
                    // Not found -> check parent.
                    String nameToLookup = originalBeanName(name);
                    if (args != null) {
                        // Delegation to parent with explicit args.
                        return (T) parentBeanFactory.getBean(nameToLookup, args);
                    }
                    else {
                        // No args -> delegate to standard getBean method.
                        return parentBeanFactory.getBean(nameToLookup, requiredType);
                    }
                }
    
                if (!typeCheckOnly) {
                    //标记为创建状态,并清理掉MegredBeanDefinition。不太理解
                    markBeanAsCreated(beanName);
                }
    
                try {
                    //获取RBD,RBD是经过BD和父类BD融合的BeanDefinition
                    final RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName);
                    //检查RBD,可能会抛出异常。在AbstractBeanFactory中,检测RBD定义的Bean是否为抽象
                    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 + "'");
                            }
                            //注册到Map中,双向注册,可以通过2个Map相互检索
                            registerDependentBean(dep, beanName);
                            try {
                                //递归调用获取所有依赖的Bean
                                getBean(dep);
                            }
                            catch (NoSuchBeanDefinitionException ex) {
                                throw new BeanCreationException(mbd.getResourceDescription(), beanName,
                                        "'" + beanName + "' depends on missing bean '" + dep + "'", ex);
                            }
                        }
                    }
    
                    // Create bean instance.
                    //如果是单例,则调用DefaultSingletonBeanRegistry的getSingleton方法createBean
                    if (mbd.isSingleton()) {
                        sharedInstance = getSingleton(beanName, new ObjectFactory<Object>() {
                            @Override
                            public Object getObject() throws BeansException {
                                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);
                    }
                    //如果Scope是其他类型
                    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, new ObjectFactory<Object>() {
                                @Override
                                public Object getObject() throws BeansException {
                                    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 && bean != null && !requiredType.isInstance(bean)) {
                try {
                    return getTypeConverter().convertIfNecessary(bean, requiredType);
                }
                catch (TypeMismatchException ex) {
                    if (logger.isDebugEnabled()) {
                        logger.debug("Failed to convert bean '" + name + "' to required type '" +
                                ClassUtils.getQualifiedName(requiredType) + "'", ex);
                    }
                    throw new BeanNotOfRequiredTypeException(name, requiredType, bean.getClass());
                }
            }
            return (T) bean;
        }
    
    

    2.2.2. DefaultSingletonBeanRegistry解析

      在上一小节中我们对AbstractBeanFactory中的doGetBean进行了分析,其中调用了不少DefaultSingletonBeanRegistry中的方法,下面我们对其核心源码进行分析。

    
     /** Cache of singleton objects: bean name --> bean instance */
     //一级缓存,已经创建成功的Bean缓存在这里
     private final Map<String, Object> singletonObjects = new ConcurrentHashMap<String, Object>(256);
    
    //二级缓存,如果Bean有其他依赖,会将该Bean先缓存在这里,以便其他Bean依赖。用来解决循环依赖
     /** Cache of early singleton objects: bean name --> bean instance */
     private final Map<String, Object> earlySingletonObjects = new HashMap<String, Object>(16);
     
    //三级缓存,待创建的Bean缓存在这里
     /** Cache of singleton factories: bean name --> ObjectFactory */
     private final Map<String, ObjectFactory<?>> singletonFactories = new HashMap<String, ObjectFactory<?>>(16);
    
    
      //
      protected Object getSingleton(String beanName, boolean allowEarlyReference) {
          //从一级缓存中查询Bean
        Object singletonObject = this.singletonObjects.get(beanName);
         //如果从一级缓存中没有查到Bean,且该Bean正在创建
        if (singletonObject == null && isSingletonCurrentlyInCreation(beanName)) {
          synchronized (this.singletonObjects) {
              //从二级缓存查询Bean
            singletonObject = this.earlySingletonObjects.get(beanName);
            //如果二级缓存中没有查询到Bean
            if (singletonObject == null && allowEarlyReference) {
                //从三级缓存中查询
              ObjectFactory<?> singletonFactory = this.singletonFactories.get(beanName);
              //如果从三级缓存中查询到Bean
              if (singletonFactory != null) {
                  //从Factory中获取Bean
                singletonObject = singletonFactory.getObject();
                  //放入二级缓存中
                this.earlySingletonObjects.put(beanName, singletonObject);
                  //从三级缓存中删除
                this.singletonFactories.remove(beanName);
              }
            }
          }
        }
        //返回
        return (singletonObject != NULL_OBJECT ? singletonObject : null);
      }
    
    
        /**获取单例对象,如果对象不存在,则使用singletonFactory创建一个
         * Return the (raw) singleton object registered under the given name,
         * creating and registering a new one if none registered yet.
         * @param beanName the name of the bean
         * @param singletonFactory the ObjectFactory to lazily create the singleton
         * with, if necessary
         * @return the registered singleton object
         */
        public Object getSingleton(String beanName, ObjectFactory<?> singletonFactory) {
            Assert.notNull(beanName, "'beanName' must not be null");
            synchronized (this.singletonObjects) {
                //从一级缓存中查询Bean
                Object singletonObject = this.singletonObjects.get(beanName);
                if (singletonObject == null) {
                    //如果正在销毁Bean,则抛出异常
                    if (this.singletonsCurrentlyInDestruction) {
                        throw new BeanCreationNotAllowedException(beanName,
                                "Singleton bean creation not allowed while singletons of this factory are in destruction " +
                                "(Do not request a bean from a BeanFactory in a destroy method implementation!)");
                    }
                    if (logger.isDebugEnabled()) {
                        logger.debug("Creating shared instance of singleton bean '" + beanName + "'");
                    }
                    //执行创建之前的操作,方法默认进行了排除检测,并把该beanName加入到singletonsCurrentlyInCreation中。如果该beanName已经在singletonsCurrentlyInCreation中,则抛出异常。
                    beforeSingletonCreation(beanName);
                    boolean newSingleton = false;
                    boolean recordSuppressedExceptions = (this.suppressedExceptions == null);
                    //如果this.suppressedExceptions == null,则新建
                    if (recordSuppressedExceptions) {
                        this.suppressedExceptions = new LinkedHashSet<Exception>();
                    }
                    try {
                        //获取bean
                        singletonObject = singletonFactory.getObject();
                        //如果执行到这一步,说明没有异常抛出,说明获取bean成功,标志位置true
                        newSingleton = true;
                    }
                    catch (IllegalStateException ex) {
                        // Has the singleton object implicitly appeared in the meantime ->
                        // if yes, proceed with it since the exception indicates that state.
                        singletonObject = this.singletonObjects.get(beanName);
                        if (singletonObject == null) {
                            throw ex;
                        }
                    }
                    catch (BeanCreationException ex) {
                        if (recordSuppressedExceptions) {
                            for (Exception suppressedException : this.suppressedExceptions) {
                                ex.addRelatedCause(suppressedException);
                            }
                        }
                        throw ex;
                    }
                    finally {
                        if (recordSuppressedExceptions) {
                            this.suppressedExceptions = null;
                        }
                        //执行创建之后的操作,把beanName从singletonsCurrentlyInCreation中移除
                        afterSingletonCreation(beanName);
                    }
                    //如果为true
                    if (newSingleton) {
                        //把bean放入到一级缓存和registeredSingletons,并从二级/三级缓存中移除
                        addSingleton(beanName, singletonObject);
                    }
                }
                return (singletonObject != NULL_OBJECT ? singletonObject : null);
            }
        }
    

    2.2.3 AbstractAutowireCapableBeanFactory源码分析

      在AbstractBeanFactory中的doGetBean方法中,会调用createBean方法,在AbstractAutowireCapableBeanFactory中实现了createBean方法,下面我们一起分析源码。

    
        /**
         * Central method of this class: creates a bean instance,
         * populates the bean instance, applies post-processors, etc.
         * @see #doCreateBean
         */
        @Override
        protected Object createBean(String beanName, RootBeanDefinition mbd, Object[] args) throws BeanCreationException {
            if (logger.isDebugEnabled()) {
                logger.debug("Creating instance of bean '" + beanName + "'");
            }
            RootBeanDefinition mbdToUse = mbd;
    
            // 解析class
            // 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.
            Class<?> resolvedClass = resolveBeanClass(mbd, beanName);
            if (resolvedClass != null && !mbd.hasBeanClass() && mbd.getBeanClassName() != null) {
                mbdToUse = new RootBeanDefinition(mbd);
                mbdToUse.setBeanClass(resolvedClass);
            }
    
            // Prepare method overrides.
            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.
                // 尝试使用BeanPostProcessors创建一个代理
                Object bean = resolveBeforeInstantiation(beanName, mbdToUse);
                if (bean != null) {
                    // 如果使用尝试使用BeanPostProcessors创建一个代理创建代理Bean成功,则直接返回
                    return bean;
                }
            }
            catch (Throwable ex) {
                throw new BeanCreationException(mbdToUse.getResourceDescription(), beanName,
                        "BeanPostProcessor before instantiation of bean failed", ex);
            }
    
            // 调用doCreateBean
            Object beanInstance = doCreateBean(beanName, mbdToUse, args);
            if (logger.isDebugEnabled()) {
                logger.debug("Finished creating instance of bean '" + beanName + "'");
            }
            return beanInstance;
        }
    
    
        protected Object doCreateBean(final String beanName, final RootBeanDefinition mbd, final Object[] args)
                throws BeanCreationException {
    
            // Instantiate the bean.
            BeanWrapper instanceWrapper = null;
            if (mbd.isSingleton()) {
                // 从缓存中获取
                instanceWrapper = this.factoryBeanInstanceCache.remove(beanName);
            }
            if (instanceWrapper == null) {
                // 创建BwanWrapper,注意,只是创建BeanWrapper,并不会对Bean中的Field进行注入操作
                instanceWrapper = createBeanInstance(beanName, mbd, args);
            }
            final Object bean = (instanceWrapper != null ? instanceWrapper.getWrappedInstance() : null);
            Class<?> beanType = (instanceWrapper != null ? instanceWrapper.getWrappedClass() : null);
            mbd.resolvedTargetType = beanType;
    
            // Allow post-processors to modify the merged bean definition.
            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.
            // if 是单例 && 允许循环依赖(字段默认为true) && bean正在创建(一般是正在创建的,此方法是在DefaultSingletonBeanRegistry中的getSingleton方法中调用的)
            // 则把该Bean放到DefaultSingletonBeanRegistry的3级缓存中,以便其他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, new ObjectFactory<Object>() {
                    @Override
                    public Object getObject() throws BeansException {
                        return getEarlyBeanReference(beanName, mbd, bean);
                    }
                });
            }
    
            // Initialize the bean instance.
            Object exposedObject = bean;
            try {
                // 划重点!!在此方法中注入依赖的Bean
                // 划重点!!在此方法中注入依赖的Bean
                // 划重点!!在此方法中注入依赖的Bean
                populateBean(beanName, mbd, instanceWrapper);
                if (exposedObject != null) {
                    // 调用aware方法和beanPostProcesser方法
                    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) {
                Object earlySingletonReference = getSingleton(beanName, false);
                if (earlySingletonReference != null) {
                    if (exposedObject == bean) {
                        exposedObject = earlySingletonReference;
                    }
                    else if (!this.allowRawInjectionDespiteWrapping && hasDependentBean(beanName)) {
                        String[] dependentBeans = getDependentBeans(beanName);
                        Set<String> actualDependentBeans = new LinkedHashSet<String>(dependentBeans.length);
                        for (String dependentBean : dependentBeans) {
                            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.
            try {
                registerDisposableBeanIfNecessary(beanName, bean, mbd);
            }
            catch (BeanDefinitionValidationException ex) {
                throw new BeanCreationException(
                        mbd.getResourceDescription(), beanName, "Invalid destruction signature", ex);
            }
    
            return exposedObject;
        }
    
        //注入依赖的Bean
        protected void populateBean(String beanName, RootBeanDefinition mbd, BeanWrapper bw) {
            PropertyValues pvs = mbd.getPropertyValues();
    
            if (bw == null) {
                if (!pvs.isEmpty()) {
                    throw new BeanCreationException(
                            mbd.getResourceDescription(), beanName, "Cannot apply property values to null instance");
                }
                else {
                    // Skip property population phase for null instance.
                    return;
                }
            }
    
            // Give any InstantiationAwareBeanPostProcessors the opportunity to modify the
            // state of the bean before properties are set. This can be used, for example,
            // to support styles of field injection.
            boolean continueWithPropertyPopulation = true;
    
            if (!mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) {
                for (BeanPostProcessor bp : getBeanPostProcessors()) {
                    if (bp instanceof InstantiationAwareBeanPostProcessor) {
                        InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp;
                        if (!ibp.postProcessAfterInstantiation(bw.getWrappedInstance(), beanName)) {
                            continueWithPropertyPopulation = false;
                            break;
                        }
                    }
                }
            }
    
            if (!continueWithPropertyPopulation) {
                return;
            }
    
            //检测自动装配类型,使用注解时自动装配类型为none
            if (mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_BY_NAME ||
                    mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_BY_TYPE) {
                MutablePropertyValues newPvs = new MutablePropertyValues(pvs);
    
                // Add property values based on autowire by name if applicable.
                if (mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_BY_NAME) {
                    autowireByName(beanName, mbd, bw, newPvs);
                }
    
                // Add property values based on autowire by type if applicable.
                if (mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_BY_TYPE) {
                    autowireByType(beanName, mbd, bw, newPvs);
                }
    
                pvs = newPvs;
            }
    
            boolean hasInstAwareBpps = hasInstantiationAwareBeanPostProcessors();
            boolean needsDepCheck = (mbd.getDependencyCheck() != RootBeanDefinition.DEPENDENCY_CHECK_NONE);
    
            if (hasInstAwareBpps || needsDepCheck) {
                PropertyDescriptor[] filteredPds = filterPropertyDescriptorsForDependencyCheck(bw, mbd.allowCaching);
                if (hasInstAwareBpps) {
                    for (BeanPostProcessor bp : getBeanPostProcessors()) {
                        if (bp instanceof InstantiationAwareBeanPostProcessor) {
                            //使用AutowiredAnnotationBeanPostProcessor对注解进行处理
                            InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp;
                            pvs = ibp.postProcessPropertyValues(pvs, filteredPds, bw.getWrappedInstance(), beanName);
                            if (pvs == null) {
                                return;
                            }
                        }
                    }
                }
                if (needsDepCheck) {
                    checkDependencies(beanName, mbd, filteredPds, pvs);
                }
            }
    
            applyPropertyValues(beanName, mbd, bw, pvs);
        }
    

    2.2.4 AutowiredAnnotationBeanPostProcessor源码解析

      AbstractAutowireCapableBeanFactory中的doCreateBean方法中调用了populateBean方法,在方法中调用了InstantiationAwareBeanPostProcessor接口的postProcessPropertyValues方法,而InstantiationAwareBeanPostProcessor的实现AutowiredAnnotationBeanPostProcessor对注解进行处理,实现解析注解并进行依赖注入。下面一起对源码进行分析。

    
        @Override
        public PropertyValues postProcessPropertyValues(
                PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) throws BeanCreationException {
    
            //查找注解信息
            InjectionMetadata metadata = findAutowiringMetadata(beanName, bean.getClass(), pvs);
            try {
                //注入依赖
                metadata.inject(bean, beanName, pvs);
            }
            catch (BeanCreationException ex) {
                throw ex;
            }
            catch (Throwable ex) {
                throw new BeanCreationException(beanName, "Injection of autowired dependencies failed", ex);
            }
            return pvs;
        }
    
        private InjectionMetadata findAutowiringMetadata(String beanName, Class<?> clazz, PropertyValues pvs) {
            // Fall back to class name as cache key, for backwards compatibility with custom callers.
            String cacheKey = (StringUtils.hasLength(beanName) ? beanName : clazz.getName());
            // Quick check on the concurrent map first, with minimal locking.
            InjectionMetadata metadata = this.injectionMetadataCache.get(cacheKey);
            if (InjectionMetadata.needsRefresh(metadata, clazz)) {
                synchronized (this.injectionMetadataCache) {
                    metadata = this.injectionMetadataCache.get(cacheKey);
                    if (InjectionMetadata.needsRefresh(metadata, clazz)) {
                        if (metadata != null) {
                            metadata.clear(pvs);
                        }
                        try {
                            //构建注入元数据
                            metadata = buildAutowiringMetadata(clazz);
                            this.injectionMetadataCache.put(cacheKey, metadata);
                        }
                        catch (NoClassDefFoundError err) {
                            throw new IllegalStateException("Failed to introspect bean class [" + clazz.getName() +
                                    "] for autowiring metadata: could not find class that it depends on", err);
                        }
                    }
                }
            }
            return metadata;
        }
    
        private InjectionMetadata buildAutowiringMetadata(final Class<?> clazz) {
            LinkedList<InjectionMetadata.InjectedElement> elements = new LinkedList<InjectionMetadata.InjectedElement>();
            Class<?> targetClass = clazz;
    
            do {
                final LinkedList<InjectionMetadata.InjectedElement> currElements =
                        new LinkedList<InjectionMetadata.InjectedElement>();
    
                //对Field进行反射处理,并注册回调函数
                ReflectionUtils.doWithLocalFields(targetClass, new ReflectionUtils.FieldCallback() {
                    @Override
                    public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException {
                        //查找带@autowired、@value、@Inject的字段
                        AnnotationAttributes ann = findAutowiredAnnotation(field);
                        //如果查找到了
                        if (ann != null) {
                            //如果字段是static的,则直接返回
                            if (Modifier.isStatic(field.getModifiers())) {
                                if (logger.isWarnEnabled()) {
                                    logger.warn("Autowired annotation is not supported on static fields: " + field);
                                }
                                return;
                            }
                            //是否必须
                            boolean required = determineRequiredStatus(ann);
                            //字段增加到list中
                            currElements.add(new AutowiredFieldElement(field, required));
                        }
                    }
                });
                //对Method进行反射处理,并注册回调函数
                ReflectionUtils.doWithLocalMethods(targetClass, new ReflectionUtils.MethodCallback() {
                    @Override
                    public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
                        Method bridgedMethod = BridgeMethodResolver.findBridgedMethod(method);
                        if (!BridgeMethodResolver.isVisibilityBridgeMethodPair(method, bridgedMethod)) {
                            return;
                        }
                        //查找带@autowired、@value、@Inject的方法
                        AnnotationAttributes ann = findAutowiredAnnotation(bridgedMethod);
                        if (ann != null && method.equals(ClassUtils.getMostSpecificMethod(method, clazz))) {
                            //如果字段是static的,则直接返回
                            if (Modifier.isStatic(method.getModifiers())) {
                                if (logger.isWarnEnabled()) {
                                    logger.warn("Autowired annotation is not supported on static methods: " + method);
                                }
                                return;
                            }
                            if (method.getParameterTypes().length == 0) {
                                if (logger.isWarnEnabled()) {
                                    logger.warn("Autowired annotation should only be used on methods with parameters: " +
                                            method);
                                }
                            }
                            //是否必须
                            boolean required = determineRequiredStatus(ann);
                            PropertyDescriptor pd = BeanUtils.findPropertyForMethod(bridgedMethod, clazz);
                            //方法增加到list中
                            currElements.add(new AutowiredMethodElement(method, required, pd));
                        }
                    }
                });
    
                elements.addAll(0, currElements);
                targetClass = targetClass.getSuperclass();
            }
            while (targetClass != null && targetClass != Object.class);
    
            return new InjectionMetadata(clazz, elements);
        }
    

      在postProcessPropertyValues方法中调用了InjectionMetadata的inject方法,下面进行源码分析。

    
        public void inject(Object target, String beanName, PropertyValues pvs) throws Throwable {
            Collection<InjectedElement> elementsToIterate =
                    (this.checkedElements != null ? this.checkedElements : this.injectedElements);
            if (!elementsToIterate.isEmpty()) {
                boolean debug = logger.isDebugEnabled();
                for (InjectedElement element : elementsToIterate) {
                    if (debug) {
                        logger.debug("Processing injected element of bean '" + beanName + "': " + element);
                    }
                    //调用InjectedElement的inject方法进入注入,注意在buildAutowiringMetadata方法中,注入的InjectedElement是AutowiredMethodElement和AutowiredFieldElement
                    element.inject(target, beanName, pvs);
                }
            }
        }
    
    

      AutowiredFieldElement源码:

    
            @Override
            protected void inject(Object bean, String beanName, PropertyValues pvs) throws Throwable {
                Field field = (Field) this.member;
                Object value;
                if (this.cached) {
                    value = resolvedCachedArgument(beanName, this.cachedFieldValue);
                }
                else {
                    DependencyDescriptor desc = new DependencyDescriptor(field, this.required);
                    desc.setContainingClass(bean.getClass());
                    Set<String> autowiredBeanNames = new LinkedHashSet<String>(1);
                    TypeConverter typeConverter = beanFactory.getTypeConverter();
                    try {
                        //调用ConfigurableListableBeanFactory的resolveDependency方法,解析依赖
                        value = beanFactory.resolveDependency(desc, beanName, autowiredBeanNames, typeConverter);
                    }
                    catch (BeansException ex) {
                        throw new UnsatisfiedDependencyException(null, beanName, new InjectionPoint(field), ex);
                    }
                    synchronized (this) {
                        if (!this.cached) {
                            if (value != null || this.required) {
                                this.cachedFieldValue = desc;
                                registerDependentBeans(beanName, autowiredBeanNames);
                                if (autowiredBeanNames.size() == 1) {
                                    String autowiredBeanName = autowiredBeanNames.iterator().next();
                                    if (beanFactory.containsBean(autowiredBeanName)) {
                                        if (beanFactory.isTypeMatch(autowiredBeanName, field.getType())) {
                                            this.cachedFieldValue = new ShortcutDependencyDescriptor(
                                                    desc, autowiredBeanName, field.getType());
                                        }
                                    }
                                }
                            }
                            else {
                                this.cachedFieldValue = null;
                            }
                            this.cached = true;
                        }
                    }
                }
                if (value != null) {
                    ReflectionUtils.makeAccessible(field);
                    field.set(bean, value);
                }
            }
        }
    

      AutowiredMethodElement源码:

    
            @Override
            protected void inject(Object bean, String beanName, PropertyValues pvs) throws Throwable {
                if (checkPropertySkipping(pvs)) {
                    return;
                }
                Method method = (Method) this.member;
                Object[] arguments;
                if (this.cached) {
                    // Shortcut for avoiding synchronization...
                    arguments = resolveCachedArguments(beanName);
                }
                else {
                    Class<?>[] paramTypes = method.getParameterTypes();
                    arguments = new Object[paramTypes.length];
                    DependencyDescriptor[] descriptors = new DependencyDescriptor[paramTypes.length];
                    Set<String> autowiredBeans = new LinkedHashSet<String>(paramTypes.length);
                    TypeConverter typeConverter = beanFactory.getTypeConverter();
                    for (int i = 0; i < arguments.length; i++) {
                        MethodParameter methodParam = new MethodParameter(method, i);
                        DependencyDescriptor currDesc = new DependencyDescriptor(methodParam, this.required);
                        currDesc.setContainingClass(bean.getClass());
                        descriptors[i] = currDesc;
                        try {
                            Object arg = beanFactory.resolveDependency(currDesc, beanName, autowiredBeans, typeConverter);
                            if (arg == null && !this.required) {
                                arguments = null;
                                break;
                            }
                            arguments[i] = arg;
                        }
                        catch (BeansException ex) {
                            throw new UnsatisfiedDependencyException(null, beanName, new InjectionPoint(methodParam), ex);
                        }
                    }
                    synchronized (this) {
                        if (!this.cached) {
                            if (arguments != null) {
                                this.cachedMethodArguments = new Object[paramTypes.length];
                                for (int i = 0; i < arguments.length; i++) {
                                    this.cachedMethodArguments[i] = descriptors[i];
                                }
                                registerDependentBeans(beanName, autowiredBeans);
                                if (autowiredBeans.size() == paramTypes.length) {
                                    Iterator<String> it = autowiredBeans.iterator();
                                    for (int i = 0; i < paramTypes.length; i++) {
                                        String autowiredBeanName = it.next();
                                        if (beanFactory.containsBean(autowiredBeanName)) {
                                            if (beanFactory.isTypeMatch(autowiredBeanName, paramTypes[i])) {
                                                this.cachedMethodArguments[i] = new ShortcutDependencyDescriptor(
                                                        descriptors[i], autowiredBeanName, paramTypes[i]);
                                            }
                                        }
                                    }
                                }
                            }
                            else {
                                this.cachedMethodArguments = null;
                            }
                            this.cached = true;
                        }
                    }
                }
                if (arguments != null) {
                    try {
                        ReflectionUtils.makeAccessible(method);
                        method.invoke(bean, arguments);
                    }
                    catch (InvocationTargetException ex){
                        throw ex.getTargetException();
                    }
                }
            }
    
    

    相关文章

      网友评论

          本文标题:Spring IOC解析

          本文链接:https://www.haomeiwen.com/subject/zqtuiftx.html