Spring's Bean life cycle source code analysis (Part 2)

Bean destruction process

Bean destruction occurs during the shutdown process of the Spring container.
When the Spring container is closed, such as

AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
UserService userService = (UserService) context.getBean("userService");
userService.test();

// 容器关闭
context.close();

During the Bean creation process , at the end (after initialization), there is a step to determine whether the currently created Bean is DisposableBean :

  • Whether the current Bean implements the DisposableBean interface
  • Or, whether the current Bean implements the AutoCloseable interface
  • Whether destroyMethod is specified in BeanDefinition
  • Call DestructionAwareBeanPostProcessor.requiresDestruction(bean) for judgment
    • ApplicationListenerDetector directly makes ApplicationListener DisposableBean
    • The method in InitDestroyAnnotationBeanPostProcessor that has @PreDestroy annotation is DisposableBean
  • Adapt the Bean that meets any of the above conditions into a DisposableBeanAdapter object and store it in disposableBeans (a LinkedHashMap)

During the Spring container shutdown process:

  • First publish the ContextClosedEvent event
  • Call the onCloese() method of lifecycleProcessor
  • Destroy a singleton bean
    • Traverse disposableBeans
      1. Remove each disposableBean from the singleton pool
      2. Call destroy() of disposableBean
      3. If this disposableBean is also dependent on other beans, then other beans must also be destroyed
      4. If this disposableBean also contains inner beans, remove these beans from the singleton pool (inner beans refer to https://docs.spring.io/spring-framework/docs/current/spring-framework-reference/core.html#beans-inner-beans )
    • Clear manualSingletonNames, which is a Set that stores the beanName of the singleton bean manually registered by the user.
    • Clear allBeanNamesByType, which is a Map. The key is the bean type and the value is the array of all beanNames of this type.
    • Clear singletonBeanNamesByType, similar to allBeanNamesByType, except that only singleton beans are stored

This involves a design pattern:
  when the adapter mode is destroyed, Spring will find the Bean that implements the DisposableBean interface.
  But when we define a Bean, if the Bean implements the DisposableBean interface, or implements the AutoCloseable interface, or specifies destroyMethodName in the BeanDefinition, then the Bean belongs to "DisposableBean", and these beans must call the corresponding method when the container is closed. Destruction method.
  Therefore, adaptation is required here to adapt the DisposableBean interface or AutoCloseable interface to the DisposableBean interface, so the DisposableBeanAdapter is used.
  Classes that implement the AutoCloseable interface will be encapsulated into DisposableBeanAdapter, and DisposableBeanAdapter implements the DisposableBean interface.

Guess you like

Origin blog.csdn.net/beautybug1126/article/details/132371846