How do you define a bean scope?
How do you define a bean scope?
To define a bean scope in Spring, you specify the lifecycle and visibility of a bean within the application contexts it is used. The Spring framework supports several scopes, but the most commonly used are singleton and prototype. Additionally, there are web-aware scopes like request, session, application, and websocket.
Here's how you can define a bean scope:
Annotation-based Configuration: You can use the @Scope annotation on the bean definition to specify its scope. For example, to define a singleton scope (which is the default), you can use @Scope("singleton") or simply omit the scope as singleton is the default. For a prototype scope, you would use @Scope("prototype").
@Component
@Scope("prototype")
public class MyPrototypeBean {
// class body
}
Đăng nhập để tham gia thảo luận cùng mọi người.
Chưa có bình luận nào
Hãy là người đầu tiên chia sẻ suy nghĩ của bạn.