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 小时前
Spring Insight 里如何把 Span 画成瀑布时间线
后端·spring·spring cloud·springboot·监控·apm
Bs_MoneyMagnet4 小时前
基于springboot+vue的非遗物质文化遗产系统的设计与实现 源码+文档
java·vue.js·spring boot·后端·spring·毕业设计·计算机毕业设计
OxYGC5 小时前
[AI工程] Spring AI 第十四篇:Agent 五种模式在 2.0 里怎么写
java·spring·ai·ai编程
2501_933923255 小时前
Spring Bean作用域揭秘:单例、原型、请求与会话的区别
java·后端·spring·java-ee
TheITSea8 小时前
实习经历详细逻辑
spring
名字还没想好☜9 小时前
Spring Boot 优雅停机实战:等在途请求处理完再退出,配合 K8s preStop 别丢请求
java·后端·spring
代码不停9 小时前
Spring Boot日志
spring boot·spring
逃逸线LOF9 小时前
Spring Boot4 整合 Shiro3 & Thymeleaf
spring boot·spring
右耳朵猫AI9 小时前
Java周刊2026W38 | Micronaut 修补三漏洞、JDK 27 提速 54%、Jetty 修复抖动测试
java·后端·spring
她说..10 小时前
常见设计模式-模板方法模式
java·spring·设计模式·springboot