0
0
Spring Bootframework~5 mins

@Scope for bean scope in Spring Boot

Choose your learning style9 modes available
Introduction

The @Scope annotation controls how Spring creates and manages instances of a bean. It helps decide if a new object is made each time or if the same one is reused.

When you want a single shared instance of a service across the whole application.
When you need a new instance of a bean every time it is requested.
When you want a bean to be created once per user session in a web app.
When you want a bean to be created once per HTTP request.
When you want to manage bean lifecycles differently for testing or performance.
Syntax
Spring Boot
@Scope("scopeName")
public class BeanClass {
    // bean code
}

The scopeName is usually a string like singleton or prototype.

Common scopes include singleton, prototype, request, and session.

Examples
This bean is created once and shared everywhere.
Spring Boot
@Scope("singleton")
public class MyService {
    // shared instance
}
A new bean instance is created every time it is requested.
Spring Boot
@Scope("prototype")
public class MyPrototypeBean {
    // new instance each time
}
In web apps, this bean is created once per HTTP request.
Spring Boot
@Scope("request")
public class MyRequestBean {
    // one instance per HTTP request
}
This bean is created once per user session in a web application.
Spring Boot
@Scope("session")
public class MySessionBean {
    // one instance per user session
}
Sample Program

This Spring Boot app defines a MessagePrinter bean with prototype scope. When the app runs, it injects two instances of MessagePrinter. Because of the prototype scope, these two instances are different objects. The program prints their references to show they are not the same.

Spring Boot
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@Component
@Scope("prototype")
public class MessagePrinter {
    public void printMessage() {
        System.out.println("Hello from MessagePrinter instance: " + this);
    }
}

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ScopeExampleApplication implements CommandLineRunner {

    @Autowired
    private MessagePrinter printer1;

    @Autowired
    private MessagePrinter printer2;

    public static void main(String[] args) {
        SpringApplication.run(ScopeExampleApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        printer1.printMessage();
        printer2.printMessage();
    }
}
OutputSuccess
Important Notes

Singleton scope is the default in Spring, meaning one shared instance.

Prototype scope creates a new instance every time the bean is requested.

Web scopes like request and session only work in web-aware Spring contexts.

Summary

@Scope controls how many instances of a bean Spring creates.

Use singleton for shared beans and prototype for new instances each time.

Web apps can use request and session scopes for per-request or per-user beans.