VOOZH about

URL: https://www.geeksforgeeks.org/advance-java/java-spring-using-scope-annotation-to-set-a-pojos-scope/

⇱ Java Spring - Using @Scope Annotation to Set a POJO's Scope - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Java Spring - Using @Scope Annotation to Set a POJO's Scope

Last Updated : 17 Dec, 2025

In the Spring Framework, each bean defined in the IoC container has a scope that determines its lifecycle and how many instances are created. By default, Spring creates only one instance of a bean (singleton scope), but it also supports other scopes that control how and when new bean instances are generated.

The @Scope annotation is used to define this behavior at either the class level (for components) or method level (for @Bean methods).

Bean Scopes in Spring

  • singleton: Creates a single bean instance per Spring IoC container.
  • prototype: Creates a new bean instance every time it is requested.
  • request: Creates one bean instance per HTTP request (used in web applications).
  • session: Creates one bean instance per HTTP session (used in web applications).

@Scope Annotation

The @Scope annotation defines the lifecycle scope of a bean. By default, every Spring bean is singleton, meaning the same instance is shared across the container.

Syntax

@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Scope {
String value() default "singleton";
}

Usage

  • When used with @Component, it applies to all instances of that class.
  • When used with @Bean, it applies to the specific bean returned by the method.

Example: Demonstrating Bean Scope

We’ll create a simple shopping application to demonstrate how singleton and prototype scopes work.

Project Structure:

👁 ProjectStructure
Project Structure

Step 1: Define ShoppingList Bean (Default Singleton Scope)

Since we didn’t specify any scope, Spring treats this bean as a singleton.

Step 2: Create Device Class

This is a simple POJO representing a product with basic attributes.

Step 3: Create Product Classes

Laptop.java

Mobile.java:

Both classes extend Device and add specific attributes to represent product types.

Step 4: Configure Bean Definitions

This configuration file declares Device beans and enables component scanning for @Component classes.

Step 5: Testing the Application

Here, each call to context.getBean(ShoppingList.class) fetches the same bean instance because the default scope is singleton.

Output (Singleton Scope):

👁 Output
Output

All lists share the same instance, showing singleton behavior.

Changing Scope to prototype

To create a new instance each time, annotate the bean with @Scope("prototype").

Updated ShoppingList.java:

Now, each getBean() call creates a separate instance.

Output (Prototype Scope):

Now if we rerun the Main.java class, we can see the three different shopping lists. The output will be as follows.

👁 Image
Output

Each shopping list is now independent.

Comment
Article Tags:

Explore