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

简介: [读书笔记]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;


目录
相关文章
|
23天前
|
XML Java 数据格式
Spring IoC容器初始化过程(xml形式)
Spring IoC容器初始化过程(xml形式)
32 0
|
1月前
|
XML Java 数据格式
Spring5源码(15)-IoC容器启动过程简析及XmlBeanFactory初始化
Spring5源码(15)-IoC容器启动过程简析及XmlBeanFactory初始化
22 1
|
1月前
|
XML Java 数据格式
Spring5源码(12)-IoC容器简介
Spring5源码(12)-IoC容器简介
21 1
|
1月前
|
XML Java 数据格式
Spring之容器:IOC(3)
【1月更文挑战第14天】 11、实验十:bean的作用域 12、实验十一:bean生命周期 13、实验十二:FactoryBean 14、实验十三:基于xml自动装配
35 1
|
1月前
|
XML Java 数据格式
Spring之容器:IOC(2)
【1月更文挑战第14天】 7、实验六:为数组类型属性赋值 8、实验七:为集合类型属性赋值 ①为List集合类型属性赋值 ②为Map集合类型属性赋值 ③引用集合类型的bean 9、实验八:p命名空间 10、实验九:引入外部属性文件 7、实验六:为数组类型属性赋值 8、实验七:为集合类型属性赋值 ①为List集合类型属性赋值 ②为Map集合类型属性赋值 ③引用集合类型的bean 9、实验八:p命名空间 10、实验九:引入外部属性文件
40 2
|
1月前
|
XML Java 数据格式
Spring之容器:IOC(1)
【1月更文挑战第13天】 一、IoC容器 1、控制反转(IoC) 2、依赖注入 3、IoC容器在Spring的实现 二、基于XML管理Bean 1、搭建子模块spring6-ioc-xml 2、实验一:获取bean ①方式一:根据id获取 ②方式二:根据类型获取 ③方式三:根据id和类型 ④注意的地方 ⑤扩展知识 3、实验二:依赖注入之setter注入 4、实验三:依赖注入之构造器注入 5、实验四:特殊值处理 ①字面量赋值 ②null值 ③xml实体 ④CDATA节 6、实验五:为对象类型属性赋值 方式一:引用外部bean 方式二:内部bean 方式三:级联属性赋值
91 1
|
1月前
|
XML Java 数据格式
Spring之IOC容器(带你一篇文章搞定IOC)
Spring之IOC容器(带你一篇文章搞定IOC)
|
2天前
|
IDE 数据中心 Docker
使用PyCharm与Docker容器进行开发:从入门到精通
使用PyCharm与Docker容器进行开发:从入门到精通
|
24天前
|
Docker 容器
Docker学习笔记三:如何运行一个容器?
Docker学习笔记三:如何运行一个容器?
Docker学习笔记三:如何运行一个容器?
|
29天前
|
Java 持续交付 Docker
微服务框架(二十一)Piplin 持续部署 Docker 容器
此系列文章将会描述Java框架Spring Boot、服务治理框架Dubbo、应用容器引擎Docker,及使用Spring Boot集成Dubbo、Mybatis等开源框架,其中穿插着Spring Boot中日志切面等技术的实现,然后通过gitlab-CI以持续集成为Docker镜像。 本文为使用Piplin 持续部署 Docker 容器

相关产品

  • 云迁移中心