IOC容器的初始化过程

1.ClassPathXmlApplicationContext类体系结构

左边的黄色部分是ApplicationContext体系继承结构,右边是BeanFactory结构体系,两个体系是典型的模板方法设计模式的使用。

从该继承体系可以看出:

(1)BeanFactory是一个bean工厂的最基本定义,里面包含了一个bean工厂的几个最基本方法:getBean(),containsBean()等,是一个很纯粹的bean工厂,不关注资源、资源位置、事件等。

ApplicationContext是一个容器的最基本接口定义,它继承了BeanFactory,拥有工厂的基本方法。同时继承了ApplicationEventPublisher、MessageSource、ResourcePatternResolver等接口,使其定义了一些额外的功能,如事件、资源等这些额外功能。

[java] view plain copy

  1. public interface ApplicationContext extends EnvironmentCapable, ListableBeanFactory, HierarchicalBeanFactory,
  2. MessageSource, ApplicationEventPublisher, ResourcePatternResolver

[java] view plain copy

  1. public interface HierarchicalBeanFactory extends BeanFactory

(2)AbstractBeanFactory和AbstractAutowireCapableBeanFactory是两个模板抽象工厂类。AbstractBeanFactory提供了bean工厂的抽象基类,同时提供了ConfigurableBeanFactory的完整实现。AbstractAutowireapableBeanFactory是继承了AbstractBeanFactory的抽象工厂,里面提供了bean创建的支持,包括bean的创建、依赖注入、检查等功能,是一个核心的bean工厂类。

(3)ClassPathXmlApplicationContext之所以拥有bean工厂的功能是通过持有一个真正的bean工厂DefaultListableBeanFactory的实例,并通过代理该工厂完成

[java] view plain copy

  1. public class ClassPathXmlApplicationContext extends AbstractXmlApplicationContext

[java] view plain copy

  1. public abstract class AbstractXmlApplicationContext extends AbstractRefreshableConfigApplicationContext

[java] view plain copy

  1. public abstract class AbstractRefreshableConfigApplicationContext extends AbstractRefreshableApplicationContext
  2. implements BeanNameAware, InitializingBean

[java] view plain copy

  1. public abstract class AbstractRefreshableApplicationContext extends AbstractApplicationContext {
  2. .........
  3. /** Bean factory for this context */
  4. private DefaultListableBeanFactory beanFactory;

(4)ClassPathXmlApplicationContext的初始化过程是对本身容器的初始化,同时也是对其持有的DefaultListableBeanFactory的初始化

2.容器初始化过程

整个过程可以通过下图来表示

从xml文件到BeanDefinition的过程是ApplicationContext的职责范围,从BeanDefinition Map到Bean是BeanFactory的职责。可以看出ApplicationContext是一个运行时的容器需要提供不同资源的支持,屏蔽不同环境的差异化。而BeanDefinition是内部关于bean定义的基本结构,bean的创建就是基于它。

整个容器的初始化是通过调用refresh()方法来实现的,该方法定义在AbstractApplicationContext接口中。AbstractApplicationContext是容器的最基础的一个抽象父类。也就是说在里面定义了一个容器初始化的基本流程。

[java] view plain copy

  1. public void refresh() throws BeansException, IllegalStateException {
  2. synchronized (this.startupShutdownMonitor) {
  3. // Prepare this context for refreshing.
  4. prepareRefresh();
  5. // Tell the subclass to refresh the internal bean factory.
  6. ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
  7. // Prepare the bean factory for use in this context.
  8. prepareBeanFactory(beanFactory);
  9. try {
  10. // Allows post-processing of the bean factory in context subclasses.
  11. postProcessBeanFactory(beanFactory);
  12. // Invoke factory processors registered as beans in the context.
  13. invokeBeanFactoryPostProcessors(beanFactory);
  14. // Register bean processors that intercept bean creation.
  15. registerBeanPostProcessors(beanFactory);
  16. // Initialize message source for this context.
  17. initMessageSource();
  18. // Initialize event multicaster for this context.
  19. initApplicationEventMulticaster();
  20. // Initialize other special beans in specific context subclasses.
  21. onRefresh();
  22. // Check for listener beans and register them.
  23. registerListeners();
  24. // Instantiate all remaining (non-lazy-init) singletons.
  25. finishBeanFactoryInitialization(beanFactory);
  26. // Last step: publish corresponding event.
  27. finishRefresh();
  28. }
  29. catch (BeansException ex) {
  30. logger.warn("Exception encountered during context initialization - cancelling refresh attempt", ex);
  31. // Destroy already created singletons to avoid dangling resources.
  32. destroyBeans();
  33. // Reset ‘active‘ flag.
  34. cancelRefresh(ex);
  35. // Propagate exception to caller.
  36. throw ex;
  37. }
  38. }
  39. }

解释如下:

3.Bean的创建过程

Bean的创建过程是BeanFactory索要完成的事情

(1)先看一下BeanDefinition的定义

从接口定义可以看出,通过bean定义能够bean的详细信息,如类名称、工厂类名称、scope、是否单例、是否抽象、是否延迟加载等等。

(2)bean的创建时机

容器初始化的时候,会先对单例和非延迟加载的对象进行预先初始化。其他的都是延迟加载,是在第一次调用getBean的时候被创建。从DefaultListableBeanFactory的preInstantiateSingletons()里可以看到这样的实现规则

[java] view plain copy

  1. public void preInstantiateSingletons() throws BeansException
  2. {
  3. if (this.logger.isInfoEnabled())
  4. {
  5. this.logger.info("Pre-instantiating singletons in " + this);
  6. }
  7. List<String> beanNames;
  8. synchronized (this.beanDefinitionMap)
  9. {
  10. // Iterate over a copy to allow for init methods which in turn register new bean definitions.
  11. // While this may not be part of the regular factory bootstrap, it does otherwise work fine.
  12. beanNames = new ArrayList<String>(this.beanDefinitionNames);
  13. }
  14. // Trigger initialization of all non-lazy singleton beans...
  15. for (String beanName : beanNames)
  16. {
  17. RootBeanDefinition bd = getMergedLocalBeanDefinition(beanName);
  18. if (!bd.isAbstract() && bd.isSingleton() && !bd.isLazyInit()) //对非抽象、单例、非延迟加载的对象先实例化
  19. {
  20. if (isFactoryBean(beanName))
  21. {
  22. final FactoryBean<?> factory = (FactoryBean<?>) getBean(FACTORY_BEAN_PREFIX + beanName);
  23. boolean isEagerInit;
  24. if (System.getSecurityManager() != null && factory instanceof SmartFactoryBean)
  25. {
  26. isEagerInit = AccessController.doPrivileged(new PrivilegedAction<Boolean>() {
  27. public Boolean run()
  28. {
  29. return ((SmartFactoryBean<?>) factory).isEagerInit();
  30. }
  31. }, getAccessControlContext());
  32. }
  33. else
  34. {
  35. isEagerInit = (factory instanceof SmartFactoryBean &&
  36. ((SmartFactoryBean<?>) factory).isEagerInit());
  37. }
  38. if (isEagerInit)
  39. {
  40. getBean(beanName);
  41. }
  42. }
  43. else
  44. {
  45. getBean(beanName);
  46. }
  47. }
  48. }
  49. }

从上面的代码看,如果用以下配置,只有singletonBean会被预先创建

[html] view plain copy

  1. <?xml version="1.0" encoding="GB2312"?>
  2. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
  3. <beans default-autowire="byName">
  4. <bean id="otherBean"          class="com.test.OtherBean" scope="prototype"/>
  5. <bean id="myBean"          class="com.test.MyBean" lazy-init="true"/>
  6. <bean id="singletonBean"          class="com.test.SingletonBean"/>
  7. </beans>

(3)bean的创建过程

对于bean的创建过程,其实都是通过调用工厂的getBean方法来完成的。在这个方法中会完成对构造函数的选择、依赖注入等。

无论预先创建还是延迟加载都是调用getBean来实现,AbstractBeanFactory定义了getBean的过程:

[java] view plain copy

  1. public Object getBean(String name) throws BeansException {
  2. return doGetBean(name, null, null, false);
  3. }

再看doGetBean()方法

[java] view plain copy

  1. protected <T> T doGetBean(
  2. final String name, final Class<T> requiredType, final Object[] args, boolean typeCheckOnly)
  3. throws BeansException
  4. {
  5. final String beanName = transformedBeanName(name);
  6. Object bean;
  7. // Eagerly check singleton cache for manually registered singletons.
  8. Object sharedInstance = getSingleton(beanName);
  9. if (sharedInstance != null && args == null)
  10. {
  11. if (logger.isDebugEnabled())
  12. {
  13. if (isSingletonCurrentlyInCreation(beanName))
  14. {
  15. logger.debug("Returning eagerly cached instance of singleton bean ‘" + beanName +
  16. "‘ that is not fully initialized yet - a consequence of a circular reference");
  17. }
  18. else
  19. {
  20. logger.debug("Returning cached instance of singleton bean ‘" + beanName + "‘");
  21. }
  22. }
  23. bean = getObjectForBeanInstance(sharedInstance, name, beanName, null);
  24. }
  25. else
  26. {
  27. // Fail if we‘re already creating this bean instance:
  28. // We‘re assumably within a circular reference.
  29. if (isPrototypeCurrentlyInCreation(beanName))
  30. {
  31. throw new BeanCurrentlyInCreationException(beanName);
  32. }
  33. // Check if bean definition exists in this factory.
  34. BeanFactory parentBeanFactory = getParentBeanFactory();
  35. if (parentBeanFactory != null && !containsBeanDefinition(beanName))
  36. {
  37. // Not found -> check parent.
  38. String nameToLookup = originalBeanName(name);
  39. if (args != null)
  40. {
  41. // Delegation to parent with explicit args.
  42. return (T) parentBeanFactory.getBean(nameToLookup, args);
  43. }
  44. else
  45. {
  46. // No args -> delegate to standard getBean method.
  47. return parentBeanFactory.getBean(nameToLookup, requiredType);
  48. }
  49. }
  50. if (!typeCheckOnly)
  51. {
  52. markBeanAsCreated(beanName);
  53. }
  54. try
  55. {
  56. final RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName);
  57. checkMergedBeanDefinition(mbd, beanName, args);
  58. // Guarantee initialization of beans that the current bean depends on.
  59. String[] dependsOn = mbd.getDependsOn();
  60. if (dependsOn != null)
  61. {
  62. for (String dependsOnBean : dependsOn)
  63. {
  64. getBean(dependsOnBean);
  65. registerDependentBean(dependsOnBean, beanName);
  66. }
  67. }
  68. // Create bean instance.
  69. if (mbd.isSingleton())              //如果是单例,间接通过getSingleton方法来创建,里面会实现将单例对象缓存
  70. {
  71. sharedInstance = getSingleton(beanName, new ObjectFactory<Object>() {
  72. public Object getObject() throws BeansException
  73. {
  74. try
  75. {
  76. return createBean(beanName, mbd, args);
  77. }
  78. catch (BeansException ex)
  79. {
  80. // Explicitly remove instance from singleton cache: It might have been put there
  81. // eagerly by the creation process, to allow for circular reference resolution.
  82. // Also remove any beans that received a temporary reference to the bean.
  83. destroySingleton(beanName);
  84. throw ex;
  85. }
  86. }
  87. });
  88. bean = getObjectForBeanInstance(sharedInstance, name, beanName, mbd);
  89. }
  90. else if (mbd.isPrototype())                 //非单例对象
  91. {
  92. // It‘s a prototype -> create a new instance.
  93. Object prototypeInstance = null;
  94. try
  95. {
  96. beforePrototypeCreation(beanName);
  97. prototypeInstance = createBean(beanName, mbd, args);
  98. }
  99. finally
  100. {
  101. afterPrototypeCreation(beanName);
  102. }
  103. bean = getObjectForBeanInstance(prototypeInstance, name, beanName, mbd);
  104. }
  105. else
  106. {
  107. String scopeName = mbd.getScope();
  108. final Scope scope = this.scopes.get(scopeName);
  109. if (scope == null)
  110. {
  111. throw new IllegalStateException("No Scope registered for scope ‘" + scopeName + "‘");
  112. }
  113. try
  114. {
  115. Object scopedInstance = scope.get(beanName, new ObjectFactory<Object>() {
  116. public Object getObject() throws BeansException
  117. {
  118. beforePrototypeCreation(beanName);
  119. try
  120. {
  121. return createBean(beanName, mbd, args);
  122. }
  123. finally
  124. {
  125. afterPrototypeCreation(beanName);
  126. }
  127. }
  128. });
  129. bean = getObjectForBeanInstance(scopedInstance, name, beanName, mbd);
  130. }
  131. catch (IllegalStateException ex)
  132. {
  133. throw new BeanCreationException(beanName,
  134. "Scope ‘" + scopeName + "‘ is not active for the current thread; " +
  135. "consider defining a scoped proxy for this bean if you intend to refer to it from a singleton",
  136. ex);
  137. }
  138. }
  139. }
  140. catch (BeansException ex)
  141. {
  142. cleanupAfterBeanCreationFailure(beanName);
  143. throw ex;
  144. }
  145. }
  146. // Check if required type matches the type of the actual bean instance.
  147. if (requiredType != null && bean != null && !requiredType.isAssignableFrom(bean.getClass()))
  148. {
  149. try
  150. {
  151. return getTypeConverter().convertIfNecessary(bean, requiredType);
  152. }
  153. catch (TypeMismatchException ex)
  154. {
  155. if (logger.isDebugEnabled())
  156. {
  157. logger.debug("Failed to convert bean ‘" + name + "‘ to required type [" +
  158. ClassUtils.getQualifiedName(requiredType) + "]", ex);
  159. }
  160. throw new BeanNotOfRequiredTypeException(name, requiredType, bean.getClass());
  161. }
  162. }
  163. return (T) bean;
  164. }

doGetBean的大概过程

1)先试着从单例缓存中获取对象

2)从父容器中取定义,有则由父容器创建

3)如果是单例,则走单例对象的创建过程:在spring容器中单例对象和非单例对象的创建过程是一样的。都会调用父类AbstractAutowireCapableBeanFactory的createBean()方法。不同的是单例对象之创建一次并且需要缓存起来。DefaultListableBeanFactory的父类DefaultSingletonBeanRegistry提供了对单例对象缓存等支持工作。所以是单例对象的话,会调用DefaultSingletonBeanRegistry的getSingleton()方法,它会间接调用AbstractAutowireCapableBeanFactory的createBean()方法。

bean的创建是由AbstractAutowireCapableBeanFactory来定义

[java] view plain copy

  1. /**
  2. * Central method of this class: creates a bean instance,
  3. * populates the bean instance, applies post-processors, etc.
  4. * @see #doCreateBean
  5. */
  6. @Override
  7. protected Object createBean(final String beanName, final RootBeanDefinition mbd, final Object[] args)
  8. throws BeanCreationException
  9. {
  10. if (logger.isDebugEnabled())
  11. {
  12. logger.debug("Creating instance of bean ‘" + beanName + "‘");
  13. }
  14. // Make sure bean class is actually resolved at this point.
  15. resolveBeanClass(mbd, beanName);
  16. // Prepare method overrides.
  17. try
  18. {
  19. mbd.prepareMethodOverrides();
  20. }
  21. catch (BeanDefinitionValidationException ex)
  22. {
  23. throw new BeanDefinitionStoreException(mbd.getResourceDescription(),
  24. beanName, "Validation of method overrides failed", ex);
  25. }
  26. try
  27. {
  28. // Give BeanPostProcessors a chance to return a proxy instead of the target bean instance.
  29. Object bean = resolveBeforeInstantiation(beanName, mbd);
  30. if (bean != null)
  31. {
  32. return bean;
  33. }
  34. }
  35. catch (Throwable ex)
  36. {
  37. throw new BeanCreationException(mbd.getResourceDescription(), beanName,
  38. "BeanPostProcessor before instantiation of bean failed", ex);
  39. }
  40. Object beanInstance = doCreateBean(beanName, mbd, args);
  41. if (logger.isDebugEnabled())
  42. {
  43. logger.debug("Finished creating instance of bean ‘" + beanName + "‘");
  44. }
  45. return beanInstance;
  46. }

createBean会调用doCreateBean方法

[java] view plain copy

  1. /**
  2. * Actually create the specified bean. Pre-creation processing has already happened
  3. * at this point, e.g. checking {@code postProcessBeforeInstantiation} callbacks.
  4. * <p>Differentiates between default bean instantiation, use of a
  5. * factory method, and autowiring a constructor.
  6. * @param beanName the name of the bean
  7. * @param mbd the merged bean definition for the bean
  8. * @param args arguments to use if creating a prototype using explicit arguments to a
  9. * static factory method. This parameter must be {@code null} except in this case.
  10. * @return a new instance of the bean
  11. * @throws BeanCreationException if the bean could not be created
  12. * @see #instantiateBean
  13. * @see #instantiateUsingFactoryMethod
  14. * @see #autowireConstructor
  15. */
  16. protected Object doCreateBean(final String beanName, final RootBeanDefinition mbd, final Object[] args)
  17. {
  18. // Instantiate the bean.
  19. BeanWrapper instanceWrapper = null;
  20. if (mbd.isSingleton())
  21. {
  22. instanceWrapper = this.factoryBeanInstanceCache.remove(beanName);
  23. }
  24. if (instanceWrapper == null)
  25. {
  26. instanceWrapper = createBeanInstance(beanName, mbd, args);
  27. }
  28. final Object bean = (instanceWrapper != null ? instanceWrapper.getWrappedInstance() : null);
  29. Class<?> beanType = (instanceWrapper != null ? instanceWrapper.getWrappedClass() : null);
  30. // Allow post-processors to modify the merged bean definition.
  31. synchronized (mbd.postProcessingLock)
  32. {
  33. if (!mbd.postProcessed)
  34. {
  35. applyMergedBeanDefinitionPostProcessors(mbd, beanType, beanName);
  36. mbd.postProcessed = true;
  37. }
  38. }
  39. // Eagerly cache singletons to be able to resolve circular references
  40. // even when triggered by lifecycle interfaces like BeanFactoryAware.
  41. boolean earlySingletonExposure = (mbd.isSingleton() && this.allowCircularReferences &&
  42. isSingletonCurrentlyInCreation(beanName));
  43. if (earlySingletonExposure)
  44. {
  45. if (logger.isDebugEnabled())
  46. {
  47. logger.debug("Eagerly caching bean ‘" + beanName +
  48. "‘ to allow for resolving potential circular references");
  49. }
  50. addSingletonFactory(beanName, new ObjectFactory<Object>() {
  51. public Object getObject() throws BeansException
  52. {
  53. return getEarlyBeanReference(beanName, mbd, bean);
  54. }
  55. });
  56. }
  57. // Initialize the bean instance.
  58. Object exposedObject = bean;
  59. try
  60. {
  61. populateBean(beanName, mbd, instanceWrapper);
  62. if (exposedObject != null)
  63. {
  64. exposedObject = initializeBean(beanName, exposedObject, mbd);
  65. }
  66. }
  67. catch (Throwable ex)
  68. {
  69. if (ex instanceof BeanCreationException && beanName.equals(((BeanCreationException) ex).getBeanName()))
  70. {
  71. throw (BeanCreationException) ex;
  72. }
  73. else
  74. {
  75. throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Initialization of bean failed", ex);
  76. }
  77. }
  78. if (earlySingletonExposure)
  79. {
  80. Object earlySingletonReference = getSingleton(beanName, false);
  81. if (earlySingletonReference != null)
  82. {
  83. if (exposedObject == bean)
  84. {
  85. exposedObject = earlySingletonReference;
  86. }
  87. else if (!this.allowRawInjectionDespiteWrapping && hasDependentBean(beanName))
  88. {
  89. String[] dependentBeans = getDependentBeans(beanName);
  90. Set<String> actualDependentBeans = new LinkedHashSet<String>(dependentBeans.length);
  91. for (String dependentBean : dependentBeans)
  92. {
  93. if (!removeSingletonIfCreatedForTypeCheckOnly(dependentBean))
  94. {
  95. actualDependentBeans.add(dependentBean);
  96. }
  97. }
  98. if (!actualDependentBeans.isEmpty())
  99. {
  100. throw new BeanCurrentlyInCreationException(beanName,
  101. "Bean with name ‘" + beanName + "‘ has been injected into other beans [" +
  102. StringUtils.collectionToCommaDelimitedString(actualDependentBeans) +
  103. "] in its raw version as part of a circular reference, but has eventually been " +
  104. "wrapped. This means that said other beans do not use the final version of the " +
  105. "bean. This is often the result of over-eager type matching - consider using " +
  106. "‘getBeanNamesOfType‘ with the ‘allowEagerInit‘ flag turned off, for example.");
  107. }
  108. }
  109. }
  110. }
  111. // Register bean as disposable.
  112. try
  113. {
  114. registerDisposableBeanIfNecessary(beanName, bean, mbd);
  115. }
  116. catch (BeanDefinitionValidationException ex)
  117. {
  118. throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Invalid destruction signature", ex);
  119. }
  120. return exposedObject;
  121. }

doCreateBean流程

1)会创建一个BeanWrapper对象,用来存放实例化对象

2)如果没有指定构造函数,会通过反射拿到一个默认的构造函数

3)调用spring的BeanUtils的instantiateClass方法,通过反射创建对象。

4)applyMergedBeanDefinitionPostProcessors

5)populateBean(),根据注入方式进行注入。根据是否有依赖检查进行依赖检查

执行bean注入的时候,会选择注入类型

[java] view plain copy

  1. /**
  2. * Populate the bean instance in the given BeanWrapper with the property values
  3. * from the bean definition.
  4. * @param beanName the name of the bean
  5. * @param mbd the bean definition for the bean
  6. * @param bw BeanWrapper with bean instance
  7. */
  8. protected void populateBean(String beanName, RootBeanDefinition mbd, BeanWrapper bw)
  9. {
  10. PropertyValues pvs = mbd.getPropertyValues();
  11. if (bw == null)
  12. {
  13. if (!pvs.isEmpty())
  14. {
  15. throw new BeanCreationException(
  16. mbd.getResourceDescription(), beanName, "Cannot apply property values to null instance");
  17. }
  18. else
  19. {
  20. // Skip property population phase for null instance.
  21. return;
  22. }
  23. }
  24. // Give any InstantiationAwareBeanPostProcessors the opportunity to modify the
  25. // state of the bean before properties are set. This can be used, for example,
  26. // to support styles of field injection.
  27. boolean continueWithPropertyPopulation = true;
  28. if (!mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors())
  29. {
  30. for (BeanPostProcessor bp : getBeanPostProcessors())
  31. {
  32. if (bp instanceof InstantiationAwareBeanPostProcessor)
  33. {
  34. InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp;
  35. if (!ibp.postProcessAfterInstantiation(bw.getWrappedInstance(), beanName))
  36. {
  37. continueWithPropertyPopulation = false;
  38. break;
  39. }
  40. }
  41. }
  42. }
  43. if (!continueWithPropertyPopulation)
  44. {
  45. return;
  46. }
  47. if (mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_BY_NAME ||
  48. mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_BY_TYPE)
  49. {
  50. MutablePropertyValues newPvs = new MutablePropertyValues(pvs);
  51. // Add property values based on autowire by name if applicable.
  52. if (mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_BY_NAME) //根据名称注入
  53. {
  54. autowireByName(beanName, mbd, bw, newPvs);
  55. }
  56. // Add property values based on autowire by type if applicable.
  57. if (mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_BY_TYPE) //根据类型注入
  58. {
  59. autowireByType(beanName, mbd, bw, newPvs);
  60. }
  61. pvs = newPvs;
  62. }
  63. boolean hasInstAwareBpps = hasInstantiationAwareBeanPostProcessors();
  64. boolean needsDepCheck = (mbd.getDependencyCheck() != RootBeanDefinition.DEPENDENCY_CHECK_NONE);
  65. if (hasInstAwareBpps || needsDepCheck)
  66. {
  67. PropertyDescriptor[] filteredPds = filterPropertyDescriptorsForDependencyCheck(bw, mbd.allowCaching);
  68. if (hasInstAwareBpps)
  69. {
  70. for (BeanPostProcessor bp : getBeanPostProcessors())
  71. {
  72. if (bp instanceof InstantiationAwareBeanPostProcessor)
  73. {
  74. InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp;
  75. pvs = ibp.postProcessPropertyValues(pvs, filteredPds, bw.getWrappedInstance(), beanName);
  76. if (pvs == null)
  77. {
  78. return;
  79. }
  80. }
  81. }
  82. }
  83. if (needsDepCheck)
  84. {
  85. checkDependencies(beanName, mbd, filteredPds, pvs);
  86. }
  87. }
  88. applyPropertyValues(beanName, mbd, bw, pvs);
  89. }

6)initializeBean(),判断是否实现了BeanNameAware、BeanClassLoaderAware等spring提供的接口,如果实现了,进行默认的注入。同时判断是否实现了InitializingBean接口,如果实现了,调用afterPropertySet方法

[java] view plain copy

  1. protected Object initializeBean(final String beanName, final Object bean, RootBeanDefinition mbd) {
  2. if (System.getSecurityManager() != null) {
  3. AccessController.doPrivileged(new PrivilegedAction<Object>() {
  4. public Object run() {
  5. invokeAwareMethods(beanName, bean);
  6. return null;
  7. }
  8. }, getAccessControlContext());
  9. }
  10. else {
  11. invokeAwareMethods(beanName, bean);
  12. }
  13. Object wrappedBean = bean;
  14. if (mbd == null || !mbd.isSynthetic()) {
  15. wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);
  16. }
  17. try {
  18. invokeInitMethods(beanName, wrappedBean, mbd);
  19. }
  20. catch (Throwable ex) {
  21. throw new BeanCreationException(
  22. (mbd != null ? mbd.getResourceDescription() : null),
  23. beanName, "Invocation of init method failed", ex);
  24. }
  25. if (mbd == null || !mbd.isSynthetic()) {
  26. wrappedBean = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);
  27. }
  28. return wrappedBean;
  29. }
  30. private void invokeAwareMethods(final String beanName, final Object bean) {
  31. if (bean instanceof Aware) {
  32. if (bean instanceof BeanNameAware) {
  33. ((BeanNameAware) bean).setBeanName(beanName);
  34. }
  35. if (bean instanceof BeanClassLoaderAware) {
  36. ((BeanClassLoaderAware) bean).setBeanClassLoader(getBeanClassLoader());
  37. }
  38. if (bean instanceof BeanFactoryAware) {
  39. ((BeanFactoryAware) bean).setBeanFactory(AbstractAutowireCapableBeanFactory.this);
  40. }
  41. }
  42. }

其中invokeInitMethods实现如下:

[java] view plain copy

  1. protected void invokeInitMethods(String beanName, final Object bean, RootBeanDefinition mbd)
  2. throws Throwable {
  3. boolean isInitializingBean = (bean instanceof InitializingBean);
  4. if (isInitializingBean && (mbd == null || !mbd.isExternallyManagedInitMethod("afterPropertiesSet"))) {
  5. if (logger.isDebugEnabled()) {
  6. logger.debug("Invoking afterPropertiesSet() on bean with name ‘" + beanName + "‘");
  7. }
  8. if (System.getSecurityManager() != null) {
  9. try {
  10. AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {
  11. public Object run() throws Exception {
  12. ((InitializingBean) bean).afterPropertiesSet();
  13. return null;
  14. }
  15. }, getAccessControlContext());
  16. }
  17. catch (PrivilegedActionException pae) {
  18. throw pae.getException();
  19. }
  20. }
  21. else {
  22. ((InitializingBean) bean).afterPropertiesSet();
  23. }
  24. }
  25. if (mbd != null) {
  26. String initMethodName = mbd.getInitMethodName();
  27. if (initMethodName != null && !(isInitializingBean && "afterPropertiesSet".equals(initMethodName)) &&
  28. !mbd.isExternallyManagedInitMethod(initMethodName)) {
  29. invokeCustomInitMethod(beanName, bean, mbd);
  30. }
  31. }
  32. }

Ioc容器初始化过程

1.ClassPathXmlApplicationContext类体系结构

左边的黄色部分是ApplicationContext体系继承结构,右边是BeanFactory结构体系,两个体系是典型的模板方法设计模式的使用。

从该继承体系可以看出:

(1)BeanFactory是一个bean工厂的最基本定义,里面包含了一个bean工厂的几个最基本方法:getBean(),containsBean()等,是一个很纯粹的bean工厂,不关注资源、资源位置、事件等。

ApplicationContext是一个容器的最基本接口定义,它继承了BeanFactory,拥有工厂的基本方法。同时继承了ApplicationEventPublisher、MessageSource、ResourcePatternResolver等接口,使其定义了一些额外的功能,如事件、资源等这些额外功能。

[java] view plain copy

  1. public interface ApplicationContext extends EnvironmentCapable, ListableBeanFactory, HierarchicalBeanFactory,
  2. MessageSource, ApplicationEventPublisher, ResourcePatternResolver

[java] view plain copy

  1. public interface HierarchicalBeanFactory extends BeanFactory

(2)AbstractBeanFactory和AbstractAutowireCapableBeanFactory是两个模板抽象工厂类。AbstractBeanFactory提供了bean工厂的抽象基类,同时提供了ConfigurableBeanFactory的完整实现。AbstractAutowireapableBeanFactory是继承了AbstractBeanFactory的抽象工厂,里面提供了bean创建的支持,包括bean的创建、依赖注入、检查等功能,是一个核心的bean工厂类。

(3)ClassPathXmlApplicationContext之所以拥有bean工厂的功能是通过持有一个真正的bean工厂DefaultListableBeanFactory的实例,并通过代理该工厂完成

[java] view plain copy

  1. public class ClassPathXmlApplicationContext extends AbstractXmlApplicationContext

[java] view plain copy

  1. public abstract class AbstractXmlApplicationContext extends AbstractRefreshableConfigApplicationContext

[java] view plain copy

  1. public abstract class AbstractRefreshableConfigApplicationContext extends AbstractRefreshableApplicationContext
  2. implements BeanNameAware, InitializingBean

[java] view plain copy

  1. public abstract class AbstractRefreshableApplicationContext extends AbstractApplicationContext {
  2. .........
  3. /** Bean factory for this context */
  4. private DefaultListableBeanFactory beanFactory;

(4)ClassPathXmlApplicationContext的初始化过程是对本身容器的初始化,同时也是对其持有的DefaultListableBeanFactory的初始化

2.容器初始化过程

整个过程可以通过下图来表示

从xml文件到BeanDefinition的过程是ApplicationContext的职责范围,从BeanDefinition Map到Bean是BeanFactory的职责。可以看出ApplicationContext是一个运行时的容器需要提供不同资源的支持,屏蔽不同环境的差异化。而BeanDefinition是内部关于bean定义的基本结构,bean的创建就是基于它。

整个容器的初始化是通过调用refresh()方法来实现的,该方法定义在AbstractApplicationContext接口中。AbstractApplicationContext是容器的最基础的一个抽象父类。也就是说在里面定义了一个容器初始化的基本流程。

[java] view plain copy

  1. public void refresh() throws BeansException, IllegalStateException {
  2. synchronized (this.startupShutdownMonitor) {
  3. // Prepare this context for refreshing.
  4. prepareRefresh();
  5. // Tell the subclass to refresh the internal bean factory.
  6. ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
  7. // Prepare the bean factory for use in this context.
  8. prepareBeanFactory(beanFactory);
  9. try {
  10. // Allows post-processing of the bean factory in context subclasses.
  11. postProcessBeanFactory(beanFactory);
  12. // Invoke factory processors registered as beans in the context.
  13. invokeBeanFactoryPostProcessors(beanFactory);
  14. // Register bean processors that intercept bean creation.
  15. registerBeanPostProcessors(beanFactory);
  16. // Initialize message source for this context.
  17. initMessageSource();
  18. // Initialize event multicaster for this context.
  19. initApplicationEventMulticaster();
  20. // Initialize other special beans in specific context subclasses.
  21. onRefresh();
  22. // Check for listener beans and register them.
  23. registerListeners();
  24. // Instantiate all remaining (non-lazy-init) singletons.
  25. finishBeanFactoryInitialization(beanFactory);
  26. // Last step: publish corresponding event.
  27. finishRefresh();
  28. }
  29. catch (BeansException ex) {
  30. logger.warn("Exception encountered during context initialization - cancelling refresh attempt", ex);
  31. // Destroy already created singletons to avoid dangling resources.
  32. destroyBeans();
  33. // Reset ‘active‘ flag.
  34. cancelRefresh(ex);
  35. // Propagate exception to caller.
  36. throw ex;
  37. }
  38. }
  39. }

解释如下:

3.Bean的创建过程

Bean的创建过程是BeanFactory索要完成的事情

(1)先看一下BeanDefinition的定义

从接口定义可以看出,通过bean定义能够bean的详细信息,如类名称、工厂类名称、scope、是否单例、是否抽象、是否延迟加载等等。

(2)bean的创建时机

容器初始化的时候,会先对单例和非延迟加载的对象进行预先初始化。其他的都是延迟加载,是在第一次调用getBean的时候被创建。从DefaultListableBeanFactory的preInstantiateSingletons()里可以看到这样的实现规则

[java] view plain copy

  1. public void preInstantiateSingletons() throws BeansException
  2. {
  3. if (this.logger.isInfoEnabled())
  4. {
  5. this.logger.info("Pre-instantiating singletons in " + this);
  6. }
  7. List<String> beanNames;
  8. synchronized (this.beanDefinitionMap)
  9. {
  10. // Iterate over a copy to allow for init methods which in turn register new bean definitions.
  11. // While this may not be part of the regular factory bootstrap, it does otherwise work fine.
  12. beanNames = new ArrayList<String>(this.beanDefinitionNames);
  13. }
  14. // Trigger initialization of all non-lazy singleton beans...
  15. for (String beanName : beanNames)
  16. {
  17. RootBeanDefinition bd = getMergedLocalBeanDefinition(beanName);
  18. if (!bd.isAbstract() && bd.isSingleton() && !bd.isLazyInit()) //对非抽象、单例、非延迟加载的对象先实例化
  19. {
  20. if (isFactoryBean(beanName))
  21. {
  22. final FactoryBean<?> factory = (FactoryBean<?>) getBean(FACTORY_BEAN_PREFIX + beanName);
  23. boolean isEagerInit;
  24. if (System.getSecurityManager() != null && factory instanceof SmartFactoryBean)
  25. {
  26. isEagerInit = AccessController.doPrivileged(new PrivilegedAction<Boolean>() {
  27. public Boolean run()
  28. {
  29. return ((SmartFactoryBean<?>) factory).isEagerInit();
  30. }
  31. }, getAccessControlContext());
  32. }
  33. else
  34. {
  35. isEagerInit = (factory instanceof SmartFactoryBean &&
  36. ((SmartFactoryBean<?>) factory).isEagerInit());
  37. }
  38. if (isEagerInit)
  39. {
  40. getBean(beanName);
  41. }
  42. }
  43. else
  44. {
  45. getBean(beanName);
  46. }
  47. }
  48. }
  49. }

从上面的代码看,如果用以下配置,只有singletonBean会被预先创建

[html] view plain copy

  1. <?xml version="1.0" encoding="GB2312"?>
  2. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
  3. <beans default-autowire="byName">
  4. <bean id="otherBean"          class="com.test.OtherBean" scope="prototype"/>
  5. <bean id="myBean"          class="com.test.MyBean" lazy-init="true"/>
  6. <bean id="singletonBean"          class="com.test.SingletonBean"/>
  7. </beans>

(3)bean的创建过程

对于bean的创建过程,其实都是通过调用工厂的getBean方法来完成的。在这个方法中会完成对构造函数的选择、依赖注入等。

无论预先创建还是延迟加载都是调用getBean来实现,AbstractBeanFactory定义了getBean的过程:

[java] view plain copy

  1. public Object getBean(String name) throws BeansException {
  2. return doGetBean(name, null, null, false);
  3. }

再看doGetBean()方法

[java] view plain copy

  1. protected <T> T doGetBean(
  2. final String name, final Class<T> requiredType, final Object[] args, boolean typeCheckOnly)
  3. throws BeansException
  4. {
  5. final String beanName = transformedBeanName(name);
  6. Object bean;
  7. // Eagerly check singleton cache for manually registered singletons.
  8. Object sharedInstance = getSingleton(beanName);
  9. if (sharedInstance != null && args == null)
  10. {
  11. if (logger.isDebugEnabled())
  12. {
  13. if (isSingletonCurrentlyInCreation(beanName))
  14. {
  15. logger.debug("Returning eagerly cached instance of singleton bean ‘" + beanName +
  16. "‘ that is not fully initialized yet - a consequence of a circular reference");
  17. }
  18. else
  19. {
  20. logger.debug("Returning cached instance of singleton bean ‘" + beanName + "‘");
  21. }
  22. }
  23. bean = getObjectForBeanInstance(sharedInstance, name, beanName, null);
  24. }
  25. else
  26. {
  27. // Fail if we‘re already creating this bean instance:
  28. // We‘re assumably within a circular reference.
  29. if (isPrototypeCurrentlyInCreation(beanName))
  30. {
  31. throw new BeanCurrentlyInCreationException(beanName);
  32. }
  33. // Check if bean definition exists in this factory.
  34. BeanFactory parentBeanFactory = getParentBeanFactory();
  35. if (parentBeanFactory != null && !containsBeanDefinition(beanName))
  36. {
  37. // Not found -> check parent.
  38. String nameToLookup = originalBeanName(name);
  39. if (args != null)
  40. {
  41. // Delegation to parent with explicit args.
  42. return (T) parentBeanFactory.getBean(nameToLookup, args);
  43. }
  44. else
  45. {
  46. // No args -> delegate to standard getBean method.
  47. return parentBeanFactory.getBean(nameToLookup, requiredType);
  48. }
  49. }
  50. if (!typeCheckOnly)
  51. {
  52. markBeanAsCreated(beanName);
  53. }
  54. try
  55. {
  56. final RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName);
  57. checkMergedBeanDefinition(mbd, beanName, args);
  58. // Guarantee initialization of beans that the current bean depends on.
  59. String[] dependsOn = mbd.getDependsOn();
  60. if (dependsOn != null)
  61. {
  62. for (String dependsOnBean : dependsOn)
  63. {
  64. getBean(dependsOnBean);
  65. registerDependentBean(dependsOnBean, beanName);
  66. }
  67. }
  68. // Create bean instance.
  69. if (mbd.isSingleton())              //如果是单例,间接通过getSingleton方法来创建,里面会实现将单例对象缓存
  70. {
  71. sharedInstance = getSingleton(beanName, new ObjectFactory<Object>() {
  72. public Object getObject() throws BeansException
  73. {
  74. try
  75. {
  76. return createBean(beanName, mbd, args);
  77. }
  78. catch (BeansException ex)
  79. {
  80. // Explicitly remove instance from singleton cache: It might have been put there
  81. // eagerly by the creation process, to allow for circular reference resolution.
  82. // Also remove any beans that received a temporary reference to the bean.
  83. destroySingleton(beanName);
  84. throw ex;
  85. }
  86. }
  87. });
  88. bean = getObjectForBeanInstance(sharedInstance, name, beanName, mbd);
  89. }
  90. else if (mbd.isPrototype())                 //非单例对象
  91. {
  92. // It‘s a prototype -> create a new instance.
  93. Object prototypeInstance = null;
  94. try
  95. {
  96. beforePrototypeCreation(beanName);
  97. prototypeInstance = createBean(beanName, mbd, args);
  98. }
  99. finally
  100. {
  101. afterPrototypeCreation(beanName);
  102. }
  103. bean = getObjectForBeanInstance(prototypeInstance, name, beanName, mbd);
  104. }
  105. else
  106. {
  107. String scopeName = mbd.getScope();
  108. final Scope scope = this.scopes.get(scopeName);
  109. if (scope == null)
  110. {
  111. throw new IllegalStateException("No Scope registered for scope ‘" + scopeName + "‘");
  112. }
  113. try
  114. {
  115. Object scopedInstance = scope.get(beanName, new ObjectFactory<Object>() {
  116. public Object getObject() throws BeansException
  117. {
  118. beforePrototypeCreation(beanName);
  119. try
  120. {
  121. return createBean(beanName, mbd, args);
  122. }
  123. finally
  124. {
  125. afterPrototypeCreation(beanName);
  126. }
  127. }
  128. });
  129. bean = getObjectForBeanInstance(scopedInstance, name, beanName, mbd);
  130. }
  131. catch (IllegalStateException ex)
  132. {
  133. throw new BeanCreationException(beanName,
  134. "Scope ‘" + scopeName + "‘ is not active for the current thread; " +
  135. "consider defining a scoped proxy for this bean if you intend to refer to it from a singleton",
  136. ex);
  137. }
  138. }
  139. }
  140. catch (BeansException ex)
  141. {
  142. cleanupAfterBeanCreationFailure(beanName);
  143. throw ex;
  144. }
  145. }
  146. // Check if required type matches the type of the actual bean instance.
  147. if (requiredType != null && bean != null && !requiredType.isAssignableFrom(bean.getClass()))
  148. {
  149. try
  150. {
  151. return getTypeConverter().convertIfNecessary(bean, requiredType);
  152. }
  153. catch (TypeMismatchException ex)
  154. {
  155. if (logger.isDebugEnabled())
  156. {
  157. logger.debug("Failed to convert bean ‘" + name + "‘ to required type [" +
  158. ClassUtils.getQualifiedName(requiredType) + "]", ex);
  159. }
  160. throw new BeanNotOfRequiredTypeException(name, requiredType, bean.getClass());
  161. }
  162. }
  163. return (T) bean;
  164. }

doGetBean的大概过程

1)先试着从单例缓存中获取对象

2)从父容器中取定义,有则由父容器创建

3)如果是单例,则走单例对象的创建过程:在spring容器中单例对象和非单例对象的创建过程是一样的。都会调用父类AbstractAutowireCapableBeanFactory的createBean()方法。不同的是单例对象之创建一次并且需要缓存起来。DefaultListableBeanFactory的父类DefaultSingletonBeanRegistry提供了对单例对象缓存等支持工作。所以是单例对象的话,会调用DefaultSingletonBeanRegistry的getSingleton()方法,它会间接调用AbstractAutowireCapableBeanFactory的createBean()方法。

bean的创建是由AbstractAutowireCapableBeanFactory来定义

[java] view plain copy

  1. /**
  2. * Central method of this class: creates a bean instance,
  3. * populates the bean instance, applies post-processors, etc.
  4. * @see #doCreateBean
  5. */
  6. @Override
  7. protected Object createBean(final String beanName, final RootBeanDefinition mbd, final Object[] args)
  8. throws BeanCreationException
  9. {
  10. if (logger.isDebugEnabled())
  11. {
  12. logger.debug("Creating instance of bean ‘" + beanName + "‘");
  13. }
  14. // Make sure bean class is actually resolved at this point.
  15. resolveBeanClass(mbd, beanName);
  16. // Prepare method overrides.
  17. try
  18. {
  19. mbd.prepareMethodOverrides();
  20. }
  21. catch (BeanDefinitionValidationException ex)
  22. {
  23. throw new BeanDefinitionStoreException(mbd.getResourceDescription(),
  24. beanName, "Validation of method overrides failed", ex);
  25. }
  26. try
  27. {
  28. // Give BeanPostProcessors a chance to return a proxy instead of the target bean instance.
  29. Object bean = resolveBeforeInstantiation(beanName, mbd);
  30. if (bean != null)
  31. {
  32. return bean;
  33. }
  34. }
  35. catch (Throwable ex)
  36. {
  37. throw new BeanCreationException(mbd.getResourceDescription(), beanName,
  38. "BeanPostProcessor before instantiation of bean failed", ex);
  39. }
  40. Object beanInstance = doCreateBean(beanName, mbd, args);
  41. if (logger.isDebugEnabled())
  42. {
  43. logger.debug("Finished creating instance of bean ‘" + beanName + "‘");
  44. }
  45. return beanInstance;
  46. }

createBean会调用doCreateBean方法

[java] view plain copy

  1. /**
  2. * Actually create the specified bean. Pre-creation processing has already happened
  3. * at this point, e.g. checking {@code postProcessBeforeInstantiation} callbacks.
  4. * <p>Differentiates between default bean instantiation, use of a
  5. * factory method, and autowiring a constructor.
  6. * @param beanName the name of the bean
  7. * @param mbd the merged bean definition for the bean
  8. * @param args arguments to use if creating a prototype using explicit arguments to a
  9. * static factory method. This parameter must be {@code null} except in this case.
  10. * @return a new instance of the bean
  11. * @throws BeanCreationException if the bean could not be created
  12. * @see #instantiateBean
  13. * @see #instantiateUsingFactoryMethod
  14. * @see #autowireConstructor
  15. */
  16. protected Object doCreateBean(final String beanName, final RootBeanDefinition mbd, final Object[] args)
  17. {
  18. // Instantiate the bean.
  19. BeanWrapper instanceWrapper = null;
  20. if (mbd.isSingleton())
  21. {
  22. instanceWrapper = this.factoryBeanInstanceCache.remove(beanName);
  23. }
  24. if (instanceWrapper == null)
  25. {
  26. instanceWrapper = createBeanInstance(beanName, mbd, args);
  27. }
  28. final Object bean = (instanceWrapper != null ? instanceWrapper.getWrappedInstance() : null);
  29. Class<?> beanType = (instanceWrapper != null ? instanceWrapper.getWrappedClass() : null);
  30. // Allow post-processors to modify the merged bean definition.
  31. synchronized (mbd.postProcessingLock)
  32. {
  33. if (!mbd.postProcessed)
  34. {
  35. applyMergedBeanDefinitionPostProcessors(mbd, beanType, beanName);
  36. mbd.postProcessed = true;
  37. }
  38. }
  39. // Eagerly cache singletons to be able to resolve circular references
  40. // even when triggered by lifecycle interfaces like BeanFactoryAware.
  41. boolean earlySingletonExposure = (mbd.isSingleton() && this.allowCircularReferences &&
  42. isSingletonCurrentlyInCreation(beanName));
  43. if (earlySingletonExposure)
  44. {
  45. if (logger.isDebugEnabled())
  46. {
  47. logger.debug("Eagerly caching bean ‘" + beanName +
  48. "‘ to allow for resolving potential circular references");
  49. }
  50. addSingletonFactory(beanName, new ObjectFactory<Object>() {
  51. public Object getObject() throws BeansException
  52. {
  53. return getEarlyBeanReference(beanName, mbd, bean);
  54. }
  55. });
  56. }
  57. // Initialize the bean instance.
  58. Object exposedObject = bean;
  59. try
  60. {
  61. populateBean(beanName, mbd, instanceWrapper);
  62. if (exposedObject != null)
  63. {
  64. exposedObject = initializeBean(beanName, exposedObject, mbd);
  65. }
  66. }
  67. catch (Throwable ex)
  68. {
  69. if (ex instanceof BeanCreationException && beanName.equals(((BeanCreationException) ex).getBeanName()))
  70. {
  71. throw (BeanCreationException) ex;
  72. }
  73. else
  74. {
  75. throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Initialization of bean failed", ex);
  76. }
  77. }
  78. if (earlySingletonExposure)
  79. {
  80. Object earlySingletonReference = getSingleton(beanName, false);
  81. if (earlySingletonReference != null)
  82. {
  83. if (exposedObject == bean)
  84. {
  85. exposedObject = earlySingletonReference;
  86. }
  87. else if (!this.allowRawInjectionDespiteWrapping && hasDependentBean(beanName))
  88. {
  89. String[] dependentBeans = getDependentBeans(beanName);
  90. Set<String> actualDependentBeans = new LinkedHashSet<String>(dependentBeans.length);
  91. for (String dependentBean : dependentBeans)
  92. {
  93. if (!removeSingletonIfCreatedForTypeCheckOnly(dependentBean))
  94. {
  95. actualDependentBeans.add(dependentBean);
  96. }
  97. }
  98. if (!actualDependentBeans.isEmpty())
  99. {
  100. throw new BeanCurrentlyInCreationException(beanName,
  101. "Bean with name ‘" + beanName + "‘ has been injected into other beans [" +
  102. StringUtils.collectionToCommaDelimitedString(actualDependentBeans) +
  103. "] in its raw version as part of a circular reference, but has eventually been " +
  104. "wrapped. This means that said other beans do not use the final version of the " +
  105. "bean. This is often the result of over-eager type matching - consider using " +
  106. "‘getBeanNamesOfType‘ with the ‘allowEagerInit‘ flag turned off, for example.");
  107. }
  108. }
  109. }
  110. }
  111. // Register bean as disposable.
  112. try
  113. {
  114. registerDisposableBeanIfNecessary(beanName, bean, mbd);
  115. }
  116. catch (BeanDefinitionValidationException ex)
  117. {
  118. throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Invalid destruction signature", ex);
  119. }
  120. return exposedObject;
  121. }

doCreateBean流程

1)会创建一个BeanWrapper对象,用来存放实例化对象

2)如果没有指定构造函数,会通过反射拿到一个默认的构造函数

3)调用spring的BeanUtils的instantiateClass方法,通过反射创建对象。

4)applyMergedBeanDefinitionPostProcessors

5)populateBean(),根据注入方式进行注入。根据是否有依赖检查进行依赖检查

执行bean注入的时候,会选择注入类型

[java] view plain copy

  1. /**
  2. * Populate the bean instance in the given BeanWrapper with the property values
  3. * from the bean definition.
  4. * @param beanName the name of the bean
  5. * @param mbd the bean definition for the bean
  6. * @param bw BeanWrapper with bean instance
  7. */
  8. protected void populateBean(String beanName, RootBeanDefinition mbd, BeanWrapper bw)
  9. {
  10. PropertyValues pvs = mbd.getPropertyValues();
  11. if (bw == null)
  12. {
  13. if (!pvs.isEmpty())
  14. {
  15. throw new BeanCreationException(
  16. mbd.getResourceDescription(), beanName, "Cannot apply property values to null instance");
  17. }
  18. else
  19. {
  20. // Skip property population phase for null instance.
  21. return;
  22. }
  23. }
  24. // Give any InstantiationAwareBeanPostProcessors the opportunity to modify the
  25. // state of the bean before properties are set. This can be used, for example,
  26. // to support styles of field injection.
  27. boolean continueWithPropertyPopulation = true;
  28. if (!mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors())
  29. {
  30. for (BeanPostProcessor bp : getBeanPostProcessors())
  31. {
  32. if (bp instanceof InstantiationAwareBeanPostProcessor)
  33. {
  34. InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp;
  35. if (!ibp.postProcessAfterInstantiation(bw.getWrappedInstance(), beanName))
  36. {
  37. continueWithPropertyPopulation = false;
  38. break;
  39. }
  40. }
  41. }
  42. }
  43. if (!continueWithPropertyPopulation)
  44. {
  45. return;
  46. }
  47. if (mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_BY_NAME ||
  48. mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_BY_TYPE)
  49. {
  50. MutablePropertyValues newPvs = new MutablePropertyValues(pvs);
  51. // Add property values based on autowire by name if applicable.
  52. if (mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_BY_NAME) //根据名称注入
  53. {
  54. autowireByName(beanName, mbd, bw, newPvs);
  55. }
  56. // Add property values based on autowire by type if applicable.
  57. if (mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_BY_TYPE) //根据类型注入
  58. {
  59. autowireByType(beanName, mbd, bw, newPvs);
  60. }
  61. pvs = newPvs;
  62. }
  63. boolean hasInstAwareBpps = hasInstantiationAwareBeanPostProcessors();
  64. boolean needsDepCheck = (mbd.getDependencyCheck() != RootBeanDefinition.DEPENDENCY_CHECK_NONE);
  65. if (hasInstAwareBpps || needsDepCheck)
  66. {
  67. PropertyDescriptor[] filteredPds = filterPropertyDescriptorsForDependencyCheck(bw, mbd.allowCaching);
  68. if (hasInstAwareBpps)
  69. {
  70. for (BeanPostProcessor bp : getBeanPostProcessors())
  71. {
  72. if (bp instanceof InstantiationAwareBeanPostProcessor)
  73. {
  74. InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp;
  75. pvs = ibp.postProcessPropertyValues(pvs, filteredPds, bw.getWrappedInstance(), beanName);
  76. if (pvs == null)
  77. {
  78. return;
  79. }
  80. }
  81. }
  82. }
  83. if (needsDepCheck)
  84. {
  85. checkDependencies(beanName, mbd, filteredPds, pvs);
  86. }
  87. }
  88. applyPropertyValues(beanName, mbd, bw, pvs);
  89. }

6)initializeBean(),判断是否实现了BeanNameAware、BeanClassLoaderAware等spring提供的接口,如果实现了,进行默认的注入。同时判断是否实现了InitializingBean接口,如果实现了,调用afterPropertySet方法

[java] view plain copy

  1. protected Object initializeBean(final String beanName, final Object bean, RootBeanDefinition mbd) {
  2. if (System.getSecurityManager() != null) {
  3. AccessController.doPrivileged(new PrivilegedAction<Object>() {
  4. public Object run() {
  5. invokeAwareMethods(beanName, bean);
  6. return null;
  7. }
  8. }, getAccessControlContext());
  9. }
  10. else {
  11. invokeAwareMethods(beanName, bean);
  12. }
  13. Object wrappedBean = bean;
  14. if (mbd == null || !mbd.isSynthetic()) {
  15. wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);
  16. }
  17. try {
  18. invokeInitMethods(beanName, wrappedBean, mbd);
  19. }
  20. catch (Throwable ex) {
  21. throw new BeanCreationException(
  22. (mbd != null ? mbd.getResourceDescription() : null),
  23. beanName, "Invocation of init method failed", ex);
  24. }
  25. if (mbd == null || !mbd.isSynthetic()) {
  26. wrappedBean = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);
  27. }
  28. return wrappedBean;
  29. }
  30. private void invokeAwareMethods(final String beanName, final Object bean) {
  31. if (bean instanceof Aware) {
  32. if (bean instanceof BeanNameAware) {
  33. ((BeanNameAware) bean).setBeanName(beanName);
  34. }
  35. if (bean instanceof BeanClassLoaderAware) {
  36. ((BeanClassLoaderAware) bean).setBeanClassLoader(getBeanClassLoader());
  37. }
  38. if (bean instanceof BeanFactoryAware) {
  39. ((BeanFactoryAware) bean).setBeanFactory(AbstractAutowireCapableBeanFactory.this);
  40. }
  41. }
  42. }

其中invokeInitMethods实现如下:

[java] view plain copy

  1. protected void invokeInitMethods(String beanName, final Object bean, RootBeanDefinition mbd)
  2. throws Throwable {
  3. boolean isInitializingBean = (bean instanceof InitializingBean);
  4. if (isInitializingBean && (mbd == null || !mbd.isExternallyManagedInitMethod("afterPropertiesSet"))) {
  5. if (logger.isDebugEnabled()) {
  6. logger.debug("Invoking afterPropertiesSet() on bean with name ‘" + beanName + "‘");
  7. }
  8. if (System.getSecurityManager() != null) {
  9. try {
  10. AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {
  11. public Object run() throws Exception {
  12. ((InitializingBean) bean).afterPropertiesSet();
  13. return null;
  14. }
  15. }, getAccessControlContext());
  16. }
  17. catch (PrivilegedActionException pae) {
  18. throw pae.getException();
  19. }
  20. }
  21. else {
  22. ((InitializingBean) bean).afterPropertiesSet();
  23. }
  24. }
  25. if (mbd != null) {
  26. String initMethodName = mbd.getInitMethodName();
  27. if (initMethodName != null && !(isInitializingBean && "afterPropertiesSet".equals(initMethodName)) &&
  28. !mbd.isExternallyManagedInitMethod(initMethodName)) {
  29. invokeCustomInitMethod(beanName, bean, mbd);
  30. }
  31. }

}

Ioc容器初始化过程

时间: 2024-08-25 14:09:20

IOC容器的初始化过程的相关文章

Spring IoC容器的初始化过程

Spring IoC容器的初始化包括 BeanDefinition的Resource定位.载入和注册 这三个基本的过程.IoC容器的初始化过程不包含Bean依赖注入的实现.Bean依赖的注入一般会发生在第一次通过getBean向容器索取Bean的时候. 先看以下代码: ApplicationContext context = new ClassPathXmlApplicationContext("ioc.xml"); Car car = (Car) context.getBean(&q

SpringBoot启动流程分析(四):IoC容器的初始化过程

SpringBoot系列文章简介 SpringBoot源码阅读辅助篇: Spring IoC容器与应用上下文的设计与实现 SpringBoot启动流程源码分析: SpringBoot启动流程分析(一):SpringApplication类初始化过程 SpringBoot启动流程分析(二):SpringApplication的run方法 SpringBoot启动流程分析(三):SpringApplication的run方法之prepareContext()方法 SpringBoot启动流程分析(四

Spring IOC容器的初始化-(三)BeanDefinition的注册

---恢复内容开始--- 前言 在上一篇中有一处代码是BeanDefiniton注册的入口,我们回顾一下. 1.BeanDefiniton在IOC容器注册 首先我们回顾两点,1. 发起注册的地方:2. 注册的实现类 1.发起注册的地方 我们先看第一点,在上篇博文中我们讲了Bean的解析,在DefaultBeanDefinitonDocumentReader类的processBeanDefiniton()方法中,其中一个是去完成BeanDefiniton的载入,另一个是完成注册.我们看下代码: D

spring5源码分析系列(五)——IOC容器的初始化(三)

前言:上一篇讲到了DocumentLoader将Bean定义资源转换为Document对象,此篇我们继续后面的内容. (9)XmlBeanDefinitionReader解析载入的Bean定义资源文件 XmlBeanDefinitionReader类中的doLoadBeanDefinitions方法是从特定XML文件中实际载入Bean定义资源的方法,该方法在载入Bean定义资源之后将其转换为Document对象, 接下来调用registerBeanDefinitions启动Spring IOC容

IoC容器的启动过程

 <context-param>   <param-name>contextConfigLocation</param-name>   <param-value>applicationContext.xml</param-value>  </context-param>    <listener>   <listener-class>org.springframework.web.context.Context

spring源码研究之IoC容器在web容器中初始化过程

前段时间在公司做了一个项目,项目用了spring框架实现,WEB容器是Tomct 5,虽然说把项目做完了,但是一直对spring的IoC容器在web容器如何启动和起作用的并不清楚.所以就抽时间看一下spring的源代码,借此了解它的原理. 我们知道,对于使用Spring的web应用,无须手动创建Spring容器,而是通过配置文件,声明式的创建Spring容器.因此在Web应用中创建Spring容器有如下两种方式: 1. 直接在web.xml文件中配置创建Spring容器. 2. 利用第三方MVC

Spring IOC容器的初始化流程

IOC初始化流程 Resource定位:指对BeanDefinition的资源定位过程.Bean 可能定义在XML中,或者是一个注解,或者是其他形式.这些都被用Resource来定位, 读取Resource获取BeanDefinition 并注册到 Bean定义注册表中. BeanDefinition的载入:把用户定义好的Javabean表示为IoC容器内部的数据结构,这个容器内部的数据结构就是BeanDefinition. 向IoC容器注册这些BeanDefinition. 获取Bean的流程

spring5源码分析系列(四)——IOC容器的初始化(二)

前言:上一篇讲到了Xml Bean读取器(XmlBeanDefinitionReader)调用其父类AbstractBeanDefinitionReader的reader.loadBeanDefinitions方法读取Bean定义资源,此篇我们继续后面的内容. (5)AbstractBeanDefinitionReader的loadBeanDefinitions方法 方法源码如下: //重载方法,调用下面的loadBeanDefinitions(String, Set<Resource>)方法

spring5源码分析系列(六)——IOC容器的初始化(四)

前言:上一篇讲到了解析子元素,此篇我们继续后面的内容. (15)解析过后的BeanDefinition在IOC容器中的注册 接下来分析DefaultBeanDefinitionDocumentReader对Bean定义转换的Document对象解析的流程中,在其parseDefaultElement方法中完成对Document对象的解析后得到封装BeanDefinition的BeanDefinitionHold对象, 然后调用BeanDefinitionReaderUtils的registerB