Spring Bean大体步骤分为四个阶段

实例化 -> 属性赋值 -> 初始化 -> 销毁

阐释Spring下bean的生命周期

① Spring IoC容器找到关于Bean的定义并实例化该Bean。
② Spring IoC容器对Bean进行依赖注入。
③ 如果Bean实现了BeanNameAware接口,则将该Bean的id传给setBeanName方法。
④ 如果Bean实现了BeanFactoryAware接口,则将BeanFactory对象传给setBeanFactory方法。
⑤ 如果Bean实现了BeanPostProcessor接口,则调用其postProcessBeforeInitialization方法。
⑥ 如果Bean实现了InitializingBean接口,则调用其afterPropertySet方法。
⑦ 如果有和Bean关联的BeanPostProcessors对象,则这些对象的postProcessAfterInitialization方法被调用。
⑧ 当销毁Bean实例时,如果Bean实现了DisposableBean接口,则调用其destroy方法。1) spring对bean进行实例化,默认bean是单例

各种接口方法分类

Bean的完整生命周期经理了各种方法调用,这些方法可以划分为以下几类:
1、Bean 自身的方法:这个包括了Bean本身调用的方法和通过配置文件中的init-method和destroy-method指定的方法
2、生命周期接口方法:这包括了BeanNameAware、BeanFactoryAware、InitializingBean和DiposableBean这些接口的方法
3、容器级生命周期接口方法:这个包括了InstantiationAwareBeanPostProcessor 和 BeanPostProcessor 这两个接口实现,一般称它们的实现类为“后处理器”。
4、工厂处理器接口方法: 这个包括了AspectJWeavingEnabler, ConfigurationClassPostProcessor, CustomAutowireConfigurer等等非常有用的工厂后处理器  接口的方法。工厂后处理器也是容器级的。在应用上下文装配配置文件之后立即调用。、

API 解释

BeanNameAware

//设置beanName的名称

  1. public interface BeanNameAware extends Aware {
  2. //设置beanName名称
  3. void setBeanName(String name);
  4. }

BeanFactoryAware

//设置bean工厂

  1. public interface BeanFactoryAware extends Aware {
  2. //设置bean工厂
  3. void setBeanFactory(BeanFactory beanFactory) throws BeansException;
  4. }

ApplicationContextAware

//设置业务上下文

  1. public interface ApplicationContextAware extends Aware {
  2. void setApplicationContext(ApplicationContext applicationContext) throws BeansException;
  3. }

BeanPostProcessor

//Spring后置处理器,在每个bean的初始化前后调用,首先调用的是before方法

  1. public interface BeanPostProcessor {
  2. Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException;
  3. Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException;
  4. }

InitializingBean

  1. public interface InitializingBean {
  2. void afterPropertiesSet() throws Exception;
  3. }

BeanPostProcessor

//Spring后置处理器,在每个bean的初始化前后调用,调用的是after方法

  1. public interface BeanPostProcessor {
  2. Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException;
  3. Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException;
  4. }

调用初始化方法

使用/销毁

总结
1.调用无参构造进行实例化
2.Aware接口表示的就是属性注入的接口

源码解读

GenericBeanDefinition

存放对象元信息
image.png

AbstractBeanFactory

image.png

AnnotationConfigApplicationContext

  1. public AnnotationConfigApplicationContext(Class<?>... annotatedClasses) {
  2. this();
  3. //this.beanDefinitionMap.put(beanName, beanDefinition);
  4. register(annotatedClasses);
  5. //还未开始扫描和解析
  6. refresh();
  7. }

AbstractApplicationContext

  1. //扫描解析
  2. invokeBeanFactoryPostProcessors(beanFactory);
  3. //实例化bean
  4. finishBeanFactoryInitialization(beanFactory);

//实例化所以单例对象,此时bean工厂的spring 单例池中已经包含了bean的定义信息
beanFactory.preInstantiateSingletons()

  1. protected void finishBeanFactoryInitialization(ConfigurableListableBeanFactory beanFactory) {
  2. // Initialize conversion service for this context.
  3. if (beanFactory.containsBean(CONVERSION_SERVICE_BEAN_NAME) &&
  4. beanFactory.isTypeMatch(CONVERSION_SERVICE_BEAN_NAME, ConversionService.class)) {
  5. beanFactory.setConversionService(
  6. beanFactory.getBean(CONVERSION_SERVICE_BEAN_NAME, ConversionService.class));
  7. }
  8. // Initialize LoadTimeWeaverAware beans early to allow for registering their transformers early.
  9. String[] weaverAwareNames = beanFactory.getBeanNamesForType(LoadTimeWeaverAware.class, false, false);
  10. for (String weaverAwareName : weaverAwareNames) {
  11. getBean(weaverAwareName);
  12. }
  13. // Stop using the temporary ClassLoader for type matching.
  14. beanFactory.setTempClassLoader(null);
  15. // Allow for caching all bean definition metadata, not expecting further changes.
  16. beanFactory.freezeConfiguration();
  17. // Instantiate all remaining (non-lazy-init) singletons.
  18. //实例化所以单例对象
  19. beanFactory.preInstantiateSingletons();
  20. }

DefaultListableBeanFactory

  1. @Override
  2. public void preInstantiateSingletons() throws BeansException {
  3. if (this.logger.isDebugEnabled()) {
  4. this.logger.debug("Pre-instantiating singletons in " + this);
  5. }
  6. //获取所以bean的名称
  7. List<String> beanNames;
  8. synchronized (this.beanDefinitionMap) {
  9. // Iterate over a copy to allow for init methods which in turn register new bean definitions.
  10. // While this may not be part of the regular factory bootstrap, it does otherwise work fine.
  11. beanNames = new ArrayList<String>(this.beanDefinitionNames);
  12. }
  13. // Trigger initialization of all non-lazy singleton beans...
  14. //触发所有非延迟加载单例beans的初始化,主要步骤为调用getBean
  15. for (String beanName : beanNames) {
  16. //合并父BeanDefinition
  17. RootBeanDefinition bd = getMergedLocalBeanDefinition(beanName);
  18. //如果不是抽象的 & 是单例的 & 不是懒加载的
  19. if (!bd.isAbstract() && bd.isSingleton() && !bd.isLazyInit()) {
  20. //BeanFactory 和 FactoryBean 的区别?面试点
  21. if (isFactoryBean(beanName)) {
  22. final FactoryBean<?> factory = (FactoryBean<?>) getBean(FACTORY_BEAN_PREFIX + beanName);
  23. boolean isEagerInit;
  24. if (System.getSecurityManager() != null && factory instanceof SmartFactoryBean) {
  25. isEagerInit = AccessController.doPrivileged(new PrivilegedAction<Boolean>() {
  26. @Override
  27. public Boolean run() {
  28. return ((SmartFactoryBean<?>) factory).isEagerInit();
  29. }
  30. }, getAccessControlContext());
  31. }
  32. else {
  33. isEagerInit = (factory instanceof SmartFactoryBean &&
  34. ((SmartFactoryBean<?>) factory).isEagerInit());
  35. }
  36. if (isEagerInit) {
  37. getBean(beanName);
  38. }
  39. }
  40. else {
  41. //进这里
  42. getBean(beanName);
  43. }
  44. }
  45. }
  46. // Trigger post-initialization callback for all applicable beans...
  47. for (String beanName : beanNames) {
  48. Object singletonInstance = getSingleton(beanName);
  49. if (singletonInstance instanceof SmartInitializingSingleton) {
  50. final SmartInitializingSingleton smartSingleton = (SmartInitializingSingleton) singletonInstance;
  51. if (System.getSecurityManager() != null) {
  52. AccessController.doPrivileged(new PrivilegedAction<Object>() {
  53. @Override
  54. public Object run() {
  55. smartSingleton.afterSingletonsInstantiated();
  56. return null;
  57. }
  58. }, getAccessControlContext());
  59. }
  60. else {
  61. smartSingleton.afterSingletonsInstantiated();
  62. }
  63. }
  64. }
  65. }

AbstractBeanFactory

//spring bean 生命周期关键代码
//spring单例池
//private final Map singletonObjects = new ConcurrentHashMap(64);

  1. public Object getBean(String name) throws BeansException {
  2. return doGetBean(name, null, null, false);
  3. }
  4. protected <T> T doGetBean(
  5. final String name, final Class<T> requiredType, final Object[] args, boolean typeCheckOnly)
  6. throws BeansException {
  7. //获取对应的beanName
  8. final String beanName = transformedBeanName(name);
  9. Object bean;
  10. // Eagerly check singleton cache for manually registered singletons.
  11. /**
  12. * 检查缓存中或者实例工厂中是否有对应的bean
  13. * 为什么首先会使用这段代码呢?
  14. * 因为在创建单例bean的时候会存在依赖注入的情况,而创建依赖的时候为了避免循环依赖
  15. * Spring创建bean的原则是不等bean创建完成就将创建bean的ObjectFactory提早曝光
  16. * 也就是将ObjectFactory加入缓存,一旦下一个bean创建的时候需要依赖上一个bean的时候直接使用objectFactory
  17. *
  18. */
  19. //为什么要先去调用一遍获取bean?spring启动的时候会调用一次,返回null,获取一个bean的时候
  20. //也会调用这个方法,并且返回对象。
  21. //直接尝试从缓存中获取,或者singletonFacotory中获取
  22. Object sharedInstance = getSingleton(beanName);
  23. if (sharedInstance != null && args == null) {
  24. if (logger.isDebugEnabled()) {
  25. if (isSingletonCurrentlyInCreation(beanName)) {
  26. logger.debug("Returning eagerly cached instance of singleton bean '" + beanName +
  27. "' that is not fully initialized yet - a consequence of a circular reference");
  28. }
  29. else {
  30. logger.debug("Returning cached instance of singleton bean '" + beanName + "'");
  31. }
  32. }
  33. //返回对应的实例,有时候存在诸如beanFactory的情况并不是直接返回实例本身,而是返回指定方法返回的实例
  34. bean = getObjectForBeanInstance(sharedInstance, name, beanName, null);
  35. }
  36. else {
  37. //进这里
  38. // Fail if we're already creating this bean instance:
  39. // We're assumably within a circular reference.
  40. //如果是原型,不应该在初始化的时候创建
  41. //只有在单例情况才会尝试解决循环依赖。原型模式下如果存在A中有b,b中有A,
  42. //那么当依赖注入的时候就会产生当A还未创建完的时候对于B的创建再次返回创建A
  43. //造成循环依赖 也就是下面的情况
  44. if (isPrototypeCurrentlyInCreation(beanName)) {
  45. throw new BeanCurrentlyInCreationException(beanName);
  46. }
  47. // Check if bean definition exists in this factory.
  48. BeanFactory parentBeanFactory = getParentBeanFactory();
  49. //如果beanDefinitionMap中也就是已经加载的bean类中不包括beanName,则尝试从parentbeanfactory中获取
  50. if (parentBeanFactory != null && !containsBeanDefinition(beanName)) {
  51. // Not found -> check parent.
  52. String nameToLookup = originalBeanName(name);
  53. if (args != null) {
  54. //递归到parentFacotory中寻找
  55. // Delegation to parent with explicit args.
  56. return (T) parentBeanFactory.getBean(nameToLookup, args);
  57. }
  58. else {
  59. // No args -> delegate to standard getBean method.
  60. return parentBeanFactory.getBean(nameToLookup, requiredType);
  61. }
  62. }
  63. //如果不是仅仅做类型检查而是创建bean这里要进行记录
  64. if (!typeCheckOnly) {
  65. //如果bean被创建,在这里会标记一遍
  66. markBeanAsCreated(beanName);
  67. }
  68. //检查BeanDefinition
  69. //将存储XML配置文件的GericBeanDefinition转换为RootBeanDefinition,如果指定BeanName是子类的话会同时合并父类的相关属性
  70. final RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName);
  71. checkMergedBeanDefinition(mbd, beanName, args);
  72. // Guarantee initialization of beans that the current bean depends on.
  73. String[] dependsOn = mbd.getDependsOn();
  74. //若存在依赖则要递归实例化依赖的bean
  75. if (dependsOn != null) {
  76. for (String dependsOnBean : dependsOn) {
  77. getBean(dependsOnBean);
  78. //缓存依赖调用
  79. registerDependentBean(dependsOnBean, beanName);
  80. }
  81. }
  82. // Create bean instance.
  83. //实例化依赖的bean后就可以实例化mdb本身了
  84. //singleton模式的创建
  85. if (mbd.isSingleton()) {
  86. sharedInstance = getSingleton(beanName, new ObjectFactory<Object>() {
  87. public Object getObject() throws BeansException {
  88. try {
  89. //创建bean对象
  90. return createBean(beanName, mbd, args);
  91. }
  92. catch (BeansException ex) {
  93. // Explicitly remove instance from singleton cache: It might have been put there
  94. // eagerly by the creation process, to allow for circular reference resolution.
  95. // Also remove any beans that received a temporary reference to the bean.
  96. destroySingleton(beanName);
  97. throw ex;
  98. }
  99. }
  100. });
  101. bean = getObjectForBeanInstance(sharedInstance, name, beanName, mbd);
  102. }
  103. //prototype的创建(new)
  104. else if (mbd.isPrototype()) {
  105. // It's a prototype -> create a new instance.
  106. Object prototypeInstance = null;
  107. try {
  108. beforePrototypeCreation(beanName);
  109. prototypeInstance = createBean(beanName, mbd, args);
  110. }
  111. finally {
  112. afterPrototypeCreation(beanName);
  113. }
  114. bean = getObjectForBeanInstance(prototypeInstance, name, beanName, mbd);
  115. }
  116. //指定scope上的实例化
  117. else {
  118. String scopeName = mbd.getScope();
  119. final Scope scope = this.scopes.get(scopeName);
  120. if (scope == null) {
  121. throw new IllegalStateException("No Scope registered for scope '" + scopeName + "'");
  122. }
  123. try {
  124. Object scopedInstance = scope.get(beanName, new ObjectFactory<Object>() {
  125. public Object getObject() throws BeansException {
  126. beforePrototypeCreation(beanName);
  127. try {
  128. return createBean(beanName, mbd, args);
  129. }
  130. finally {
  131. afterPrototypeCreation(beanName);
  132. }
  133. }
  134. });
  135. bean = getObjectForBeanInstance(scopedInstance, name, beanName, mbd);
  136. }
  137. catch (IllegalStateException ex) {
  138. throw new BeanCreationException(beanName,
  139. "Scope '" + scopeName + "' is not active for the current thread; " +
  140. "consider defining a scoped proxy for this bean if you intend to refer to it from a singleton",
  141. ex);
  142. }
  143. }
  144. }
  145. //检查需要的类型是否符合bean的实际类型
  146. // Check if required type matches the type of the actual bean instance.
  147. if (requiredType != null && bean != null && !requiredType.isAssignableFrom(bean.getClass())) {
  148. try {
  149. return getTypeConverter().convertIfNecessary(bean, requiredType);
  150. }
  151. catch (TypeMismatchException ex) {
  152. if (logger.isDebugEnabled()) {
  153. logger.debug("Failed to convert bean '" + name + "' to required type [" +
  154. ClassUtils.getQualifiedName(requiredType) + "]", ex);
  155. }
  156. throw new BeanNotOfRequiredTypeException(name, requiredType, bean.getClass());
  157. }
  158. }
  159. return (T) bean;
  160. }

DefaultSingletonBeanRegistry

  1. public Object getSingleton(String beanName, ObjectFactory<?> singletonFactory) {
  2. Assert.notNull(beanName, "'beanName' must not be null");
  3. synchronized (this.singletonObjects) {
  4. Object singletonObject = this.singletonObjects.get(beanName);
  5. if (singletonObject == null) {
  6. if (this.singletonsCurrentlyInDestruction) {
  7. throw new BeanCreationNotAllowedException(beanName,
  8. "Singleton bean creation not allowed while the singletons of this factory are in destruction " +
  9. "(Do not request a bean from a BeanFactory in a destroy method implementation!)");
  10. }
  11. if (logger.isDebugEnabled()) {
  12. logger.debug("Creating shared instance of singleton bean '" + beanName + "'");
  13. }
  14. //防止重新创建bean,告诉spring我要开始创建bean了
  15. beforeSingletonCreation(beanName);
  16. boolean newSingleton = false;
  17. boolean recordSuppressedExceptions = (this.suppressedExceptions == null);
  18. if (recordSuppressedExceptions) {
  19. this.suppressedExceptions = new LinkedHashSet<Exception>();
  20. }
  21. try {
  22. //这里singletonFactory.getObject()才会去真正的创建对象
  23. //创建对象,但创建出来的是代理对象
  24. //先创建原对象,再创建代理对象
  25. singletonObject = singletonFactory.getObject();
  26. newSingleton = true;
  27. }
  28. catch (IllegalStateException ex) {
  29. // Has the singleton object implicitly appeared in the meantime ->
  30. // if yes, proceed with it since the exception indicates that state.
  31. singletonObject = this.singletonObjects.get(beanName);
  32. if (singletonObject == null) {
  33. throw ex;
  34. }
  35. }
  36. catch (BeanCreationException ex) {
  37. if (recordSuppressedExceptions) {
  38. for (Exception suppressedException : this.suppressedExceptions) {
  39. ex.addRelatedCause(suppressedException);
  40. }
  41. }
  42. throw ex;
  43. }
  44. finally {
  45. if (recordSuppressedExceptions) {
  46. this.suppressedExceptions = null;
  47. }
  48. afterSingletonCreation(beanName);
  49. }
  50. if (newSingleton) {
  51. addSingleton(beanName, singletonObject);
  52. }
  53. }
  54. return (singletonObject != NULL_OBJECT ? singletonObject : null);
  55. }
  56. }

解决循环依赖的办法,初始化执行
1.
2.implements InitializingBean
3.[@PostConstruct](https://docs.spring.io/spring-framework/docs/current/spring-framework-reference/core.html#beans-postconstruct-and-predestroy-annotations)

AbstractAutowireCapableBeanFactory

//设置属性
populateBean(beanName, mbd, instanceWrapper);
//执行后置处理器,aop就在这里完成处理
exposedObject = initializeBean(beanName, exposedObject, mbd);

  1. protected Object doCreateBean(final String beanName, final RootBeanDefinition mbd, final Object[] args) {
  2. // Instantiate the bean.
  3. BeanWrapper instanceWrapper = null;
  4. if (mbd.isSingleton()) {
  5. instanceWrapper = this.factoryBeanInstanceCache.remove(beanName);
  6. }
  7. if (instanceWrapper == null) {
  8. instanceWrapper = createBeanInstance(beanName, mbd, args);
  9. }
  10. final Object bean = (instanceWrapper != null ? instanceWrapper.getWrappedInstance() : null);
  11. Class<?> beanType = (instanceWrapper != null ? instanceWrapper.getWrappedClass() : null);
  12. // Allow post-processors to modify the merged bean definition.
  13. synchronized (mbd.postProcessingLock) {
  14. if (!mbd.postProcessed) {
  15. applyMergedBeanDefinitionPostProcessors(mbd, beanType, beanName);
  16. mbd.postProcessed = true;
  17. }
  18. }
  19. // Eagerly cache singletons to be able to resolve circular references
  20. // even when triggered by lifecycle interfaces like BeanFactoryAware.
  21. boolean earlySingletonExposure = (mbd.isSingleton() && this.allowCircularReferences &&
  22. isSingletonCurrentlyInCreation(beanName));
  23. if (earlySingletonExposure) {
  24. if (logger.isDebugEnabled()) {
  25. logger.debug("Eagerly caching bean '" + beanName +
  26. "' to allow for resolving potential circular references");
  27. }
  28. addSingletonFactory(beanName, new ObjectFactory<Object>() {
  29. @Override
  30. public Object getObject() throws BeansException {
  31. return getEarlyBeanReference(beanName, mbd, bean);
  32. }
  33. });
  34. }
  35. // Initialize the bean instance.
  36. Object exposedObject = bean;
  37. try {
  38. //设置属性
  39. populateBean(beanName, mbd, instanceWrapper);
  40. //执行后置处理器,aop就在这里完成处理
  41. if (exposedObject != null) {
  42. exposedObject = initializeBean(beanName, exposedObject, mbd);
  43. }
  44. }
  45. catch (Throwable ex) {
  46. if (ex instanceof BeanCreationException && beanName.equals(((BeanCreationException) ex).getBeanName())) {
  47. throw (BeanCreationException) ex;
  48. }
  49. else {
  50. throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Initialization of bean failed", ex);
  51. }
  52. }
  53. if (earlySingletonExposure) {
  54. Object earlySingletonReference = getSingleton(beanName, false);
  55. if (earlySingletonReference != null) {
  56. if (exposedObject == bean) {
  57. exposedObject = earlySingletonReference;
  58. }
  59. else if (!this.allowRawInjectionDespiteWrapping && hasDependentBean(beanName)) {
  60. String[] dependentBeans = getDependentBeans(beanName);
  61. Set<String> actualDependentBeans = new LinkedHashSet<String>(dependentBeans.length);
  62. for (String dependentBean : dependentBeans) {
  63. if (!removeSingletonIfCreatedForTypeCheckOnly(dependentBean)) {
  64. actualDependentBeans.add(dependentBean);
  65. }
  66. }
  67. if (!actualDependentBeans.isEmpty()) {
  68. throw new BeanCurrentlyInCreationException(beanName,
  69. "Bean with name '" + beanName + "' has been injected into other beans [" +
  70. StringUtils.collectionToCommaDelimitedString(actualDependentBeans) +
  71. "] in its raw version as part of a circular reference, but has eventually been " +
  72. "wrapped. This means that said other beans do not use the final version of the " +
  73. "bean. This is often the result of over-eager type matching - consider using " +
  74. "'getBeanNamesOfType' with the 'allowEagerInit' flag turned off, for example.");
  75. }
  76. }
  77. }
  78. }
  79. // Register bean as disposable.
  80. try {
  81. registerDisposableBeanIfNecessary(beanName, bean, mbd);
  82. }
  83. catch (BeanDefinitionValidationException ex) {
  84. throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Invalid destruction signature", ex);
  85. }
  86. return exposedObject;
  87. }

AbstractAutowireCapableBeanFactory

//解决循环依赖
//wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName); PostConstruct被执行
//invokeInitMethods(beanName, wrappedBean, mbd);InitializingBean被执行

  1. protected Object initializeBean(final String beanName, final Object bean, RootBeanDefinition mbd) {
  2. if (System.getSecurityManager() != null) {
  3. AccessController.doPrivileged(new PrivilegedAction<Object>() {
  4. @Override
  5. public Object run() {
  6. invokeAwareMethods(beanName, bean);
  7. return null;
  8. }
  9. }, getAccessControlContext());
  10. }
  11. else {
  12. invokeAwareMethods(beanName, bean);
  13. }
  14. Object wrappedBean = bean;
  15. if (mbd == null || !mbd.isSynthetic()) {
  16. //第七次执行后置处理器
  17. //执行后置处理器 ----- @PostConstruct
  18. //执行到此时,spring 对象已经产生
  19. wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);
  20. }
  21. try {
  22. //执行springbean 的生命周期回调 init 方法
  23. invokeInitMethods(beanName, wrappedBean, mbd);
  24. }
  25. catch (Throwable ex) {
  26. throw new BeanCreationException(
  27. (mbd != null ? mbd.getResourceDescription() : null),
  28. beanName, "Invocation of init method failed", ex);
  29. }
  30. if (mbd == null || !mbd.isSynthetic()) {
  31. wrappedBean = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);
  32. }
  33. return wrappedBean;
  34. }

无参构造
PostConstruct… <- 后置处理器PostProcessors befor 执行 PostConstruct //第七次执行
InitializingBean… <- 后置处理器PostProcessors after 执行 InitializingBean //第八次执行

先后顺序
1.扫描
2.解析
3.getBean -> 实例化
4.填充属性 -> 自动装配
5.left callback -> InitializingBean or [@PostConstruct](https://docs.spring.io/spring-framework/docs/current/spring-framework-reference/core.html#beans-postconstruct-and-predestroy-annotations)
6.代理 -> 目标对象转换成代理对象

sprigbean 生命周期是没法模拟的

总结步骤

1.利用SimpleInstantiationStragety进行初始化(可以是工厂方式的,也可以是单参数形式的,也可以是无参数的简单初始化),结果是我们得到了一个实例,但是属性都是空的

2.进行属性的注入,这里分别通过byName或者byType获取属性信息,并递归调用getBean方法获取属性bean

3.根据bean实现的aware接口调用相应的aware方法设置资源(beanNameAware,BeanClassLoaderAware,BeanFactoryAware)

4.如果实现了后处理器BeanPostProcessor,则遍历后处理器调用postProcessBeforeInitialization方法处理逻辑

5.调用用户自定义的init-method方法

6.如果实现了InitializingBean接口则调用afterPropertiesSet方法,这个方法只会在所有属性都被设置的时候才会触发

7. 如果实现了后处理器BeanPostProcessor,则遍历后处理器调用postProcessAfterInitialization方法处理逻辑

8.bean可以使用了