Spring中Bean的作用域有6种:
1,singleton:springIOC容器只创建该bean唯一的实例。
2,propotype:每一次请求都会产生一个新的bean实例。相当于new操作。
以下request,session,globleSession都是web应用中的Bean:
3,request:针对于每一次的http请求都会产生不同的bean。
4,session:针对每一次httpSesion请求都回产生不同的Bean实例。
5,globleSession:每个全局的httpsession对应的一个Bean实例。
6,自定义作用域:Spring的bean作用域机制是可以扩展的。
定义一个bean的作用域:
<bean id="loginAction" class="org.han.action.LoginAction" scope="singleton">
针对与web应用中的Bean作用域的管理,需要在web.xml中添加配置:
servlet2.4以上的容器:
<web-app>
<listener>
<listener-class>
org.springframework.web.context.request.RequestContextListener
</listener-class>
</listener>
</web-app>
servlet2.4以下的容器:
<filter>
<filter-name>requestContextFilter</filter-name>
<filter-class>
org.springframework.web.filter.RequestContextFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>requestContextFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>