Spring Framework Tutorial
Software Setup and Configuration (STS/Eclipse/IntelliJ)
Core Spring
Spring Annotations
Spring Data
Spring JDBC
Spring Security
In the Spring framework, beans are managed objects that live within the container, and they have a lifecycle. The @Scope
annotation is used to define the lifecycle and the boundaries of a bean - essentially, how and when a bean should be instantiated and discarded. Here's an overview of the @Scope
annotation and how to set a Plain Old Java Object (POJO)'s scope in Spring.
The @Scope
annotation defines the scope of a single bean definition. It determines the lifecycle of the bean, specifying how many instances should be created and when they are instantiated and discarded.
singleton (default): A single instance of the bean is created and shared in the entire Spring container. This is the default scope if none is specified.
@Component @Scope("singleton") public class SingletonBean { // ... }
prototype: A new instance of the bean is created every time it's requested or injected.
@Component @Scope("prototype") public class PrototypeBean { // ... }
request: For web applications, a new bean instance is created for each HTTP request. This scope is only valid in the context of a web-aware Spring ApplicationContext
.
@Component @Scope("request") public class RequestScopedBean { // ... }
session: For web applications, a new bean is created for each user session. Like 'request', this scope is valid only in a web-aware Spring context.
@Component @Scope("session") public class SessionScopedBean { // ... }
application: A single bean instance is used for the lifetime of the entire application. This is typical for web applications where you want to share a bean at the application level, not just the session or request level.
websocket: A single bean instance is scoped to the lifecycle of a WebSocket.
Sometimes, you might want to create a proxy object to handle the bean's scope, especially if you want to inject a shorter-lived bean into a longer-lived bean (for example, injecting a request-scoped bean into a singleton bean). Spring provides the proxyMode
attribute of the @Scope
annotation for this purpose.
For instance, if you want to inject a request-scoped bean into a singleton bean, you can set proxyMode
to ScopedProxyMode.TARGET_CLASS
:
@Component @Scope(value = "request", proxyMode = ScopedProxyMode.TARGET_CLASS) public class RequestScopedBean { // ... }
The @Scope
annotation is essential for managing the lifecycle of beans in the Spring container. By defining the scope, you have precise control over how beans are created, shared, and discarded. This flexibility ensures that resources are used optimally and that beans can be integrated seamlessly in various contexts, from single-user applications to multi-user web applications.
Using @Scope to Define Bean Scope in Spring:
@Scope
annotation in Spring is used to define the scope of a bean. Scopes determine the lifecycle and visibility of a bean within the Spring IoC container.@Component @Scope("singleton") public class MySingletonBean { // Singleton bean content }
How to Set Singleton Scope Using @Scope in Spring:
@Component @Scope("singleton") public class MySingletonBean { // Singleton bean content }
Setting Prototype Scope with @Scope Annotation in Spring:
@Component @Scope("prototype") public class MyPrototypeBean { // Prototype bean content }
Scoping Strategies for POJOs with @Scope in Spring:
@Scope
annotation. This can be beneficial for managing stateful components.@Component @Scope("session") public class SessionScopedBean { // Session-scoped bean content }
Session and Request Scopes Using @Scope in Spring:
@Scope
to define beans that are specific to a user session or an HTTP request.@Component @Scope("session") public class SessionScopedBean { // Session-scoped bean content }
Thread Scope with @Scope Annotation in Spring:
@Scope
.@Component @Scope("thread") public class ThreadScopedBean { // Thread-scoped bean content }
Lazy Initialization and Eager Initialization with @Scope:
@Scope
annotation can be used to control the initialization strategy of a bean. Use @Lazy
for lazy initialization and omit it for eager initialization.@Component @Scope("singleton") @Lazy public class LazyInitializedBean { // Lazy-initialized singleton bean content }