Spring Framework Tutorial

Software Setup and Configuration (STS/Eclipse/IntelliJ)

Core Spring

Spring Annotations

Spring Data

Spring JDBC

Spring Security

Singleton and Prototype Bean Scopes in Java Spring

In Spring Framework, beans can be defined with different scopes which determine their lifecycle and their uniqueness in the application context. Two of the most commonly used bean scopes are Singleton and Prototype.

Singleton Scope (singleton):

When a bean is defined with Singleton scope:

  1. Spring IoC container creates only one instance of the bean, by default.
  2. This single instance is stored in a cache of such singleton beans, and all subsequent requests for that named bean return the cached instance.
  3. It's the default scope for a Spring Bean if you don��t specify any scope.

Example in XML Configuration:

<bean id="exampleBean" class="com.example.ExampleBean" scope="singleton"/>

Example in Java Configuration:

@Bean
@Scope("singleton")
public ExampleBean exampleBean() {
    return new ExampleBean();
}

Prototype Scope (prototype):

  1. If a bean is defined with the prototype scope, the Spring IoC container creates a new bean instance every time a request for that specific bean is made.
  2. The Prototype scope is preferred for all stateful beans, whereas Singleton scope is preferred for stateless beans.

Example in XML Configuration:

<bean id="exampleBean" class="com.example.ExampleBean" scope="prototype"/>

Example in Java Configuration:

@Bean
@Scope("prototype")
public ExampleBean exampleBean() {
    return new ExampleBean();
}

Differences:

  1. Instance Creation:

    • Singleton beans are created only once per container.
    • Prototype beans are created every time they're injected/looked up.
  2. Memory Consumption:

    • Singleton beans can reduce memory consumption because you're reusing an existing instance.
    • Prototype beans can increase memory consumption if created frequently and not managed properly (i.e., if instances are not being released).
  3. Statefulness:

    • Singleton beans should be stateless because stateful singletons can lead to race conditions in a multi-threaded environment.
    • Prototype beans can be stateful since every time the bean is retrieved from the container, a new instance is provided.
  4. Lifecycle:

    • The Spring container manages the complete lifecycle of a Singleton bean, from creation to destruction.
    • For Prototype beans, Spring container manages the initialization phase only; it's up to the client code to clean up and dispose of them.

When to use which scope?

  • Use Singleton for stateless beans where you don��t need to maintain any state.
  • Use Prototype for beans where you want to maintain state, or if they should not be shared.

It's important to understand the needs of your application and the beans within it to select the appropriate scope.

  1. Java Spring @Scope annotation usage:

    • The @Scope annotation in Spring is used to declare the scope of a bean.

    • Example using @Scope for a Singleton bean:

      @Component
      @Scope("singleton")
      public class MySingletonBean {
          // Singleton bean logic
      }
      
    • Example using @Scope for a Prototype bean:

      @Component
      @Scope("prototype")
      public class MyPrototypeBean {
          // Prototype bean logic
      }
      
  2. Managing bean scopes in Spring applications:

    • Bean scopes are managed through the Spring configuration, either using annotations or XML configuration.

    • Example XML configuration for a Singleton bean:

      <bean id="singletonBean" class="com.example.MySingletonBean" scope="singleton"/>
      
    • Example XML configuration for a Prototype bean:

      <bean id="prototypeBean" class="com.example.MyPrototypeBean" scope="prototype"/>
      
  3. Singleton vs. Prototype in Spring with examples:

    • Example illustrating the use of a Singleton bean:

      @Service
      public class MySingletonService {
          // Singleton service logic
      }
      
    • Example illustrating the use of a Prototype bean:

      @Component
      @Scope("prototype")
      public class MyPrototypeComponent {
          // Prototype component logic
      }
      
    • In this scenario, the MySingletonService will be a single shared instance, while each injection of MyPrototypeComponent will result in a new instance.