springboot bean的实例化过程和属性注入过程

时间:2024-11-07 10:50:03

了解过springboot的加载流程的都知道springboot初始化bean都在refresh方法中。这个方法代码如下:

// Prepare this context for refreshing.
prepareRefresh();
// Tell the subclass to refresh the internal bean factory.新建beanFactory
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
// Prepare the bean factory for use in this context.
prepareBeanFactory(beanFactory);
try {
   // Allows post-processing of the bean factory in context subclasses.
   postProcessBeanFactory(beanFactory);
   // Invoke factory processors registered as beans in the context.
   // 加载实现beanFactoryPostProcessor的bean,bean定义的时候执行
   invokeBeanFactoryPostProcessors(beanFactory);
   // Register bean processors that intercept bean creation.
   // 加载实现了beanPostProcessor,在bean实例化前、后执行
   registerBeanPostProcessors(beanFactory);
   // Initialize message source for this context.
   initMessageSource();
   // Initialize event multicaster for this context.
   initApplicationEventMulticaster();
   // Initialize other special beans in specific context subclasses.
   onRefresh();
   // Check for listener beans and register them.
   registerListeners();
   // Instantiate all remaining (non-lazy-init) singletons.
   //实例化非懒加载的bean、bean封装、属性注入、注解注入(主要使用BeanPostProcessor或子类实现)等
   finishBeanFactoryInitialization(beanFactory);
   // Last step: publish corresponding event.
   finishRefresh();

这里我们主要看下finishBeanfactoryInitialization方法。此方法实现了bean的实例和属性注入等。进入此方法的最后一行。

// Instantiate all remaining (non-lazy-init) singletons.
();

注释写的很清楚,实例化剩余非懒加载的bean。此方法的实现是核心类DefaultLisListableBeanFactory实现。这个方法中有个判断:bean是否是beanFactory的实现类。如果是则获取bean的时候会从beanFactory实现类的getObject中获取,我们重点看看getBean这个方法。getBean是spring中最重要、最牛逼的方法之一, 具体的逻辑是通过doGetBean方法处理的。我们看下doGetBean方法,方法很长。我们分成几个部分去看。

1、先判断是否已经存在缓存中,代码如下:

if (sharedInstance != null && args == null) {
   if (logger.isDebugEnabled()) {
      if (isSingletonCurrentlyInCreation(beanName)) {
         logger.debug("Returning eagerly cached instance of singleton bean '" + beanName +
               "' that is not fully initialized yet - a consequence of a circular reference");
      }
      else {
         logger.debug("Returning cached instance of singleton bean '" + beanName + "'");
      }
   }
   bean = getObjectForBeanInstance(sharedInstance, name, beanName, null);

2、从父beanfactory中获取    

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) (nameToLookup, args);
   }
   else {
      // No args -> delegate to standard getBean method.
      return (nameToLookup, requiredType);
   }
}

3、直接创建RootBeanDefinition

//mark 给此bean 马克一下。防止重复创建
if (!typeCheckOnly) {
   markBeanAsCreated(beanName);
}

try {
   final RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName);
   checkMergedBeanDefinition(mbd, beanName, args);

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

4、是singleton还是prototype类型的,根据不同类型去实例化bean,代码只贴了单例的类型:

// Create bean instance.
if (()) {
   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);

5、检查bean的类型是否匹配

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

整个doGetBean方法改成这5个部分,重点看下第4个部分中的createBean和getObjectForBeanInstance方法。

1、createBean方法,里面主要是2个部分,bean直接是通过BeanPostProcessor的postProcessBeforeIntantiation方法获取的。注释也是描述的很清楚:Give BeanPostProcessors a chance to return a proxy instead of the bean instance(英语渣渣不敢瞎翻译误导大家,理解就好),代码如下:

try {
   // Give BeanPostProcessors a chance to return a proxy instead of the target bean instance.
   Object bean = resolveBeforeInstantiation(beanName, mbdToUse);
   if (bean != null) {
      return bean;
   }
}
catch (Throwable ex) {
   throw new BeanCreationException((), beanName,
         "BeanPostProcessor before instantiation of bean failed", ex);
}

第一个部分自己实现,那么createBean方法第2个部分毋庸置疑肯定是spring去实例化bean,代码如下:

try {
   Object beanInstance = doCreateBean(beanName, mbdToUse, args);
   if (logger.isDebugEnabled()) {
      logger.debug("Finished creating instance of bean '" + beanName + "'");
   }
   return beanInstance;
}

又是do开头的方法,说明这个也是一个创建方法,spring中一般do开头的都是用于创建某个对象。跟着代码走,看下doCreateBean方法,在查看此方法之前,可能需要了解下BeanWarpper这个封装类。bean的封装、属性注入等都是用BeanWarpper去完成的。看下代码:

// Instantiate the bean.
BeanWrapper instanceWrapper = null;
if (()) {
   instanceWrapper = this.factoryBeanInstanceCache.remove(beanName);
}
if (instanceWrapper == null) {
   instanceWrapper = createBeanInstance(beanName, mbd, args);
}

进入createBeanInstance方法中,就是调用构造器去实例化bean,返回beanWrapper。通过构造器获取实例代码如下:

// Need to determine the constructor...
Constructor<?>[] ctors = determineConstructorsFromBeanPostProcessors(beanClass, beanName);
if (ctors != null ||
      () == RootBeanDefinition.AUTOWIRE_CONSTRUCTOR ||
      () || !ObjectUtils.isEmpty(args))  {
   return autowireConstructor(beanName, mbd, ctors, args);
}

// No special handling: simply use no-arg constructor.
return instantiateBean(beanName, mbd);

这里实例化我一开始以为就是简单的反射,后面我点进去时候发现里面一直提策略实例化,我目前了解的是可以解决Override的问题等。如果有兴趣的可以自行查看。到这里为止。我们终于实例化bean了。下面看下第二步bean的属性注入等。代码如下:

Object exposedObject = bean;
try {
   populateBean(beanName, mbd, instanceWrapper);
   exposedObject = initializeBean(beanName, exposedObject, mbd);
}
catch (Throwable ex) {
   if (ex instanceof BeanCreationException && (((BeanCreationException) ex).getBeanName())) {
      throw (BeanCreationException) ex;
   }
   else {
      throw new BeanCreationException(
            (), beanName, "Initialization of bean failed", ex);
   }
}

populateBean方法名称就暴露他是干啥的:填充bean。我放一段比较重要的部分:

if (hasInstAwareBpps || needsDepCheck) {
   if (pvs == null) {
      pvs = ();
   }
   PropertyDescriptor[] filteredPds = filterPropertyDescriptorsForDependencyCheck(bw, mbd.allowCaching);
   if (hasInstAwareBpps) {
      for (BeanPostProcessor bp : getBeanPostProcessors()) {
         if (bp instanceof InstantiationAwareBeanPostProcessor) {
            InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp;
            pvs = (pvs, filteredPds, (), beanName);
            if (pvs == null) {
               return;
            }
         }
      }
   }
   if (needsDepCheck) {
      checkDependencies(beanName, mbd, filteredPds, pvs);
   }
}

这段代码貌似没有什么注入、填充什么的,我们看下InstantiationAwareBeanPostProcessor这个接口,发现这个接口有个很熟悉的实现类是:AutowiredAnnotationBeanPostProcessor。这个类的方法中我们看到终于看到了jnject方法。但是在inject之前需要调用下findAutowiringMatedata方法获取一下元数据:

// Fall back to class name as cache key, for backwards compatibility with custom callers.
String cacheKey = (StringUtils.hasLength(beanName) ? beanName : ());
// Quick check on the concurrent map first, with minimal locking.
InjectionMetadata metadata = this.injectionMetadataCache.get(cacheKey);
if (InjectionMetadata.needsRefresh(metadata, clazz)) {
   synchronized (this.injectionMetadataCache) {
      metadata = this.injectionMetadataCache.get(cacheKey);
      if (InjectionMetadata.needsRefresh(metadata, clazz)) {
         if (metadata != null) {
            (pvs);
         }
         metadata = buildAutowiringMetadata(clazz);
         this.injectionMetadataCache.put(cacheKey, metadata);
      }
   }
}
return metadata;

那么问题又来了,这个injectionMetadataCache集合值是从那里来的呢?AutowiredAnnotationBeanPostProcessor实现了MergeBeandefinitionPostProcessor,那么就好了,肯定是在bean实例的时候调用了postProcessMergedBeanDefintion这个方法。果然在doCreateBean方法中有这么一段:

// Allow post-processors to modify the merged bean definition.
synchronized (mbd.postProcessingLock) {
   if (!mbd.postProcessed) {
      try {
         //通过 BeanPostProcessor将需要注解的元数据 放到Map中
         applyMergedBeanDefinitionPostProcessors(mbd, beanType, beanName);
      }
      catch (Throwable ex) {
         throw new BeanCreationException((), beanName,
               "Post-processing of merged bean definition failed", ex);
      }
      mbd.postProcessed = true;
   }
}

哎,spring缜密啊。元数据有了,下面我们看下()方法是如何操作的:

if (this.isField) {
   Field field = (Field) this.member;
   ReflectionUtils.makeAccessible(field);
   (target, getResourceToInject(target, requestingBeanName));
}
else {
   if (checkPropertySkipping(pvs)) {
      return;
   }
   try {
      Method method = (Method) this.member;
      ReflectionUtils.makeAccessible(method);
      (target, getResourceToInject(target, requestingBeanName));
   }
   catch (InvocationTargetException ex) {
      throw ();
   }
}

方法简单明了,直接使用反射将值set到属性中,至此 bean的实例、属性注入基本完成,下面我们回头来看doGetBean的另一个方法getObjectForBeanInstance。

2、getObjectForBeanInstance方法。对于是FactoryBean类型的 bean通过getObject获取到bean的代理实例,跟着方法一直走下去会到getObejct()方法中。

if (System.getSecurityManager() != null) {
   AccessControlContext acc = getAccessControlContext();
   try {
      object = AccessController.doPrivileged((PrivilegedExceptionAction<Object>) factory::getObject, acc);
   }
   catch (PrivilegedActionException pae) {
      throw ();
   }
}
else {
   //从FactoryBean实现bean中getObejct获取到bean
   object = ();
}

到此 finishBeanfactoryInitialization方法执行结束!