Spring Bean Scopes
Can someone explain what the scopes are in Spring beans I've always just used 'prototype' but are there other parameters I can put in place of that?
Example of what I'm talking about
<bean id="customerInfoController" class="com.action.Controller" scope="prototype">
<property name="accountDao" ref="accountDao"/>
<property name="utilityDao" ref="utilityDao"/>
<property name="account_usageDao" ref="account_usageDao"/>
</bean>
Solution 1:
From the spring specs, there are five types of bean scopes supported :
1. singleton(default*)
Scopes a single bean definition to a single object instance per Spring IoC container.
2. prototype
Scopes a single bean definition to any number of object instances.
3. 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.
4. session
Scopes a single bean definition to the lifecycle of a HTTP Session. Only valid in the context of a web-aware Spring ApplicationContext.
5. 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.
*default means when no scope is explicitly provided in the <bean />
tag.
read more about them here: http://static.springsource.org/spring/docs/3.0.0.M3/reference/html/ch04s04.html
Solution 2:
In Spring, bean scope is used to decide which type of bean instance should be returned from Spring container back to the caller.
5 types of bean scopes are supported :
-
Singleton : It returns a single bean instance per Spring IoC container.This single instance is stored in a cache of such singleton beans, and all subsequent requests and references for that named bean return the cached object.If no bean scope is specified in bean configuration file, default to singleton.
-
Prototype : It returns a new bean instance each time when requested. It does not store any cache version like singleton.
-
Request : It returns a single bean instance per HTTP request.
-
Session : It returns a single bean instance per HTTP session (User level session).
-
GlobalSession : It returns a single bean instance per global HTTP session. It is only valid in the context of a web-aware Spring ApplicationContext (Application level session).
In most cases, you may only deal with the Spring’s core scope – singleton and prototype, and the default scope is singleton.
Solution 3:
Just want to update, that in Spring 5, as mentioned in Spring docs, Spring supports 6 scopes, four of which are available only if you use a web-aware ApplicationContext.
singleton (Default) 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 HTTP request has 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 an HTTP Session. Only valid in the context of a web-aware Spring ApplicationContext.
application Scopes a single bean definition to the lifecycle of a ServletContext. Only valid in the context of a web-aware Spring ApplicationContext.
websocket Scopes a single bean definition to the lifecycle of a WebSocket. Only valid in the context of a web-aware Spring ApplicationContext.
Solution 4:
The Spring documentation describes the following standard scopes:
singleton: (Default) 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 HTTP request has 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 an 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.
Additional custom scopes can also be created and configured using a CustomScopeConfigurer
. An example would be the flow
scope added by Spring Webflow.
By the way, you argues that you always used prototype
what I find strange. The standard scope is singleton
and in the application I develop, I rarely need the prototype scope. You should maybe take a look at this.
Solution 5:
Detailed explanation for each scope can be found here in Spring bean scopes. Below is the summary
Singleton - (Default) 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 HTTP request has 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 an 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.