SoFunction
Updated on 2025-04-05

Detailed explanation of Spring Bean's Scope Scope Scope

Spring Bean's Scope Scope

Bean Scope in Spring framework is used to control the life cycle and visibility of a bean.

Spring provides multiple scopes, each scope determines how beans are created and managed in applications.

Here are the most commonly used scopes and features in Spring:

Scope scope type

1. Singleton (singleton)

  • Default scope:If the scope is not explicitly specified, the default is Singleton.
  • life cycle:In the entire application, there is only one instance. This instance is created when Spring container starts and exists throughout the application run.
  • Applicable scenarios:Suitable for stateless beans, such as beans in the service layer and DAO layer.
 @Component
 @Scope(ConfigurableBeanFactory.SCOPE_SINGLETON)
 public class MySingletonComponent {
     // ...
 }

2. Prototype (prototype)

  • life cycle:A new instance is created for each request. After creating a prototype bean, Spring containers no longer manage their life cycle.
  • Applicable scenarios:Suitable for stateful beans, such as beans that require each request to be processed independently.
 @Component
 @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
 public class MyPrototypeComponent {
     // ...
 }

In Spring framework, use@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)Modified@ComponentIndicates that the component is a prototype scoped bean.

This means that every time the bean is requested through the Spring container, a new instance is created.

Unlike singleton-scoped beans, prototype-scoped beans are not cached, and a new instance is returned for each request.

life cycle

Create a new instance with each request

  • Each call()When a new one is createdMyPrototypeComponentExample.
  • This means that each request is isolated and each instance has an independent state.

Lifecycle Management

  • The life cycle of a prototype-scoped bean is managed by the client code that created it. Spring containers are only responsible for initializing and assembling beans. Once the bean is created and returned to the client code, the container no longer manages its life cycle.
  • The container will not call the destroy method (e.g.@PreDestroyAnnotated method) to clean up resources, because the container does not know when these beans should be destroyed, which in turn means that the prototype beans are no longer managing their life cycle after being created by Spring.

In Spring framework, when the scope of a bean is set toSCOPE_PROTOTYPEWhen this bean is started,Won'tBeing automatically instantiated. Instead, a new instance is created every time the bean is requested through the Spring container.

Key Summary

Singleton scope

  • Default scope.
  • It is instantiated once when the container starts.
  • There is only one instance throughout the application life cycle.

Prototype scope

  • A new instance is created every time a request is requested (also it is created every time a request is requested. If a class defined as that scope is not used anywhere, then you can think it is no different from a normal Java class).
  • The request method includes injecting the bean into other beans, and each injection is an independent bean. Or useWhen obtaining beans.
  • The container is not automatically instantiated when it starts.
  • The container no longer manages its life cycle after the bean is created.
  • It is especially suitable for Java classes that need to be injected into other classes in Spring, but this class needs to be instantiated when it is required by some scenarios, such as thread task classes that implement the Runnable interface.

Example:

@Data
@Accessors(chain = true)
@Component
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class Task implements Runnable {

    private TaskInfo taskInfo;

    // Inject object    @Autowired
    private TaskService taskService;

    @Override
    public void run() {
        (taskInfo);
    }
}
// Call sample codeApplicationContext context = xxx;
ExecutorService executor = xxx;
TaskInfo taskInfo = xxx;
// Here, each time the getBean gets a new instanceTask task = ().setTaskInfo(taskInfo);
(task);

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.