[读书笔记]IOC容器的依赖注入详解

本文涉及的产品
全局流量管理 GTM,标准版 1个月
云解析 DNS,旗舰版 1个月
公共DNS(含HTTPDNS解析),每月1000万次HTTP解析
简介: [读书笔记]IOC容器的依赖注入详解

前面我们对IOC容器的初始化过程进行了详细的分析,这个初始化过程完成的主要工作是在IOC容器中建立BeanDefinition数据映射。但是在此过程并没有看到IOC容器对Bean依赖关系进行注入,接下来我们学习这个依赖注入。


假设当前IOC容器已经载入了用户定义的Bean信息,开始分析依赖注入的原理。首先,注意到依赖注入的过程是用户第一次向IOC容器所要bean时触发的,当然也有例外,也就是我们可以在BeanDefinition信息中通过控制lazy-init属性来让容器完成对bean的预实例化(非抽象、单例、lazy-init不为true就会预实例化)。这个预实例化实际上也是一个完成依赖注入的过程,但它是在初始化的过程中完成的。


当用户向IOC容器索要Bean时,会触发BeanFactory的getBean接口。这个接口的实现就是触发依赖注入发生的地方。我们从DefaultListableBeanFactory的基类AbstractBeanFactory去看看getBean的实现。


bb92326332c94e1eb457706fa3bd4852.png


当项目启动时,在初始化流程中的refresh-finishBeanFactoryInitialization就会对那些组件(比如controller、service等等)实例化并完成依赖注入。

【1】AbstractBeanFactory的doGetBean


该方法是实际取得Bean的方法,也是触发依赖注入发生的地方。

protected <T> T doGetBean(
    String name, @Nullable Class<T> requiredType, @Nullable Object[] args, boolean typeCheckOnly)
    throws BeansException {
  String beanName = transformedBeanName(name);
  Object bean;
//先从缓存中取得Bean,处理那些已经被创建过的单例模式的Bean,对这种Bean的请求不需要重复地创建
// DefaultSingletonBeanRegistry.getSingleton
  // Eagerly check singleton cache for manually registered singletons.
  Object sharedInstance = getSingleton(beanName);
  if (sharedInstance != null && args == null) {
    if (logger.isTraceEnabled()) {
      if (isSingletonCurrentlyInCreation(beanName)) {
        logger.trace("Returning eagerly cached instance of singleton bean '" + beanName +
            "' that is not fully initialized yet - a consequence of a circular reference");
      }
      else {
        logger.trace("Returning cached instance of singleton bean '" + beanName + "'");
      }
    }
    //这里完成的是FactoryBean的相关处理,以取得FactoryBean的生产结果
    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);
    }
// 这里对IOC容器中的BeanDefinition是否存在进行检查,检查是否能在当前的BeanFactory中取得需要的Bean。
//如果在当前的工厂中取不到,则到双亲BeanFactory中去取;如果当前的双亲工厂取不到,那就顺着双亲BeanFactory链一直向上查找
    // Check if bean definition exists in this factory.
    BeanFactory parentBeanFactory = getParentBeanFactory();
    if (parentBeanFactory != null && !containsBeanDefinition(beanName)) {
      // Not found -> check parent.
      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);
    }
    try {
    // 根据Bean的名字取得BeanDefinition
      RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName);
      checkMergedBeanDefinition(mbd, beanName, args);
//获取当前Bean的所有依赖Bean,这样会触发getBean的递归调用,直到取到一个没有任何依赖的Bean为止
      // 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);
          }
        }
      }
//这里通过调用createBean方法创建Singleton bean的实例
      // Create bean instance.
      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);
      }
// 这里是创建prototype bean的地方
      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 Bean
      else {
        String scopeName = mbd.getScope();
        if (!StringUtils.hasLength(scopeName)) {
          throw new IllegalStateException("No scope name defined for bean ´" + beanName + "'");
        }
        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;
    }
  }
// 对创建的Bean进行类型检查,如果没有问题就返回这个新创建的bean,这个Bean已经是包含了依赖关系的bean
  // 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;
}

重点来说,getBean是依赖注入的起点,之后会调用createBean。在这个过程中,Bean对象会依据BeanDefinition定义的要求生成,在AbstractAutowireCapableBeanFactory中实现了这个createBean,createBean不但生成了需要的Bean,还对Bean初始化进行处理,比如实现了在BeanDefinition中的init-method属性定义,Bean后置处理器等。


其中getObjectForBeanInstance方法将会返回当前bean实例或者工厂bean生产的bean(如果当前bean是一个FactoryBean的话)。

【2】AbstractAutowireCapableBeanFactory.createBean

该方法是AbstractAutowireCapableBeanFactory类的核心方法,其创建了一个Bean实例,为Bean实例属性赋值,触发Bean后置处理器。在AbstractBeanFactory的doGetBean会调用createBean方法。

@Override
protected Object createBean(String beanName, RootBeanDefinition mbd, @Nullable Object[] args)
    throws BeanCreationException {
  if (logger.isTraceEnabled()) {
    logger.trace("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.
  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配置了postProcessor,那么这里返回的是一个proxy
    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.isTraceEnabled()) {
      logger.trace("Finished creating instance of bean '" + beanName + "'");
    }
    return beanInstance;
  }
  catch (BeanCreationException | ImplicitlyAppearedSingletonException ex) {
    // A previously detected exception with proper bean creation context already,
    // or illegal singleton state to be communicated up to DefaultSingletonBeanRegistry.
    throw ex;
  }
  catch (Throwable ex) {
    throw new BeanCreationException(
        mbdToUse.getResourceDescription(), beanName, "Unexpected exception during bean creation", ex);
  }
}

我们继续看上面的核心方法doCreateBean(beanName, mbdToUse, args)

① doCreateBean

AbstractAutowireCapableBeanFactory.doCreateBean源码如下所示:

protected Object doCreateBean(final String beanName, final RootBeanDefinition mbd, final @Nullable Object[] args)
    throws BeanCreationException {
  // Instantiate the bean. 这个BeanWrapper是用来持有创建出来的Bean对象的
  BeanWrapper instanceWrapper = null;
  // 如果是Singleton,先把缓存中的同名Bean清除
  if (mbd.isSingleton()) {
    instanceWrapper = this.factoryBeanInstanceCache.remove(beanName);
  }
// 这里是创建Bean的地方,由createBeanInstance来完成
  if (instanceWrapper == null) {
    instanceWrapper = createBeanInstance(beanName, mbd, args);
  }
  final Object bean = instanceWrapper.getWrappedInstance();
  Class<?> beanType = instanceWrapper.getWrappedClass();
  if (beanType != NullBean.class) {
    mbd.resolvedTargetType = beanType;//设置BeanDefinition的BeanTYPE
  }
  // Allow post-processors to modify the merged bean definition.
  // 允许后置处理器修改BeanDefinition
  synchronized (mbd.postProcessingLock) {
    if (!mbd.postProcessed) {
      try {
      // 尝试调用MergedBeanDefinitionPostProcessor.postProcessMergedBeanDefinition
        applyMergedBeanDefinitionPostProcessors(mbd, beanType, beanName);
      }
      catch (Throwable ex) {
        throw new BeanCreationException(mbd.getResourceDescription(), beanName,
            "Post-processing of merged bean definition failed", ex);
      }
      mbd.postProcessed = true;//标明已经进行过bean的后置处理
    }
  }
  // Eagerly cache singletons to be able to resolve circular references
  // even when triggered by lifecycle interfaces like BeanFactoryAware.
  //急切地缓存单例,以便能够解析循环引用,即使是由BeanFactoryAware等生命周期接口触发
  boolean earlySingletonExposure = (mbd.isSingleton() && this.allowCircularReferences &&
      isSingletonCurrentlyInCreation(beanName));
  if (earlySingletonExposure) {
    if (logger.isTraceEnabled()) {
      logger.trace("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 {
    populateBean(beanName, mbd, instanceWrapper);
    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<>(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 {
  // 尝试作为DisposableBean 放入 private final Map<String, Object> disposableBeans = new LinkedHashMap<>();
    registerDisposableBeanIfNecessary(beanName, bean, mbd);
  }
  catch (BeanDefinitionValidationException ex) {
    throw new BeanCreationException(
        mbd.getResourceDescription(), beanName, "Invalid destruction signature", ex);
  }
  return exposedObject;
}


这里有三个核心方法:createBeanInstance、populateBean以及initializeBean。


在createBeanInstance中生成了Bean所包含的Java对象,这个对象的生成有很多种不同的方式。可以通过工厂方法生成,也可以通过容器的autowire特性生成,这些生成方式都是由相关的BeanDefinition来指定的。

② createBeanInstance

AbstractAutowireCapableBeanFactory.createBeanInstance,这里完成实例对象的创建。

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);
// 访问权限校验
  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 获取BeanName
  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;
      }
    }
  }
//是否曾经解析过/实例化过这个Bean
  if (resolved) {
    if (autowireNecessary) { 
      return autowireConstructor(beanName, mbd, null, null);
    }
    else {//使用默认的构造函数进行实例化
      return instantiateBean(beanName, mbd);
    }
  }
// AUTOWIRE_CONSTRUCTOR  表示根据构造函数进行依赖注入 
//自动连接可以满足的最贪婪的构造函数(包括解析适当的构造函数)
  // Candidate constructors for autowiring?
  Constructor<?>[] ctors = determineConstructorsFromBeanPostProcessors(beanClass, beanName);
  if (ctors != null || mbd.getResolvedAutowireMode() == AUTOWIRE_CONSTRUCTOR ||
      mbd.hasConstructorArgumentValues() || !ObjectUtils.isEmpty(args)) {
    return autowireConstructor(beanName, mbd, ctors, args);
  }
// 使用默认的构造函数进行实例化 -确定用于默认构造的首选构造函数(如果有)。如有必要,构造函数参数将自动连接。
// 默认为null
  // Preferred constructors for default construction?
  ctors = mbd.getPreferredConstructors();
  if (ctors != null) {
    return autowireConstructor(beanName, mbd, ctors, null);
  }
// 简单使用无参构造函数进行实例化
  // No special handling: simply use no-arg constructor.
  return instantiateBean(beanName, mbd);
}

我们接下来看一下其instantiateBean方法。

protected BeanWrapper instantiateBean(final String beanName, final RootBeanDefinition mbd) {
  try {
    Object beanInstance;
    final BeanFactory parent = this;
    if (System.getSecurityManager() != null) {
      beanInstance = AccessController.doPrivileged((PrivilegedAction<Object>) () ->
          getInstantiationStrategy().instantiate(mbd, beanName, parent),
          getAccessControlContext());
    }
    else {
    // 使用默认的实例化策略对Bean进行实例化,默认的实例化策略是CglibSubclassingInstantiationStrategy
    // 也就是使用CGLIB来对Bean进行实例化
      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);
  }
}


这里使用CGLIB对Bean进行实例化。CGLIB是一个常用的字节码生成器的类库,它提供了一系列的API来提供生成和转换Java的字节码的功能。在Spring AOP中也使用CGLIB对Java的字节码进行增强。在IOC容器中,要了解怎样使用CGLIB来生成Bean对象,需要看一下SimpleInstantiationStrategy其是CglibSubclassingInstantiationStrategy的父类。这个SimpleInstantiationStrategy是Spring用来生成Bean对象的默认类,它提供了两种实例化Java对象的方法:一种是通过BeanUtils,它使用了JVM的反射功能;另外一种则就是前面提到的的CGLIB来生成。

@Override
public Object instantiate(RootBeanDefinition bd, @Nullable String beanName, BeanFactory owner) {
  // Don't override the class with CGLIB if no overrides.
  if (!bd.hasMethodOverrides()) {
    Constructor<?> constructorToUse;
    synchronized (bd.constructorArgumentLock) {
    //这里取得指定的构造器或者生成对象的工厂方法来对Bean进行实例化
      constructorToUse = (Constructor<?>) bd.resolvedConstructorOrFactoryMethod;
      if (constructorToUse == null) {
        final Class<?> clazz = bd.getBeanClass();
        if (clazz.isInterface()) {
          throw new BeanInstantiationException(clazz, "Specified class is an interface");
        }
        try {
          if (System.getSecurityManager() != null) {
            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进行实例化,这个BeanUtils的实例化通过Constructor来实例化Bean
    // 在BeanUtils中可以看到具体的调用ctor.newInstance(args)
    return BeanUtils.instantiateClass(constructorToUse);
  }
  else {
    // Must generate CGLIB subclass.
    // 使用CGLIB来实例化对象
    return instantiateWithMethodInjection(bd, beanName, owner);
  }
}

到这里Bean对应已经实例化了,此时还只是一个基础模型。还需要Spring对Bean对象进行处理,把其依赖关系设置好,属性赋值等等完成整个依赖注入的过程。依赖关系处理的依据就是已经解析得到的BeanDefinition,而这个过程就是前面的populateBean(beanName, mbd, instanceWrapper)。

③ populateBean

用bean定义中的属性值填充给定BeanWrapper中的bean实例,也就是属性赋值过程。

protected void populateBean(String beanName, RootBeanDefinition mbd, @Nullable BeanWrapper bw) {
  if (bw == null) {
    if (mbd.hasPropertyValues()) {
      throw new BeanCreationException(
          mbd.getResourceDescription(), beanName, "Cannot apply property values to null instance");
    }
    else {
      // Skip property population phase for null instance.
      return;
    }
  }
// 给InstantiationAwareBeanPostProcessors 机会,(在实例化后,设置属性值前 )修改Bean的状态
  // 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.
  if (!mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) {
    for (BeanPostProcessor bp : getBeanPostProcessors()) {
      if (bp instanceof InstantiationAwareBeanPostProcessor) {
        InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp;
        if (!ibp.postProcessAfterInstantiation(bw.getWrappedInstance(), beanName)) {
          return;
        }
      }
    }
  }
// 取得BeanDefinition中设置的property值
  PropertyValues pvs = (mbd.hasPropertyValues() ? mbd.getPropertyValues() : null);
// 获取autowire标识, AUTOWIRE_BY_NAME  or AUTOWIRE_BY_TYPE
// 开始进行依赖注入的过程,先处理autowire的注入
// 如果resolvedAutowireMode为 0 ,则不会触发if逻辑
  int resolvedAutowireMode = mbd.getResolvedAutowireMode();
  if (resolvedAutowireMode == AUTOWIRE_BY_NAME || resolvedAutowireMode == AUTOWIRE_BY_TYPE) {
    MutablePropertyValues newPvs = new MutablePropertyValues(pvs);
    // Add property values based on autowire by name if applicable.
    if (resolvedAutowireMode == AUTOWIRE_BY_NAME) {
      autowireByName(beanName, mbd, bw, newPvs);
    }
    // Add property values based on autowire by type if applicable.
    if (resolvedAutowireMode == AUTOWIRE_BY_TYPE) {
      autowireByType(beanName, mbd, bw, newPvs);
    }
    pvs = newPvs;
  }
// 当前factory是否有InstantiationAwareBeanPostProcessor
  boolean hasInstAwareBpps = hasInstantiationAwareBeanPostProcessors();
// 是否需要依赖检查
  boolean needsDepCheck = (mbd.getDependencyCheck() != AbstractBeanDefinition.DEPENDENCY_CHECK_NONE);
  PropertyDescriptor[] filteredPds = null;
//如果有InstantiationAwareBeanPostProcessor--AutowiredAnnotationBeanPostProcessor就是其实现
  if (hasInstAwareBpps) {
    if (pvs == null) {
      pvs = mbd.getPropertyValues();
    }
    for (BeanPostProcessor bp : getBeanPostProcessors()) {
    // 尝试调用每一个InstantiationAwareBeanPostProcessor.postProcessProperties
    // postProcessProperties 使用属性值之前提供一个机会/窗口
      if (bp instanceof InstantiationAwareBeanPostProcessor) {
        InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp;
        PropertyValues pvsToUse = ibp.postProcessProperties(pvs, bw.getWrappedInstance(), beanName);
        if (pvsToUse == null) {
          if (filteredPds == null) {
            filteredPds = filterPropertyDescriptorsForDependencyCheck(bw, mbd.allowCaching);
          }
          pvsToUse = ibp.postProcessPropertyValues(pvs, filteredPds, bw.getWrappedInstance(), beanName);
          if (pvsToUse == null) {
            return;
          }
        }
        pvs = pvsToUse;
      }
    }
  }
  // 依赖检查
  if (needsDepCheck) {
    if (filteredPds == null) {
      filteredPds = filterPropertyDescriptorsForDependencyCheck(bw, mbd.allowCaching);
    }
    checkDependencies(beanName, mbd, filteredPds, pvs);
  }
// 对属性进行注入
  if (pvs != null) {
    applyPropertyValues(beanName, mbd, bw, pvs);
  }
}

这里需要注意,AutowiredAnnotationBeanPostProcessor是InstantiationAwareBeanPostProcessorAdapter的实现。如果resolvedAutowireMode为0,那么我们@Autowired注解的依赖解析就发生在这里。执行其postProcessProperties方法时会触发其inject方法,该方法会解析autowire注入依赖的。

7b7e742546e9442e8967378e7a1ebc9b.png


④ applyPropertyValues

AbstractAutowireCapableBeanFactoryapplyPropertyValues方法。应用给定的属性值,解析对该bean工厂中其他bean的任何运行时引用。必须使用深度复制,因此我们不会永久修改此属性。

protected void applyPropertyValues(String beanName, BeanDefinition mbd, BeanWrapper bw, PropertyValues pvs) {
// 为空直接返回
  if (pvs.isEmpty()) {
    return;
  }
  if (System.getSecurityManager() != null && bw instanceof BeanWrapperImpl) {
    ((BeanWrapperImpl) bw).setSecurityContext(getAccessControlContext());
  }
  MutablePropertyValues mpvs = null;
  List<PropertyValue> original;
// 检测是否为MutablePropertyValues类型
  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;
  }
//对BeanDefinition的解析是在这个valueResolver 中完成的
  BeanDefinitionValueResolver valueResolver = new BeanDefinitionValueResolver(this, beanName, mbd, converter);
  // Create a deep copy, resolving any references for values.
  //这里为解析值创建一个副本,副本的数据将会被注入到Bean中
  List<PropertyValue> deepCopy = new ArrayList<>(original.size());
  boolean resolveNecessary = false;
//下面这一部分是向deepCopy放入一个个PV,如果是未转化/解析过的,则进行解析
  for (PropertyValue pv : original) {
    if (pv.isConverted()) {
      deepCopy.add(pv);
    }
    else {
      String propertyName = pv.getName();
      Object originalValue = pv.getValue();
      if (originalValue == AutowiredPropertyMarker.INSTANCE) {
        Method writeMethod = bw.getPropertyDescriptor(propertyName).getWriteMethod();
        if (writeMethod == null) {
          throw new IllegalArgumentException("Autowire marker for property without write method: " + pv);
        }
        originalValue = new DependencyDescriptor(new MethodParameter(writeMethod, 0), true);
      }
      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 {
  // 这里是依赖注入发生的地方,会在BeanWrapperImpl中完成
    bw.setPropertyValues(new MutablePropertyValues(deepCopy));
  }
  catch (BeansException ex) {
    throw new BeanCreationException(
        mbd.getResourceDescription(), beanName, "Error setting property values", ex);
  }
}


这里我们看下value解析的过程。

⑤ resolveValueIfNecessary


BeanDefinitionValueResolver的resolveValueIfNecessary方法如下所示,其包含了所有对注入类型的处理。


给定一个PropertyValue,返回一个值,必要时解析对工厂中其他bean的任何引用。该值可以是:


一个BeanDefinition,它导致创建相应的新bean实例。这种“内部bean”的单例标志和名称总是被忽略:内部bean是匿名原型。

必须解析的RuntimeBeanReference。

ManagedList。这是一个特殊集合,可能包含RuntimeBeanReferences或需要解析的集合。

ManagedSet。还可能包含需要解析的RuntimeBeanReferences或集合。

ManagedMap。在这种情况下,该值可能是需要解析的RuntimeBeanReference或集合。

  • 一个普通的对象或 null,在这种情况下,它是单独存在的。
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.
  // RuntimeBeanReference是在对BeanDefinition进行解析时生成的数据对象
  if (value instanceof RuntimeBeanReference) { //判断RuntimeBeanReference
    RuntimeBeanReference ref = (RuntimeBeanReference) value;
    return resolveReference(argName, ref);
  }
  else if (value instanceof RuntimeBeanNameReference) { //判断RuntimeBeanNameReference
    String refName = ((RuntimeBeanNameReference) value).getBeanName();
    refName = String.valueOf(doEvaluate(refName));
    if (!this.beanFactory.containsBean(refName)) {
      throw new BeanDefinitionStoreException(
          "Invalid bean name '" + refName + "' in bean reference for " + argName);
    }
    return refName;
  }
  // 判断BeanDefinitionHolder
  else if (value instanceof BeanDefinitionHolder) {
    // Resolve BeanDefinitionHolder: contains BeanDefinition with name and aliases.
    BeanDefinitionHolder bdHolder = (BeanDefinitionHolder) value;
    return resolveInnerBean(argName, bdHolder.getBeanName(), bdHolder.getBeanDefinition());
  }
  // 判断BeanDefinition
  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);
  }
  // 判断DependencyDescriptor
  else if (value instanceof DependencyDescriptor) {
    Set<String> autowiredBeanNames = new LinkedHashSet<>(4);
    Object result = this.beanFactory.resolveDependency(
        (DependencyDescriptor) value, this.beanName, autowiredBeanNames, this.typeConverter);
    for (String autowiredBeanName : autowiredBeanNames) {
      if (this.beanFactory.containsBean(autowiredBeanName)) {
        this.beanFactory.registerDependentBean(autowiredBeanName, this.beanName);
      }
    }
    return result;
  }
  // 判断ManagedArray
  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);
        }
      }
      else {
        elementType = Object.class;
      }
    }
    return resolveManagedArray(argName, (List<?>) value, elementType);
  }
  // 判断ManagedList
  else if (value instanceof ManagedList) {
    // May need to resolve contained runtime references.
    return resolveManagedList(argName, (List<?>) value);
  }
  // 判断ManagedSet
  else if (value instanceof ManagedSet) {
    // May need to resolve contained runtime references.
    return resolveManagedSet(argName, (Set<?>) value);
  }
  // 判断 ManagedMap
  else if (value instanceof ManagedMap) {
    // May need to resolve contained runtime references.
    return resolveManagedMap(argName, (Map<?, ?>) value);
  }
  // 判断 ManagedProperties
  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;
  }
  // 判断TypedStringValue
  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);
      }
      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);
    }
  }
  // 是否为null
  else if (value instanceof NullBean) {
    return null;
  }
  // Evaluate the given value as an expression, if necessary
  else {
    return evaluate(value);
  }
}

⑥ resolveReference

对RuntimeBeanReference类型的注入解析在resolveReference方法中。

@Nullable
private Object resolveReference(Object argName, RuntimeBeanReference ref) {
  try {
    Object bean;
    Class<?> beanType = ref.getBeanType();
    // 如果ref是在双亲IOC容器中,那就到双亲IOC容器中去获取
    if (ref.isToParent()) {
      BeanFactory parent = this.beanFactory.getParentBeanFactory();
      if (parent == null) {
        throw new BeanCreationException(
            this.beanDefinition.getResourceDescription(), this.beanName,
            "Cannot resolve reference to bean " + ref +
                " in parent factory: no parent factory available");
      }
      if (beanType != null) {
        bean = parent.getBean(beanType);
      }
      else {
        bean = parent.getBean(String.valueOf(doEvaluate(ref.getBeanName())));
      }
    }
    // 在当前IOC容器中取得Bean,这里会触发一个getBean的过程
    // 如果依赖注入还没有发生,这里会触发相应的依赖注入的发生
    else {
      String resolvedName;
      if (beanType != null) { //根据type
        NamedBeanHolder<?> namedBean = this.beanFactory.resolveNamedBean(beanType);
        bean = namedBean.getBeanInstance();
        resolvedName = namedBean.getBeanName();
      }
      else { // 根据name
        resolvedName = String.valueOf(doEvaluate(ref.getBeanName()));
        bean = this.beanFactory.getBean(resolvedName);
      }
      // 这里很重要,会将当前bean名称与依赖bean放入map中
      this.beanFactory.registerDependentBean(resolvedName, this.beanName);
    }
    if (bean instanceof NullBean) {
      bean = null;
    }
    return bean;
  }
  catch (BeansException ex) {
    throw new BeanCreationException(
        this.beanDefinition.getResourceDescription(), this.beanName,
        "Cannot resolve reference to bean '" + ref.getBeanName() + "' while setting " + argName, ex);
  }
}



在完成这个解析过程后,已经为依赖注入准备好了条件。这是真正把Bean对象设置到它所依赖的另一个Bean的属性中去的地方其中处理的属性是各种各样的。


依赖注入的发生是在BeanWrapper的setPropertyValues中实现的,具体的完成却是在AbstractNestablePropertyAccessor中实现的。然后到这里就完成了对各种Bean属性的依赖注入过程。


在Bean的创建和对象依赖注入的过程中,需要依据BeanDefinition中的信息来递归地完成依赖注入。这些递归都是以getBean为入口的。一个递归是在上下文体系中查找需要的Bean和创建Bean的递归调用;另外一个递归是在依赖注入时,通过递归调用容器的getBean方法,得到当前Bean的依赖Bean,同时也触发对依赖bean的创建和注入。在对Bean的属性进行依赖注入时,解析的过程也是一个递归的过程。


这样,根据依赖关系,一层一层地完成Bean的创建和注入,直到最后完成当前Bean的创建。有了这个顶层Bean的创建和对它的属性依赖注入的完成,意味着和当前Bean相关的整个依赖链的注入也完成了。

⑦ initializeBean

关于该方法的详细信息参考博文:Spring中Bean实例化过程中的initializeBean方法

【4】自动依赖装配的实现


从autowiring使用上可以知道,这个autowiring属性在对Bean属性进行依赖注入时起作用。而这是在populateBean中实现的。也就是说,对属性autowiring的处理是populateBean的一部分。

PropertyValues pvs = (mbd.hasPropertyValues() ? mbd.getPropertyValues() : null);
int resolvedAutowireMode = mbd.getResolvedAutowireMode();
if (resolvedAutowireMode == AUTOWIRE_BY_NAME || resolvedAutowireMode == AUTOWIRE_BY_TYPE) {
  MutablePropertyValues newPvs = new MutablePropertyValues(pvs);
  // Add property values based on autowire by name if applicable.
  if (resolvedAutowireMode == AUTOWIRE_BY_NAME) {
    autowireByName(beanName, mbd, bw, newPvs);
  }
  // Add property values based on autowire by type if applicable.
  if (resolvedAutowireMode == AUTOWIRE_BY_TYPE) {
    autowireByType(beanName, mbd, bw, newPvs);
  }
  pvs = newPvs;
}

autowireByName 根据name从factory找到合适的bean

autowireByType 根据type从factory找到合适的bean

上面部分是更新PropertyValues pvs 的一个环节。以autowireByName 来说,它首先需要得到当前Bean的属性名,这些属性名已经在BeanWrapperImpl和BeanDefinition中封装好了,然后是对这一系列属性进行匹配的过程。在匹配的过程中,因为已经有了属性的名字,所以可以直接使用属性名作为Bean的名字向容器索取Bean,这个getBean会触发当前Bean的依赖Bean的依赖注入,从而得到属性对应的依赖Bean。在执行完这个getBean后,把这个依赖Bean注入到当前Bean的属性中去,这样就完成了通过这个依赖属性名自动完成依赖注入的过程。

protected void autowireByName(
    String beanName, AbstractBeanDefinition mbd, BeanWrapper bw, MutablePropertyValues pvs) {
  String[] propertyNames = unsatisfiedNonSimpleProperties(mbd, bw);
  for (String propertyName : propertyNames) {
    if (containsBean(propertyName)) {
      Object bean = getBean(propertyName);
      pvs.add(propertyName, bean);
      registerDependentBean(propertyName, beanName);
      if (logger.isTraceEnabled()) {
        logger.trace("Added autowiring by name from bean name '" + beanName +
            "' via property '" + propertyName + "' to bean named '" + propertyName + "'");
      }
    }
    else {
      if (logger.isTraceEnabled()) {
        logger.trace("Not autowiring property '" + propertyName + "' of bean '" + beanName +
            "' by name: no matching bean found");
      }
    }
  }
}

【5】Bean依赖的检查

在调用applyPropertyValues前,如果配置了依赖检查,则会调用checkDependencies方法。在这个方法中,会对Bean的Dependencies进行检查,如果发现不满足要求,就会抛出异常通知应用。

// AbstractAutowireCapableBeanFactory#checkDependencies
protected void checkDependencies(
    String beanName, AbstractBeanDefinition mbd, PropertyDescriptor[] pds, @Nullable PropertyValues pvs)
    throws UnsatisfiedDependencyException {
  int dependencyCheck = mbd.getDependencyCheck();
  for (PropertyDescriptor pd : pds) {
    if (pd.getWriteMethod() != null && (pvs == null || !pvs.contains(pd.getName()))) {
      boolean isSimple = BeanUtils.isSimpleProperty(pd.getPropertyType());
      boolean unsatisfied = (dependencyCheck == AbstractBeanDefinition.DEPENDENCY_CHECK_ALL) ||
          (isSimple && dependencyCheck == AbstractBeanDefinition.DEPENDENCY_CHECK_SIMPLE) ||
          (!isSimple && dependencyCheck == AbstractBeanDefinition.DEPENDENCY_CHECK_OBJECTS);
      if (unsatisfied) {
        throw new UnsatisfiedDependencyException(mbd.getResourceDescription(), beanName, pd.getName(),
            "Set this property value or disable dependency checking for this bean.");
      }
    }
  }
}


也就是说,在spring IOC容器中,设计了一个依赖检查特性,通过它spring可以帮助应用检查是否所有的属性都已经被正确设置。在具体使用的时候,应用只需要在Bean定义中设置dependency-check属性来指定依赖检查模式接口。如下所示,这里可以将属性设置为none、simple、object、all四种模式,默认的模式是none

//no dependency check at all.
public static final int DEPENDENCY_CHECK_NONE = 0;
// dependency checking for object references.
public static final int DEPENDENCY_CHECK_OBJECTS = 1;
// dependency checking for "simple" properties.
public static final int DEPENDENCY_CHECK_SIMPLE = 2;
//dependency checking for all properties
public static final int DEPENDENCY_CHECK_ALL = 3;


目录
相关文章
|
3月前
|
XML Java 数据格式
Spring5入门到实战------7、IOC容器-Bean管理XML方式(外部属性文件)
这篇文章是Spring5框架的实战教程,主要介绍了如何在Spring的IOC容器中通过XML配置方式使用外部属性文件来管理Bean,特别是数据库连接池的配置。文章详细讲解了创建属性文件、引入属性文件到Spring配置、以及如何使用属性占位符来引用属性文件中的值。
Spring5入门到实战------7、IOC容器-Bean管理XML方式(外部属性文件)
|
1月前
|
XML Java 数据格式
Spring IOC容器的深度解析及实战应用
【10月更文挑战第14天】在软件工程中,随着系统规模的扩大,对象间的依赖关系变得越来越复杂,这导致了系统的高耦合度,增加了开发和维护的难度。为解决这一问题,Michael Mattson在1996年提出了IOC(Inversion of Control,控制反转)理论,旨在降低对象间的耦合度,提高系统的灵活性和可维护性。Spring框架正是基于这一理论,通过IOC容器实现了对象间的依赖注入和生命周期管理。
71 0
|
2月前
|
XML Java 开发者
经典面试---spring IOC容器的核心实现原理
作为一名拥有十年研发经验的工程师,对Spring框架尤其是其IOC(Inversion of Control,控制反转)容器的核心实现原理有着深入的理解。
135 3
|
3月前
|
XML Java 数据格式
Spring5入门到实战------5、IOC容器-Bean管理(三)
这篇文章深入探讨了Spring5框架中IOC容器的高级Bean管理,包括FactoryBean的使用、Bean作用域的设置、Bean生命周期的详细解释以及Bean后置处理器的实现和应用。
Spring5入门到实战------5、IOC容器-Bean管理(三)
|
3月前
|
XML Java 数据格式
Spring5入门到实战------6、IOC容器-Bean管理XML方式(自动装配)
这篇文章是Spring5框架的入门教程,详细讲解了IOC容器中Bean的自动装配机制,包括手动装配、`byName`和`byType`两种自动装配方式,并通过XML配置文件和Java代码示例展示了如何在Spring中实现自动装配。
Spring5入门到实战------6、IOC容器-Bean管理XML方式(自动装配)
|
3月前
|
XML Java 数据格式
Spring5入门到实战------2、IOC容器底层原理
这篇文章深入探讨了Spring5框架中的IOC容器,包括IOC的概念、底层原理、以及BeanFactory接口和ApplicationContext接口的介绍。文章通过图解和实例代码,解释了IOC如何通过工厂模式和反射机制实现对象的创建和管理,以及如何降低代码耦合度,提高开发效率。
Spring5入门到实战------2、IOC容器底层原理
|
3月前
|
XML Java 数据格式
Spring5入门到实战------8、IOC容器-Bean管理注解方式
这篇文章详细介绍了Spring5框架中使用注解进行Bean管理的方法,包括创建Bean的注解、自动装配和属性注入的注解,以及如何用配置类替代XML配置文件实现完全注解开发。
Spring5入门到实战------8、IOC容器-Bean管理注解方式
|
13天前
|
Kubernetes Cloud Native Docker
云原生时代的容器化实践:Docker和Kubernetes入门
【10月更文挑战第37天】在数字化转型的浪潮中,云原生技术成为企业提升敏捷性和效率的关键。本篇文章将引导读者了解如何利用Docker进行容器化打包及部署,以及Kubernetes集群管理的基础操作,帮助初学者快速入门云原生的世界。通过实际案例分析,我们将深入探讨这些技术在现代IT架构中的应用与影响。
55 2
|
3天前
|
Kubernetes Linux 开发者
深入探索容器化技术——Docker 的实战应用
深入探索容器化技术——Docker 的实战应用
25 5
|
7天前
|
运维 Cloud Native 云计算
云原生之旅:Docker容器化实战
本文将带你走进云原生的世界,深入理解Docker技术如何改变应用部署与运维。我们将通过实际案例,展示如何利用Docker简化开发流程,提升应用的可移植性和伸缩性。文章不仅介绍基础概念,还提供操作指南和最佳实践,帮助你快速上手Docker,开启云原生的第一步。
下一篇
无影云桌面