spring学习(三) --- 创建bean

    上一节简单的介绍了下spring中bean的注册过程,就是解析配置文件,将bean的信息以BeanDefinition形式存放。这一节看一下bean的创建过程。其创建流程如下:

  1. 实例化bean
  2. 属性赋值
  3. 初始化
  4. 销毁

 

创建过程

    创建的主要逻辑在createBean()中:

protected Object createBean(String beanName, RootBeanDefinition mbd, Object[] args) throws BeanCreationException {
   if (logger.isDebugEnabled()) {
      logger.debug("Creating instance of bean '" + beanName + "'");
   }
   //bean的信息以BeanDefinition存放
   RootBeanDefinition mbdToUse = mbd;

   // 根据class属性或者beanName解析class
   Class<?> resolvedClass = resolveBeanClass(mbd, beanName);
   if (resolvedClass != null && !mbd.hasBeanClass() && mbd.getBeanClassName() != null) {
      mbdToUse = new RootBeanDefinition(mbd);
      mbdToUse.setBeanClass(resolvedClass);
   }

   // 处理lookup-method和replace-method方法,在创建BeanDefinition的时候,将这两个标签对应的信息存放在BeanDefinition中
   try {
      mbdToUse.prepareMethodOverrides();
   }
   catch (BeanDefinitionValidationException ex) {
      throw new BeanDefinitionStoreException(mbdToUse.getResourceDescription(),
            beanName, "Validation of method overrides failed", ex);
   }

   try {
      // BeanPostProcessors处理,在初始化前应用后置处理器,解析指定的bean是否存在初始化前的短路操作?
      Object bean = resolveBeforeInstantiation(beanName, mbdToUse);
      if (bean != null) {
         return bean;
      }
   }
   catch (Throwable ex) {
      throw new BeanCreationException(mbdToUse.getResourceDescription(), beanName,
            "BeanPostProcessor before instantiation of bean failed", ex);
   }
   //bean的创建
   Object beanInstance = doCreateBean(beanName, mbdToUse, args);
   if (logger.isDebugEnabled()) {
      logger.debug("Finished creating instance of bean '" + beanName + "'");
   }
   return beanInstance;
}

prepareMethodOverrides()

    这两个标签在解析BeanDefinition的时候,添加在BeanDefinition属性中(methodOverrides),表示需要覆盖的方法,所以创建之前对这个属性进行解析:

public void prepareMethodOverrides() throws BeanDefinitionValidationException {
   // 检查是否存在methodOverrides
   MethodOverrides methodOverrides = getMethodOverrides();
   if (!methodOverrides.isEmpty()) {
      Set<MethodOverride> overrides = methodOverrides.getOverrides();
      synchronized (overrides) {
         for (MethodOverride mo : overrides) {
            prepareMethodOverride(mo);
         }
      }
   }
}
protected void prepareMethodOverride(MethodOverride mo) throws BeanDefinitionValidationException {
   //获取可用方法的个数
   int count = ClassUtils.getMethodCountForName(getBeanClass(), mo.getMethodName());
   if (count == 0) {
      throw new BeanDefinitionValidationException(
            "Invalid method override: no method with name '" + mo.getMethodName() +
            "' on class [" + getBeanClassName() + "]");
   }
   else if (count == 1) {
      // 标记methodOverride未被覆盖,避免后续参数类型校验的开销。如果一个类中有多个重载的方法,则在调用的时候,
      //需要进行参数的解析,判断具体调用的方法,这是是进行预处理如果方法只有一个,那么就没有重载的方法,
      //后续的调用,直接使用该方法即可,不需要在解析参数类型了
      mo.setOverloaded(false);
   }
}

resolveBeforeInstantiation()

// 当经过前置处理之后,返回的结果若不为空,那么会直接略过后续的Bean的创建而直接返回结果。AOP功能再次判断
protected Object resolveBeforeInstantiation(String beanName, RootBeanDefinition mbd) {
   Object bean = null;
   if (!Boolean.FALSE.equals(mbd.beforeInstantiationResolved)) {
      // Make sure bean class is actually resolved at this point.
      if (!mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) {
         Class<?> targetType = determineTargetType(beanName, mbd);
         if (targetType != null) {
            //对后处理器中,所有InstantiationAwareBeanPostProcessor类型的后处理器进行
            //postProcessBeforeInstantiation方法和BeanPostProcessor的postProcessAfterInitialization方法的调用
            // bean的前置处理器
            bean = applyBeanPostProcessorsBeforeInstantiation(targetType, beanName);
            if (bean != null) {
               // bean的后置处理器
               bean = applyBeanPostProcessorsAfterInitialization(bean, beanName);
            }
         }
      }
      mbd.beforeInstantiationResolved = (bean != null);
   }
   return bean;
}

 

//经过以上这个方法后,bean可能已经不是我们认为的bean了,而是或许成为了一个经过处理的代理bean
protected Object applyBeanPostProcessorsBeforeInstantiation(Class<?> beanClass, String beanName) {
   for (BeanPostProcessor bp : getBeanPostProcessors()) {
      if (bp instanceof InstantiationAwareBeanPostProcessor) {
         InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp;
         Object result = ibp.postProcessBeforeInstantiation(beanClass, beanName);
         if (result != null) {
            return result;
         }
      }
   }
   return null;
}

创建bean实例createBean()

    创建bean的主要逻辑。

protected Object doCreateBean(final String beanName, final RootBeanDefinition mbd, final Object[] args)
      throws BeanCreationException {

   // 最终返回的 bean 的包装类
   BeanWrapper instanceWrapper = null;
   if (mbd.isSingleton()) {
      // 如果是单例,则检查工厂缓存中以前是否创建过
      instanceWrapper = this.factoryBeanInstanceCache.remove(beanName);
   }
   if (instanceWrapper == null) {
      //创建bean
      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 {
            //应用 MergedBeanDefinitionPostProcessors
            applyMergedBeanDefinitionPostProcessors(mbd, beanType, beanName);
         }
         catch (Throwable ex) {
            throw new BeanCreationException(mbd.getResourceDescription(), beanName,
                  "Post-processing of merged bean definition failed", ex);
         }
         mbd.postProcessed = true;
      }
   }

   // 检查循环依赖
   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");
      }
      //为了避免后期的循环依赖,在bean初始化完成前,将创建bean的工厂添加到缓存中,
      //如果其他的bean依赖该bean,直接从缓存中获取对应的工厂创建集合,解决循环依赖,注意是只有单例情况才能这么做
      addSingletonFactory(beanName, new ObjectFactory<Object>() {
         @Override
         public Object getObject() throws BeansException {
            return getEarlyBeanReference(beanName, mbd, bean);
         }
      });
   }

   // 初始化 bean
   Object exposedObject = bean;
   try {
      //填充属性
      populateBean(beanName, mbd, instanceWrapper);
      if (exposedObject != null) {
         //执行初始化方法,如 init-method 
         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) {
         //如果 exposedObject 在初始化方法中没有被改变,即没有被增强
         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.");
            }
         }
      }
   }

   // 注册
   try {
      registerDisposableBeanIfNecessary(beanName, bean, mbd);
   }
   catch (BeanDefinitionValidationException ex) {
      throw new BeanCreationException(
            mbd.getResourceDescription(), beanName, "Invalid destruction signature", ex);
   }

   return exposedObject;
}

 

createBeanInstance()

    创建bean的实例,根据不同的策略使用不同的创建方法,比如构建方法创造,工厂方法创造,默认的构造方法等。

protected BeanWrapper createBeanInstance(String beanName, RootBeanDefinition mbd, Object[] args) {
   // 获取bean对应的class
   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());
   }

   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;
         }
      }
   }
   if (resolved) {
      if (autowireNecessary) {
         //构造方法自动注入
         return autowireConstructor(beanName, mbd, null, null);
      }
      else {
         //默认构造方法自动注入
         return instantiateBean(beanName, mbd);
      }
   }

   // 解析构造方法
   Constructor<?>[] ctors = determineConstructorsFromBeanPostProcessors(beanClass, beanName);
   if (ctors != null ||
         mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_CONSTRUCTOR ||
         mbd.hasConstructorArgumentValues() || !ObjectUtils.isEmpty(args))  {
      //构造方法自动注入
      return autowireConstructor(beanName, mbd, ctors, args);
   }

   // 默认构造方法自动注入
   return instantiateBean(beanName, mbd);
}
    如果BeanDefinition中存在factoryMethodName属性,或者配置了factory-method,那么spring会尝试使用instantiateUsingFactoryMethod方法生成bean的实例。在解析构造函数并进行构造函数实例化的时候,因为一个bean可能对应多个构造函数,所以需要根据参数类型判断使用哪个构造函数进行实例化,判断过程是一个消耗性能的步骤,因此采用缓存机制,已经解析过的不需要再次解析,直接从beanDefinition的resolvedConstructorOrFactoryMethod中获取

    没有构造函数的构建方法十分简单,主要看带参数的构造方法注入,方法入参:
  • beanName:bean的名字
  • mbd:beanDefinition
  • chosenCtors:选择的构造器
  • explicitArgs:getBean方法中传入的参数,如果是none,则使用bean定义中的构造器参数。
public BeanWrapper autowireConstructor(final String beanName, final RootBeanDefinition mbd,
      Constructor<?>[] chosenCtors, final Object[] explicitArgs) {

   BeanWrapperImpl bw = new BeanWrapperImpl();
   this.beanFactory.initBeanWrapper(bw);

   Constructor<?> constructorToUse = null;
   ArgumentsHolder argsHolderToUse = null;
   Object[] argsToUse = null;

   if (explicitArgs != null) {
      //如果 getBean 方法传入了参数,则直接使用
      argsToUse = explicitArgs;
   }
   else {
      //如果 getBean 方法没有传入参数,则从配置文件进行解析
      Object[] argsToResolve = null;
      //从缓存中获取
      synchronized (mbd.constructorArgumentLock) {
         constructorToUse = (Constructor<?>) mbd.resolvedConstructorOrFactoryMethod;
         if (constructorToUse != null && mbd.constructorArgumentsResolved) {
            // 缓存中获取
            argsToUse = mbd.resolvedConstructorArguments;
            if (argsToUse == null) {
               //配置的构造函数参数
               argsToResolve = mbd.preparedConstructorArguments;
            }
         }
      }
      //缓存存在
      if (argsToResolve != null) {
         //配置的构造方法参数(解析参数),比如给定方法的构造函数是A(int,int),会把配置的(“1”,“1”)
         //解析成(1,1)
         argsToUse = resolvePreparedArguments(beanName, mbd, bw, constructorToUse, argsToResolve);
      }
   }
   //该构造没有被缓存,则需要进行解析
   if (constructorToUse == null) {
     
      boolean autowiring = (chosenCtors != null ||
            mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_CONSTRUCTOR);
      ConstructorArgumentValues resolvedValues = null;
      //参数个数
      int minNrOfArgs;
      if (explicitArgs != null) {
         minNrOfArgs = explicitArgs.length;
      }
      else {
         //获取配置文件中配置的构造方法参数
         ConstructorArgumentValues cargs = mbd.getConstructorArgumentValues();
         //解析后构造参数的值
         resolvedValues = new ConstructorArgumentValues();
        //获取参数个数
         minNrOfArgs = resolveConstructorArguments(beanName, mbd, bw, cargs, resolvedValues);
      }

      // Take specified constructors, if any.
      Constructor<?>[] candidates = chosenCtors;
      if (candidates == null) {
         Class<?> beanClass = mbd.getBeanClass();
         try {
            candidates = (mbd.isNonPublicAccessAllowed() ?
                  beanClass.getDeclaredConstructors() : beanClass.getConstructors());
         }
         catch (Throwable ex) {
            throw new BeanCreationException(mbd.getResourceDescription(), beanName,
                  "Resolution of declared constructors on bean Class [" + beanClass.getName() +
                  "] from ClassLoader [" + beanClass.getClassLoader() + "] failed", ex);
         }
      }
      //排序构造方法,按照参数个数进行排序:优先public和参数多的
      AutowireUtils.sortConstructors(candidates);
      int minTypeDiffWeight = Integer.MAX_VALUE;
      Set<Constructor<?>> ambiguousConstructors = null;
      LinkedList<UnsatisfiedDependencyException> causes = null;

      for (Constructor<?> candidate : candidates) {
         Class<?>[] paramTypes = candidate.getParameterTypes();

         if (constructorToUse != null && argsToUse.length > paramTypes.length) {
            //如果已经找到满足条件的构造方法,则终止
            break;
         }
         if (paramTypes.length < minNrOfArgs) {
           //参数个数不等
            continue;
         }

         ArgumentsHolder argsHolder;
         if (resolvedValues != null) {
            try {
               //构造方法参数的名字
               String[] paramNames = ConstructorPropertiesChecker.evaluate(candidate, paramTypes.length);
               if (paramNames == null) {
                  //如果参数名字为空,则获取参数名称探索器
                  ParameterNameDiscoverer pnd = this.beanFactory.getParameterNameDiscoverer();
                  if (pnd != null) {
                     //使用探索器获取对应构造方法的参数名称
                     paramNames = pnd.getParameterNames(candidate);
                  }
               }
               //根据参数名称和类型创建参数持有者
               argsHolder = createArgumentArray(beanName, mbd, resolvedValues, bw, paramTypes, paramNames,
                     getUserDeclaredConstructor(candidate), autowiring);
            }
            catch (UnsatisfiedDependencyException ex) {
               if (this.beanFactory.logger.isTraceEnabled()) {
                  this.beanFactory.logger.trace(
                        "Ignoring constructor [" + candidate + "] of bean '" + beanName + "': " + ex);
               }
               // Swallow and try next constructor.
               if (causes == null) {
                  causes = new LinkedList<UnsatisfiedDependencyException>();
               }
               causes.add(ex);
               continue;
            }
         }
         else {
            //构造方法没有参数
            if (paramTypes.length != explicitArgs.length) {
               continue;
            }
            argsHolder = new ArgumentsHolder(explicitArgs);
         }
          //检查是否有不确定性构造方法的存在,如参数为父子关系
         int typeDiffWeight = (mbd.isLenientConstructorResolution() ?
               argsHolder.getTypeDifferenceWeight(paramTypes) : argsHolder.getAssignabilityWeight(paramTypes));
         // Choose this constructor if it represents the closest match.
         if (typeDiffWeight < minTypeDiffWeight) {
            constructorToUse = candidate;
            argsHolderToUse = argsHolder;
            argsToUse = argsHolder.arguments;
            minTypeDiffWeight = typeDiffWeight;
            ambiguousConstructors = null;
         }
         else if (constructorToUse != null && typeDiffWeight == minTypeDiffWeight) {
            if (ambiguousConstructors == null) {
               ambiguousConstructors = new LinkedHashSet<Constructor<?>>();
               ambiguousConstructors.add(constructorToUse);
            }
            ambiguousConstructors.add(candidate);
         }
      }

      if (constructorToUse == null) {
         if (causes != null) {
            UnsatisfiedDependencyException ex = causes.removeLast();
            for (Exception cause : causes) {
               this.beanFactory.onSuppressedException(cause);
            }
            throw ex;
         }
         throw new BeanCreationException(mbd.getResourceDescription(), beanName,
               "Could not resolve matching constructor " +
               "(hint: specify index/type/name arguments for simple parameters to avoid type ambiguities)");
      }
      else if (ambiguousConstructors != null && !mbd.isLenientConstructorResolution()) {
         throw new BeanCreationException(mbd.getResourceDescription(), beanName,
               "Ambiguous constructor matches found in bean '" + beanName + "' " +
               "(hint: specify index/type/name arguments for simple parameters to avoid type ambiguities): " +
               ambiguousConstructors);
      }

      if (explicitArgs == null) {
         //将解析的构造方法加入缓存
         argsHolderToUse.storeCache(mbd, constructorToUse);
      }
   }

   try {
      Object beanInstance;
      //实例化
      if (System.getSecurityManager() != null) {
         final Constructor<?> ctorToUse = constructorToUse;
         final Object[] argumentsToUse = argsToUse;
         beanInstance = AccessController.doPrivileged(new PrivilegedAction<Object>() {
            @Override
            public Object run() {
               return beanFactory.getInstantiationStrategy().instantiate(
                     mbd, beanName, beanFactory, ctorToUse, argumentsToUse);
            }
         }, beanFactory.getAccessControlContext());
      }
      else {
         beanInstance = this.beanFactory.getInstantiationStrategy().instantiate(
               mbd, beanName, this.beanFactory, constructorToUse, argsToUse);
      }

      bw.setBeanInstance(beanInstance);
      return bw;
   }
   catch (Throwable ex) {
      throw new BeanCreationException(mbd.getResourceDescription(), beanName,
            "Bean instantiation via constructor failed", ex);
   }
}

    这个方法主要有以下几个功能点:

  • 构造参数确定:
    • 根据explicitArgs判断参数,如果不为空,直接确定,因为是在调用bean的时候指定的
    • 缓存中获取:如果之前分析过,那么会存在缓存中,但是需要经过类型转换器确保参数类型以及对应的构造函数参数类型
    • 配置文件获取
  • 构造函数确定:确定参数后,根据参数的格式进行匹配,所以需要对构造函数进行排序
  • 校验:有时候即使构造函数、参数名称、参数类型、参数值都确定后也不一定会直接锁定构造函数,做一次校验
  • 类型转换
  • 实例化bean

  根据实例化策略实例化bean:

public Object instantiate(RootBeanDefinition bd, String beanName, BeanFactory owner,
      final Constructor<?> ctor, Object... args) {
   //如果没有需要动态改变的方法,则直接使用反射进行实例化
   if (bd.getMethodOverrides().isEmpty()) {
      if (System.getSecurityManager() != null) {
         // use own privileged to change accessibility (when security is on)
         AccessController.doPrivileged(new PrivilegedAction<Object>() {
            @Override
            public Object run() {
               ReflectionUtils.makeAccessible(ctor);
               return null;
            }
         });
      } 
      //使用 反射 实例化
      return BeanUtils.instantiateClass(ctor, args);
   }
   else {
      //如果需要覆盖或者动态替换的方法,则需要使用 CGLIB 来动态代理,在创建代理的时候,可以增强植入我们的代码
      return instantiateWithMethodInjection(bd, beanName, owner, ctor, args);
   }
}

    就是先判断用户有没有使用replace和look-up方法,没有的话直接使用反射,如果有,需要将这两个配置的功能切入,必须使用动态代理的方式将这两个逻辑设置进去。

属性注入populateBean()

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;
      }
   }

   // 给 InstantiationAwareBeanPostProcessor 最后一次机会在属性设置前来改变 bean 

   if (!mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) {
      for (BeanPostProcessor bp : getBeanPostProcessors()) {
         if (bp instanceof InstantiationAwareBeanPostProcessor) {
            InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp;
           //postProcessAfterInstantiation 返回值为是否继续填充 bean 
            if (!ibp.postProcessAfterInstantiation(bw.getWrappedInstance(), beanName)) {
               continueWithPropertyPopulation = false;
               break;
            }
         }
      }
   }

   if (!continueWithPropertyPopulation) {
      return;
   }

   if (mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_BY_NAME ||
         mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_BY_TYPE) {
      MutablePropertyValues newPvs = new MutablePropertyValues(pvs);

      // 名称注入
      if (mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_BY_NAME) {
         autowireByName(beanName, mbd, bw, newPvs);
      }

      // 类型注入
      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) {
               InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp;
              //对所有需要依赖检查的属性进行后置处理
               pvs = ibp.postProcessPropertyValues(pvs, filteredPds, bw.getWrappedInstance(), beanName);
               if (pvs == null) {
                  return;
               }
            }
         }
      }
      if (needsDepCheck) {
         checkDependencies(beanName, mbd, filteredPds, pvs);
      }
   }
   //将属性应用到 bean 中
   applyPropertyValues(beanName, mbd, bw, pvs);
}

        其处理流程如下:

  1. InstantiationAwareBeanPostProcessor处理器进行postProcessAfterInstantiation,根据返回结果判断是否继续进行属性填充
  2. 根据类型或者名称提取依赖的bean,存在PropertyValues中
  3. InstantiationAwareBeanPostProcessor进行postProcessPropertyValues,在填充前进行再次处理
  4. 将所有的PropertyValues的属性填充到BeanWrapper中

applyPropertyValues

    我们看一下第四步的操作,到第四步的时候,已经完成了注入属性的获取,但是获取的属性是以PropertyValues存在的,没有应用到实例化的bean

protected void applyPropertyValues(String beanName, BeanDefinition mbd, BeanWrapper bw, PropertyValues pvs) {
   if (pvs == null || pvs.isEmpty()) {
      return;
   }

   if (System.getSecurityManager() != null && bw instanceof BeanWrapperImpl) {
      ((BeanWrapperImpl) bw).setSecurityContext(getAccessControlContext());
   }

   MutablePropertyValues mpvs = null;
   List<PropertyValue> original;

   if (pvs instanceof MutablePropertyValues) {
      mpvs = (MutablePropertyValues) pvs;
      //如果mpvs的值已经转换为对应的类型,可以直接设置在beanWrapper中
      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;
   }
   BeanDefinitionValueResolver valueResolver = new BeanDefinitionValueResolver(this, beanName, mbd, converter);

   // 遍历属性,并将配置的值转换为相应的类型
   List<PropertyValue> deepCopy = new ArrayList<PropertyValue>(original.size());
   boolean resolveNecessary = false;
   for (PropertyValue pv : original) {
      if (pv.isConverted()) {
         deepCopy.add(pv);
      }
      else {
         String propertyName = pv.getName();
         Object originalValue = pv.getValue();
         Object resolvedValue = valueResolver.resolveValueIfNecessary(pv, originalValue);
         Object convertedValue = resolvedValue;
         //是否能进行类型转换
         boolean convertible = bw.isWritableProperty(propertyName) &&
               !PropertyAccessorUtils.isNestedOrIndexedProperty(propertyName);
         if (convertible) {
            //将value转换为对应的类型
            convertedValue = convertForProperty(resolvedValue, propertyName, bw, converter);
         }
         // 为了避免每次创建bean的时候重复转换类型,将转换好的值放入对应的BeanDefinition中
         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();
   }

   // 将揭晓好的属性设置在BeanWrapper中
   try {
      bw.setPropertyValues(new MutablePropertyValues(deepCopy));
   }
   catch (BeansException ex) {
      throw new BeanCreationException(
            mbd.getResourceDescription(), beanName, "Error setting property values", ex);
   }
}

初始化initializeBean()

    

protected Object initializeBean(final String beanName, final Object bean, RootBeanDefinition mbd) {
   //执行定义Aware方法:比如BeanFactoryAware
   if (System.getSecurityManager() != null) {
      AccessController.doPrivileged(new PrivilegedAction<Object>() {
         @Override
         public Object run() {
            invokeAwareMethods(beanName, bean);
            return null;
         }
      }, getAccessControlContext());
   }
   else {
      invokeAwareMethods(beanName, bean);
   }

   Object wrappedBean = bean;
   if (mbd == null || !mbd.isSynthetic()) {
      //应用后处理器
      wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);
   }

   try {
     //激活用户定义的init-method
      invokeInitMethods(beanName, wrappedBean, mbd);
   }
   catch (Throwable ex) {
      throw new BeanCreationException(
            (mbd != null ? mbd.getResourceDescription() : null),
            beanName, "Invocation of init method failed", ex);
   }
   if (mbd == null || !mbd.isSynthetic()) {
      //执行后处理器
      wrappedBean = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);
   }
   return wrappedBean;
}

invokeAwareMethods   

    spring中有一些Aware接口,比如常用ApplicationContextAware等,实现这些Aware接口的bean在初始之前可以获取一下相应的资源,比如ApplicationContextAware,在bean初始化后,会注入ApplicationContext的实例。

private void invokeAwareMethods(final String beanName, final Object bean) {
   if (bean instanceof Aware) {
      if (bean instanceof BeanNameAware) {
         ((BeanNameAware) bean).setBeanName(beanName);
      }
      if (bean instanceof BeanClassLoaderAware) {
         ((BeanClassLoaderAware) bean).setBeanClassLoader(getBeanClassLoader());
      }
      if (bean instanceof BeanFactoryAware) {
         ((BeanFactoryAware) bean).setBeanFactory(AbstractAutowireCapableBeanFactory.this);
      }
   }
} 


  BeanPostProcessor

      BeanPostProcessor给用户权限去更改或者扩展spring,在调用用户自定义的初始化方法之前,以及调用初始化方法之后,会分别调用BeanPostProcessor的postProcessBeforeInitialization和postProcessAfterInitialization。

public Object applyBeanPostProcessorsBeforeInitialization(Object existingBean, String beanName)
      throws BeansException {

   Object result = existingBean;
   for (BeanPostProcessor beanProcessor : getBeanPostProcessors()) {
      result = beanProcessor.postProcessBeforeInitialization(result, beanName);
      if (result == null) {
         return result;
      }
   }
   return result;
}
public Object applyBeanPostProcessorsAfterInitialization(Object existingBean, String beanName)
      throws BeansException {

   Object result = existingBean;
   for (BeanPostProcessor beanProcessor : getBeanPostProcessors()) {
      result = beanProcessor.postProcessAfterInitialization(result, beanName);
      if (result == null) {
         return result;
      }
   }
   return result;
}

init-method    

    在配置bean的时候,我们经常会设置属性init-method,还有使用自定义的bean实现InitializingBean接口,在afterPropertiesSet中实现自定义的初始化逻辑。其中init-method和afterPropertiesSet都是在初始化bean时进行的,先执行afterPropertiesSet,在执行init-method。

protected void invokeInitMethods(String beanName, final Object bean, RootBeanDefinition mbd)
      throws Throwable {

   boolean isInitializingBean = (bean instanceof InitializingBean);
   //先检查initializingBean,判断需要调用afterPropertiesSet
   if (isInitializingBean && (mbd == null || !mbd.isExternallyManagedInitMethod("afterPropertiesSet"))) {
      if (logger.isDebugEnabled()) {
         logger.debug("Invoking afterPropertiesSet() on bean with name '" + beanName + "'");
      }
      if (System.getSecurityManager() != null) {
         try {
            AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {
               @Override
               public Object run() throws Exception {
                  ((InitializingBean) bean).afterPropertiesSet();
                  return null;
               }
            }, getAccessControlContext());
         }
         catch (PrivilegedActionException pae) {
            throw pae.getException();
         }
      }
      else {
          //属性初始化后的处理
         ((InitializingBean) bean).afterPropertiesSet();
      }
   }

   if (mbd != null) {
      String initMethodName = mbd.getInitMethodName();
      if (initMethodName != null && !(isInitializingBean && "afterPropertiesSet".equals(initMethodName)) &&
            !mbd.isExternallyManagedInitMethod(initMethodName)) {
         //调用自定义的初始化后方法init-method
         invokeCustomInitMethod(beanName, bean, mbd);
      }
   }
}

注册registerDisposableBeanIfNecessary

    spring中不但提供了对于初始化的扩展,还提供了销毁方法的扩展,对于销毁我们可以配置destroy-method或者注册DestructionAwareBeanPostProcessor来统一处理。

protected void registerDisposableBeanIfNecessary(String beanName, Object bean, RootBeanDefinition mbd) {
   AccessControlContext acc = (System.getSecurityManager() != null ? getAccessControlContext() : null);
   if (!mbd.isPrototype() && requiresDestruction(bean, mbd)) {
      if (mbd.isSingleton()) {
         // 单例模式下注册要销毁的bean,处理时限DisposableBean的bean,
         ///对所有bean使用DestructionAwareBeanPostProcessor
         registerDisposableBean(beanName,
               new DisposableBeanAdapter(bean, beanName, mbd, getBeanPostProcessors(), acc));
      }
      else {
         // 自定义scope处理
         Scope scope = this.scopes.get(mbd.getScope());
         if (scope == null) {
            throw new IllegalStateException("No Scope registered for scope name '" + mbd.getScope() + "'");
         }
         scope.registerDestructionCallback(beanName,
               new DisposableBeanAdapter(bean, beanName, mbd, getBeanPostProcessors(), acc));
      }
   }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值