Bean scope is used to decide which type of bean instance should be return to the caller. Here’s 5 types of bean scopes supported :
- singleton – Return a single bean instance per Spring IoC container
- prototype – Return a new bean instance each time when requested
- request – Return a single bean instance per HTTP request. *
- session – Return a single bean instance per HTTP session. *
- globalSession – Return a single bean instance per global HTTP session. *
In most cases, you may only deal with the Spring’s core scope – singleton and prototype, and the default scope is singleton.
<bean id="customerService" class="com.mkyong.customer.services.CustomerService" scope="prototype"/>
Bean configuration file, no bean scope is specified, default to singleton.
<bean id="customerService"
class="com.mkyong.customer.services.CustomerService" />
It is same as :
<bean id="customerService" class="com.mkyong.customer.services.CustomerService" singleton="true"/>
Using Annotation:
@Service
@Scope("prototype")
public class CustomerService{}
Enable the auto scanning:
<context:component-scan base-package="com.mkyong.customer" />
No comments:
Post a Comment