“The more that you read, the more things you will know,
the more that you learn, the more places you’ll go.”
—Dr. Seuss
Overview
Java is the base of the spring framework. Java is an object-oriented programming language so the spring framework also inherits OOPs fundamentals like class, object, etc. Here Bean refers to a java object.
Bean can be instantiated in containers like Servlet Context, Application Context, and WebApplication Context.
There are several types of scopes defined for beans for different use-cases in the spring framework.
Here, I have used spring annotations to create a bean and to provide scope. @Component is used at the class level so that the IoC container can create a bean for a class.
1. Singleton Scope
As the name suggests, a One-time object will be instantiated for a class for each application context. This scope is the default scope so if the class is annotated with
@Component, an application context will return the same bean every time when it is being requested. @Configuration, @RestController, @Controller, @Service, and @Repository are wrapper annotations on @Component. Beans of all the classes annotated with any of these annotations will have a singleton scope of bean.
Below is a code snippet to create a bean having Websocket scope.
@Component // default scope - singleton - 1st way
// hardcoded scope
//@Scope(scopeName = "singleton") - 2nd way
// standard way
//@Scope(value = ConfigurableBeanFactory.SCOPE_SINGLETON) - 3rd way
public class DataSource {
String url;
String username;
char[] password;
}
It is optional to annotate the above class with @Scope annotation.
Generally, Repositories, Services, and Configurations beans do not have states that keep changing or are different for each request. That is a good reason to create a single instance instead of a new instance for every new request(bean request). Singleton Scope is slightly different than Singleton Desing Pattern because Singleton Design Pattern says there should be a single object for a given class in memory while as per singleton scope, there should be a single bean per container.
The singleton scope should be used for stateless beans.
2. Prototype Scope
This scope allows IoC to create a new instance of the bean when it is requested. The container will have multiple beans of the same type. The bean will not be created when the application starts but it will be created when the bean is requested or there is a dependency of the prototype bean inside a singleton scoped bean.
This scope is recommended for stateful objects since their states won't be shared by other components.
Below is a code snippet to create a bean having a Prototype scope.
@Component
// hardcoded scope
//@Scope(scopeName = "prototype") - 1st way
// standard way
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE) - 2nd way
public class StatefulEntity {
}
Spring does not manage the complete lifecycle of a prototype bean. It is the responsibility of the client code to clean up prototype scoped objects and release any expensive resources that the prototype bean(s) are holding onto.
Above Both scopes are applicable for ApplicationContext, and WebApplicationContext. Request, Session, Application, and Websocket scopes are web-aware scopes used in web applications only. If the code tries to register beans having these scopes in ApplicationContext then it will throw an IlligleStateException while requesting for the bean.
3. Request Scope
When the server receives a new client request, The bean will be instantiated in the WebApplicationContext if its scope is Request.
Code snippet to create a bean having Request scope.
@Component
// hardcoded scope - 1st way
//@Scope(scopeName = "request", proxyMode = ScopedProxyMode.TARGET_CLASS)
// standard way - 2nd way
//@Scope(value = WebApplicationContext.SCOPE_REQUEST, proxyMode = ScopedProxyMode.TARGET_CLASS)
// request scope annotation
@RequestScope - 3rd way
public class SecurityContextBean {
}
The basic difference between Prototype scope and Request scope is that a bean is instantiated only one time for each client request. That bean might be requested many times but WebApplicationContext will return the same bean while in Prototype Scope, a new bean instantiated for each bean request.
4. Session Scope
There will be only one bean created for the component when the HTTP new session starts.
Code snippet to create a bean having Session scope.
@Component
// hardcode way - 1st way
//@Scope(scopeName = "session", proxyMode = ScopedProxyMode.TARGET_CLASS)
// standard way - 2nd way
//@Scope(value = WebApplicationContext.SCOPE_SESSION, proxyMode = ScopedProxyMode.TARGET_CLASS)
// session scope annotation
@SessionScope - 3rd way
public class SessionScopeBean {
}
When the HTTP Session is eventually discarded, the bean that is scoped to that particular HTTP Session will also be discarded.
5. Application Scope
Application scope operates on Servlet Context. There will be one ServletContext per server. ServletContext can have multiple dispatcher servlets and it can have only one WebApplicationContext.
So bean with Application scope is instantiated once only when it is required in ServletContext and shared among all the dispatcher servlets or ApplicationContexts if the bean is requested.
Code snippet to create a bean having Application scope.
@Component
// hardcode way - 1st way
//@Scope(scopeName = "application", proxyMode = ScopedProxyMode.TARGET_CLASS)
// standard way - 2nd way
//@Scope(value = WebApplicationContext.SCOPE_APPLICATION, proxyMode = ScopedProxyMode.TARGET_CLASS)
// application scope annotation - 3rd way
@ApplicationScope
public class ApplicationScopeBean {
}
6. Websocket Scope
Bean is created only one time per WebSocket Session.
Code snippet to create a bean having Websocket scope.
@Component
@Scope(scopeName = "websocket", proxyMode = ScopedProxyMode.TARGET_CLASS)
public class WebSocketScopeBean {
}
May 06, 2022
Tags :
Spring
Subscribe by Email
Follow Updates Articles from This Blog via Email
No Comments
Please comment here...