Bean 从创建到销毁的整个生命周期。

大致分成四大步

  • 实例化,Instantiation,create instance of the bean using it’s constructor
    • 反射,构造函数,实例化
    • 实例工厂,静态工厂
  • 属性赋值,Property population, inject dependencies into the bean’s properties (via setter methods or constructor injection)
  • 初始化,Initialization
    • Aware 回调,BeanNameAware,BeanClassLoaderAware,BeanFactoryAware,ApplicationContextAware
    • 生命周期回调,三种
    • 如果 Bean 实现了 AOP,创建动态代理
  • 销毁,Destroy

XML 定义方式

<beans xmlns = "http://www.springframework.org/schema/beans"
   xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation = "http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"
   default-init-method = "init" 
   default-destroy-method = "destroy">

   <bean id = "..." class = "...">
      <!-- collaborators and configuration for this bean go here -->
   </bean>
   
</beans>

Bean 容器找到配置文件中 Spring Bean 的定义。

Bean 容器利用 Java Reflection API 创建一个Bean的实例。

如果涉及到一些属性值 利用 set()方法设置一些属性值。

  • 如果 Bean 实现了 BeanNameAware 接口,调用 setBeanName()方法,传入Bean的名字。
  • 如果 Bean 实现了 BeanClassLoaderAware 接口,调用 setBeanClassLoader()方法,传入 ClassLoader对象的实例。
  • 如果Bean实现了 BeanFactoryAware 接口,调用 setBeanClassLoader()方法,传入 ClassLoade r对象的实例。
  • 与上面的类似,如果实现了其他 *.Aware接口,就调用相应的方法。
  • 如果有和加载这个 Bean 的 Spring 容器相关的 BeanPostProcessor 对象,执行postProcessBeforeInitialization() 方法
  • 如果Bean实现了InitializingBean接口,执行afterPropertiesSet()方法。
  • 如果 Bean 在配置文件中的定义包含 init-method 属性,执行指定的方法。
  • 如果有和加载这个 Bean的 Spring 容器相关的 BeanPostProcessor 对象,执行postProcessAfterInitialization() 方法 2016-05-26-spring-bean-beanpostprocessor
  • 当要销毁 Bean 的时候,如果 Bean 实现了 DisposableBean 接口,执行 destroy() 方法。
  • 当要销毁 Bean 的时候,如果 Bean 在配置文件中的定义包含 destroy-method 属性,执行指定的方法。

The text you provided outlines the lifecycle of a Spring Bean within the context of a Spring application. It describes the steps that occur from the creation of a bean to its destruction, detailing how Spring manages the lifecycle using various interfaces and configuration options. Here’s a breakdown of each step:

  1. Bean Definition: The Spring Bean is defined in an XML configuration file, which specifies the bean’s class and any collaborators or configuration needed for that bean.

  2. Bean Creation: The Spring container uses the Java Reflection API to create an instance of the bean as per its definition in the XML file.

  3. Property Setting: If there are any properties specified for this bean, they are set using setter methods.

  4. Aware Interfaces:

    • If the bean implements BeanNameAware, setBeanName() is called with the name of the bean.
    • If it implements BeanClassLoaderAware, setBeanClassLoader() is called with an instance of ClassLoader.
    • Similarly, if it implements other *.Aware interfaces, corresponding methods are called to pass necessary information.
  5. Post-Processor Before Initialization:

    • If there are any BeanPostProcessor objects associated with this Spring container that apply to this bean, their postProcessBeforeInitialization() methods are executed.
  6. After Properties Set:

    • If the bean implements InitializingBean, its afterPropertiesSet() method is invoked.
  7. Custom Init Method:

    • If an init-method attribute is specified in the bean’s XML definition, this method is executed after all properties have been set and after calling any initialization callbacks.
  8. Post-Processor After Initialization:

    • Again, if there are any relevant BeanPostProcessor objects, their postProcessAfterInitialization() methods are executed post-initialization.
  9. Destruction Process:

    • When it’s time for a bean to be destroyed (typically when the application context is closed), if it implements DisposableBean, its destroy() method is called.
    • If a custom destroy method has been specified via a destroy-method attribute in the configuration file, that method gets called as well.

This lifecycle allows developers to hook into different stages of a bean’s life and customize behavior through initialization and destruction callbacks or by implementing specific interfaces provided by Spring.