Bean 作用域与生命周期管理
学习 Spring 如何创建、设置作用域并销毁 Bean,以及如何通过回调接入其生命周期。
Bean 作用域与生命周期管理 是 CoddyKit 上的免费 Spring Boot 4 Complete Guide 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Spring Boot 4 Complete Guide 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Spring Boot 4 Complete Guide 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
What Is a Bean Scope?
A scope defines how many instances of a bean Spring creates and how long they live. The default scope is singleton: one shared instance per container.
Singleton Scope
With singleton, every injection point receives the same object. This is ideal for stateless services and is the most common choice.
@Service
public class PricingService {
// one shared instance across the app
}Prototype Scope
The prototype scope creates a new instance every time the bean is requested. Use it for stateful or short-lived objects.
@Component
@Scope("prototype")
public class ReportBuilder {
}Web-Aware Scopes
In web apps you also have request and session scopes, tying a bean's lifetime to a single HTTP request or a user session respectively.
- request — one per HTTP request
- session — one per user session
Choosing the Right Scope
Default to singleton for stateless logic. Reach for other scopes only when a bean must hold per-request or per-instance state, since wider sharing of mutable state causes bugs.
The Bean Lifecycle
Spring instantiates a bean, injects its dependencies, runs initialization callbacks, serves it during the app's life, and finally runs destruction callbacks on shutdown.
Initialization with @PostConstruct
Annotate a method with @PostConstruct to run setup logic after dependencies are injected, such as opening a connection or warming a cache.
@PostConstruct
public void init() {
System.out.println("Bean is ready");
}Cleanup with @PreDestroy
A @PreDestroy method runs just before the bean is removed, letting you release resources cleanly.
@PreDestroy
public void cleanup() {
System.out.println("Releasing resources");
}Lazy Initialization
By default singletons are created at startup. Marking a bean @Lazy defers creation until it is first needed, which can speed up boot time for rarely used beans.
@Lazy
@Service
public class HeavyService {
}Prototype Beans and Destruction
Be aware that Spring does not manage the full destruction lifecycle of prototype beans. The container hands them off and forgets them, so you must release their resources yourself.
Putting It Together
A typical singleton service uses @PostConstruct to initialize and @PreDestroy to clean up, giving you precise control over startup and shutdown behavior.
@Service
public class CacheService {
@PostConstruct void load() {}
@PreDestroy void flush() {}
}Quick Check
Test your understanding of bean scopes and lifecycle.
Recap
You learned the singleton, prototype, request, and session scopes, the bean lifecycle, and how to hook into it with @PostConstruct, @PreDestroy, and @Lazy.
常见问题解答
「Bean 作用域与生命周期管理」课时是免费的吗?
是的 — 「Bean 作用域与生命周期管理」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Spring Boot 4 Complete Guide 课程的其余内容,请升级到 CoddyKit PRO。 Spring Boot 4 Complete Guide 课程共包含 4 节课。
「Bean 作用域与生命周期管理」这节课中我会学到什么?
学习 Spring 如何创建、设置作用域并销毁 Bean,以及如何通过回调接入其生命周期。 你通过在浏览器中直接运行的动手代码来练习 Spring Boot 4 Complete Guide,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Spring Boot 4 Complete Guide 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Spring Boot 4 Complete Guide 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。
「Bean 作用域与生命周期管理」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Spring Boot 4 Complete Guide 课中编写并运行代码吗?
能。每节 Spring Boot 4 Complete Guide 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 了解 Spring IoC 容器
- 在实践中进行依赖注入
- 外部化应用程序属性
- Bean 作用域与生命周期管理