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.

相关推荐
小江的记录本10 小时前
【Java基础】泛型:泛型擦除、通配符、上下界限定(附《思维导图》+《面试高频考点清单》)
java·数据结构·后端·mysql·spring·面试·职场和发展
Don.TIk13 小时前
ChapterOne-搭建项目骨架
java·spring·spring cloud·mybatis
南极企鹅13 小时前
事务&@Transactional注解
java·数据库·spring·oracle·mybatis
me83215 小时前
【AI】踩坑LangChain4j集成千问模型:版本适配问题完整解决历程
java·spring·阿里云·ai
吴声子夜歌15 小时前
状态机——Spring State Machine
java·后端·spring
星秀日19 小时前
Spring Boot + Sa-Token 实时聊天系统:用户注册流程源码深度剖析
java·人工智能·spring·状态模式
夜白宋20 小时前
【Spring深入】一、事务
spring
Mahir0820 小时前
Spring 事务深度解析:核心原理与 12 种事务失效场景全解
java·spring·面试·事务失效
摇滚侠21 小时前
SpringCloud 面试题 真正的 offer 偏方 Java 基础 Java 高级
java·spring·spring cloud
敖正炀21 小时前
Spring 设计哲学再探:约定优于配置、误用与反模式
spring boot·spring