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.

相关推荐
MerlinTheMagic13 分钟前
Spring AI 核心概念
java·人工智能·spring
酷ku的森38 分钟前
Spring Web MVC入门
spring·mvc
西洼工作室1 小时前
黑马商城-微服务笔记
java·笔记·spring·微服务
异常君1 小时前
Spring 定时任务执行一次后不再触发?5 大原因与解决方案全解析
java·后端·spring
等什么君!2 小时前
SpringMVC处理请求映射路径和接收参数
java·开发语言·spring
-曾牛3 小时前
Spring Boot常用注解详解:实例与核心概念
java·spring boot·后端·spring·java-ee·个人开发·spring boot 注解
Code哈哈笑5 小时前
【Spring Boot】深入解析:#{} 和 ${}
java·spring boot·后端·spring·mybatis
云之兕5 小时前
在面试中被问到spring是什么?
spring·面试·职场和发展
brevity_souls5 小时前
Spring Security
java·mysql·spring
yuren_xia8 小时前
使用 JUnit 4在 Spring 中进行单元测试的完整步骤
spring·junit·单元测试