有以下几个疑问?

  1. 基于xml的方式我们知到,填充bean属性阶段将xml中定义的值填入。那么问题:@Autowire注解的属性何时注入?也是填充bean属性阶段

    答:是的属性填充都是在填充阶段,即 populateBean方法中进行的

  2. @Autowire@Resource等如何实现属性注入的呢?

    答:定义特殊的BeanPostProcessor,这些BeanPostProcessor是MergedBeanDefinitionPostProcessorInstantiationAwareBeanPostProcessor的子类在填充阶段就开始使用了。

  3. Spring中开启注解的配置是<context:annotation-config/> ,那它的作用是啥?

    这个配置的作用就是引入上面所说的特殊的BeanPostProcessor,来完成对注解的解析 主要有: AutowiredAnnotationBeanPostProcessor CommonAnnotationBeanPostProcessor PersistenceAnnotationBeanPostProcessor RequiredAnnotationBeanPostProcessor ———————————————— 版权声明:本文为CSDN博主「Beckio」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。 原文链接:https://blog.csdn.net/Beckio/article/details/79421335

调用途径解析

  1. 函数式接口,作为一个入参传入getSingleton()方法中 ```java @FunctionalInterface public interface ObjectFactory {

    /**

    • Return an instance (possibly shared or independent)
    • of the object managed by this factory.
    • @return the resulting instance
    • @throws BeansException in case of creation errors */ T getObject() throws BeansException;

}

  1. 2. `getSingleton`方法在方法内部,会调用这个lamda,实际进入`return createBean`这句
  2. ```java
  3. sharedInstance = getSingleton(beanName, () -> {
  4. //getSingleton 最终是触发这个 方法的一个调用
  5. try {
  6. //createBean
  7. return createBean(beanName, mbd, args);
  8. }
  9. catch (BeansException ex) {
  10. // Explicitly remove instance from singleton cache: It might have been put there
  11. // eagerly by the creation process, to allow for circular reference resolution.
  12. // Also remove any beans that received a temporary reference to the bean.
  13. destroySingleton(beanName);
  14. throw ex;
  15. }
  16. });
  1. createBean方法最后进入doCreateBean就是整个Bean的创建阶段的生命周期

生命周期-创建阶段

  1. protected Object doCreateBean(String beanName, RootBeanDefinition mbd, @Nullable Object[] args)
  2. throws BeanCreationException {
  3. // Instantiate the bean.
  4. BeanWrapper instanceWrapper = createBeanInstance(beanName, mbd, args);
  5. addSingletonFactory(beanName, () -> getEarlyBeanReference(beanName, mbd, bean));
  6. populateBean(beanName, mbd, instanceWrapper);
  7. exposedObject = initializeBean(beanName, exposedObject, mbd);
  8. //判断提前引用出去的对象,是否有地址上的变化
  9. if (earlySingletonExposure) {
  10. }
  11. //注册实现了DisposableBean的bean
  12. registerDisposableBeanIfNecessary(beanName, bean, mbd);
  13. return exposedObject;
  14. }
  1. 实例化bean对象
  2. 提前暴露bean对象在三级缓存中
  3. 填充属性
  4. 初始化bean

    1. protected Object initializeBean(String beanName, Object bean, @Nullable RootBeanDefinition mbd) {
    2. invokeAwareMethods(beanName, bean);
    3. wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);
    4. invokeInitMethods(beanName, wrappedBean, mbd);
    5. applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);
    6. return wrappedBean;
    7. }
    • 调用Aware接口的实现方法
    • 调用BeanPostProcessorpostProcessBeforeInitialization方法

      1. public Object applyBeanPostProcessorsBeforeInitialization(Object existingBean, String beanName)
      2. throws BeansException {
      3. Object result = existingBean;
      4. for (BeanPostProcessor processor : getBeanPostProcessors()) {
      5. Object current =
      6. processor.postProcessBeforeInitialization(result, beanName);
      7. if (current == null) {
      8. return result;
      9. }
      10. result = current;
      11. }
      12. return result;
      13. }
    • 调用初始化方法

    • 调用BeanPostProcessorpostProcessAfterInitialization方法

      1. @Override
      2. public Object applyBeanPostProcessorsAfterInitialization(Object existingBean, String beanName)
      3. throws BeansException {
      4. Object result = existingBean;
      5. for (BeanPostProcessor processor : getBeanPostProcessors()) {
      6. Object current = processor.postProcessAfterInitialization(result, beanName);
      7. if (current == null) {
      8. return result;
      9. }
      10. result = current;
      11. }
      12. return result;
      13. }
  5. 判断提前被引用的bean对象是否有变化

  6. 若实现了DisposableBean则注册一下