Overview
When defining a <bean> you have the option of declaring scope for that bean
In this blog we will cover
- What are different types of Scope available in Spring..
- Example for each scope type
Types of Scope
Scope | Description |
---|---|
singleton | Scopes a single bean definition to a single object instance per Spring IoC container. |
prototype | Scopes a single bean definition to any number of object instances. |
request | Scopes a single bean definition to the lifecycle of a single HTTP request; that is each and every HTTP request will have its own instance of a bean created off the back of a single bean definition. Only valid in the context of a web-aware Spring ApplicationContext. |
session | Scopes a single bean definition to the lifecycle of a HTTP Session. Only valid in the context of a web-aware Spring ApplicationContext. |
global session | Scopes a single bean definition to the lifecycle of a global HTTP Session. Typically only valid when used in a portlet context. Only valid in the context of a web-aware Spring ApplicationContext. |
singleton scope
- When you define a bean definition and it is scoped as a singleton, then the Spring IoC container will create exactly one instance of the object defined by that bean definition.
- This single instance will be stored in a cache of such singleton beans, and all subsequent requests and references for that named bean will result in the cached object being returned.
- The singleton scope is the default scope in Spring.
Sample
<bean id="accountService" class="com.foo.DefaultAccountService"/> <!-- the following is equivalent, though redundant (singleton scope is the default); using spring-beans-2.0.dtd --> <bean id="accountService" class="com.foo.DefaultAccountService" scope="singleton"/> <!-- the following is equivalent and preserved for backward compatibility in spring-beans.dtd --> <bean id="accountService" class="com.foo.DefaultAccountService" singleton="true"/>
Example using xml
Paint Object
Main class
Example using Annotation
add component scan in xml file
Paint class
Output
The prototype scope
- The non-singleton, prototype scope of bean deployment results in the creation of a new bean instance every time a request for that specific bean is made you should use the prototype scope for all beans that are stateful, while the singleton scope should be used for stateless beans.
Sample
<!-- using spring-beans-2.0.dtd --> <bean id="accountService" class="com.foo.DefaultAccountService" scope="prototype"/> <!-- the following is equivalent and preserved for backward compatibility in spring-beans.dtd --> <bean id="accountService" class="com.foo.DefaultAccountService" singleton="false"/>
Example
Try same code we used for Singleton, just change scope as prototype like this
The other scopes (request
, session
and global session
)
- The other scopes, namely request, session, and global session are for use only in web-based applications
- In order to support the scoping of beans at the request, session, and global session levels (web-scoped beans), some minor initial configuration is required before you can set about defining your bean definitions.
- If you are accessing scoped beans within Spring Web MVC, i.e. within a request that is processed by the Spring DispatcherServlet, or DispatcherPortlet, then no special setup is necessary: DispatcherServlet and DispatcherPortlet already expose all relevant state.
- See here for more detail
You can get the code used above here. Just look for com.main.spring.scope package.
0 Comments