Spring bean
An object that the Spring container creates, configures and manages, and can inject into other beans.
In more detail
Classes become beans through annotations such as @Service or @RestController, or through configuration methods. The container resolves each bean's dependencies, runs its lifecycle callbacks, and by default shares one instance per application. Beans should be stateless unless their scope says otherwise.
Related terms
- inversion of control — A design in which a piece of code gives up control of some decisions, such as how its dependencies are created or what it renders, to the code around it.
- bean scope — The rule for how many instances of a Spring bean exist: one shared singleton by default, or a new one per use, request or session.
- bean lifecycle — The stages a Spring bean passes through: construction, dependency injection, initialization, use, and destruction when the context closes.
- @Service — A Spring annotation that marks a class as a service bean so component scanning discovers and creates it.
Taught in
- Dependency Injection And The IoC Container — Modern Java: Virtual Threads & High-Throughput Services
- Spring Boot Auto Configuration — Modern Java: Virtual Threads & High-Throughput Services
- Configuration And Profiles — Modern Java: Virtual Threads & High-Throughput Services
- The Spring Bean Lifecycle — Modern Java: Virtual Threads & High-Throughput Services
- Testing APIs With MockMvc — Modern Java: Virtual Threads & High-Throughput Services