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.

相关推荐
开开心心就好3 小时前
Word图片格式调整与转换工具
java·javascript·spring·eclipse·pdf·word·excel
一只码代码的章鱼5 小时前
spring -MVC-02
java·spring·mvc
IT光6 小时前
Redis 五种类型基础操作(redis-cli + Spring Data Redis)
java·数据库·redis·spring·缓存
沛沛老爹7 小时前
软件架构风格系列(3):管道 - 过滤器架构
spring·数据架构·软件架构风格·架构入门·管道-过滤器
努力学习的明9 小时前
Spring MVC 中请求处理流程及核心组件解析
java·spring·mvc
亚林瓜子10 小时前
AWS Elastic Beanstalk控制台部署Spring极简工程(LB版)
spring·云计算·aws·elb·beanstalk·alb·eb
冬日枝丫10 小时前
【spring】spring学习系列之六:spring的启动流程(下)
java·学习·spring
沛沛老爹20 小时前
软件架构风格系列(2):面向对象架构
spring·软件架构风格·面向对象架构·架构入门
lyrhhhhhhhh20 小时前
Spring 框架 JDBC 模板技术详解
java·数据库·spring