Spring Bean lifecycle

Bean life cycle

It is easy to understand the life cycle of Spring beans. When a bean is instantiated, it may need to perform some initialization to make it available. Similarly, when beans are no longer needed and are removed from the container, some cleanup may be required.

Although there are still some activities between bean instantiation and destruction, this chapter will only discuss two important life cycle callback methods, which are necessary when initializing and destroying beans.

To define the installation and disassembly of a bean, we only need to declare the with init method and/or destroy method parameters. The init method attribute specifies a method that is called immediately when the bean is instantiated. Similarly, destroy method specifies a method that can be called only after the bean is removed from the container.

Instantiation (实例化):

The Spring container is responsible for instantiating beans.

This is the first step in the bean's lifecycle.

Dependency Injection (依赖注入):

After instantiation, Spring container injects dependencies into the bean's properties.

This step ensures that the bean has all the necessary resources to function properly.

implements aware interface

this step you can set properties of spring container.

Bean Post-Processing (Bean后处理):

Spring provides the BeanPostProcessor interface that allows you to modify the bean before it's returned to the caller.

Methods like postProcessBeforeInitialization and postProcessAfterInitialization are called during this phase.

Initialization (初始化):

After all dependencies have been injected, and bean post-processing is complete, the bean's initialization method is called.

This can be done by implementing the InitializingBean interface and providing an afterPropertiesSet method, or by using the @PostConstruct annotation.

Ready for Use (准备就绪):

After initialization, the bean is ready to be used by the application.

The container will now return the bean to the caller.

Usage (使用):

The bean is now in use by the application, performing its intended tasks.

Destruction (销毁):

When the container is shutting down, it will call the bean's destruction method.

This can be done by implementing the DisposableBean interface and providing a destroy method, or by using the @PreDestroy annotation.

Cleanup (清理):

After the bean's destruction method has been called, the bean instance is no longer in use.

The container will typically handle the cleanup of resources associated with the bean.

相关推荐
曲莫终1 小时前
SpEl表达式之强大的集合选择(Collection Selection)和集合投影(Collection Projection)
java·spring boot·spring
kaikaile19952 小时前
解密Spring Boot:深入理解条件装配与条件注解
java·spring boot·spring
广州山泉婚姻2 小时前
解锁高效开发:Spring Boot 3和MyBatis-Flex在智慧零工平台后端的应用实战
人工智能·spring boot·spring
三目君2 小时前
SpringMVC异步处理Servlet
java·spring·servlet·tomcat·mvc
fie88892 小时前
Spring MVC扩展与SSM框架整合
java·spring·mvc
用户590336360594 小时前
spring Context中代理对象和原始对象存储关系
spring
YuTaoShao7 小时前
Java八股文——Spring「SpringMVC 篇」
java·开发语言·spring
33255_40857_280598 小时前
深入解析Spring启动流程:从源码角度一步步揭秘Spring的魔法
java·spring
天天摸鱼的java工程师8 小时前
八年开发视角:Spring 框架在企业级应用中的实战场景解析
java·后端·spring
chanalbert8 小时前
Spring AOP 模块设计文档
后端·spring