Differen types of spring bean scopes
In the spring bean configurations, bean attribute called 'scope' defines what kind of object has to created and returned.
There are 5 types of bean scopes available, they are:
1) singleton: Returns a single bean instance per Spring IoC container.
2) prototype: Returns a new bean instance each time when requested.
3) request: Returns a single instance for every HTTP request call.
4) session: Returns a single instance for every HTTP session.
5) global session: global session scope is equal as session scope on portlet-based web applications.
If no bean scope is specified in bean configuration file, then it will be by default 'singleton'.
Here is an example for specifying bean scope configuration:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- Since scope is not defined, it assigns default value 'singleton'.
It creates only one instance per spring IoC. -->
<bean id="articleBean" class="com.java2novice.bean.Article" />
<!-- Since scope is prototype, it creates and returns ColorBean for every
call-->
<bean id="clrBean" class="com.java2novice.bean.ColorBean" scope="prototype"/>
<bean id="ticketBean" class="com.java2novice.bean.TicketManager" scope="singleton"/>
</beans>
|
|