Spring Framework Tutorial

Software Setup and Configuration (STS/Eclipse/IntelliJ)

Core Spring

Spring Annotations

Spring Data

Spring JDBC

Spring Security

Spring @Scope Annotation to Set a POJO's Scope

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.

1. @Scope Annotation:

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.

2. Common Bean Scopes:

  1. 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 {
        // ...
    }
    
  2. prototype: A new instance of the bean is created every time it's requested or injected.

    @Component
    @Scope("prototype")
    public class PrototypeBean {
        // ...
    }
    
  3. 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 {
        // ...
    }
    
  4. 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 {
        // ...
    }
    
  5. 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.

  6. websocket: A single bean instance is scoped to the lifecycle of a WebSocket.

3. Proxy Mode:

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 {
    // ...
}

Conclusion:

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.

  1. Using @Scope to Define Bean Scope in Spring:

    • Description: The @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.
    • Code:
      @Component
      @Scope("singleton")
      public class MySingletonBean {
          // Singleton bean content
      }
      
  2. How to Set Singleton Scope Using @Scope in Spring:

    • Description: Singleton scope is the default scope in Spring, where a single instance of the bean is shared across the entire application context.
    • Code:
      @Component
      @Scope("singleton")
      public class MySingletonBean {
          // Singleton bean content
      }
      
  3. Setting Prototype Scope with @Scope Annotation in Spring:

    • Description: Prototype scope creates a new instance of the bean whenever it is requested, ensuring that each injection point receives a unique instance.
    • Code:
      @Component
      @Scope("prototype")
      public class MyPrototypeBean {
          // Prototype bean content
      }
      
  4. Scoping Strategies for POJOs with @Scope in Spring:

    • Description: Apply scoping strategies to Plain Old Java Objects (POJOs) using the @Scope annotation. This can be beneficial for managing stateful components.
    • Code:
      @Component
      @Scope("session")
      public class SessionScopedBean {
          // Session-scoped bean content
      }
      
  5. Session and Request Scopes Using @Scope in Spring:

    • Description: Spring supports session and request scopes for web applications. Use @Scope to define beans that are specific to a user session or an HTTP request.
    • Code:
      @Component
      @Scope("session")
      public class SessionScopedBean {
          // Session-scoped bean content
      }
      
  6. Thread Scope with @Scope Annotation in Spring:

    • Description: Thread scope is a custom scope in Spring that associates a bean with a specific thread. Implement thread-scoped beans using @Scope.
    • Code:
      @Component
      @Scope("thread")
      public class ThreadScopedBean {
          // Thread-scoped bean content
      }
      
  7. Lazy Initialization and Eager Initialization with @Scope:

    • Description: The @Scope annotation can be used to control the initialization strategy of a bean. Use @Lazy for lazy initialization and omit it for eager initialization.
    • Code:
      @Component
      @Scope("singleton")
      @Lazy
      public class LazyInitializedBean {
          // Lazy-initialized singleton bean content
      }