SPRING中属性SCOPE的prototype
默认情况下,从bean工厂所取得的实例为Singleton(bean的singleton属性)
Singleton: Spring容器只存在一个共享的bean实例,默认的配置。 <bean id="example" class="com.tarena.bean.Example"/> Prototype: 每次对bean的请求都会创建一个新的bean实例。 <bean id="example" class="com.tarena.bean.Example" singleton=”false”/> 二者选择的原则:有状态的bean都使用Prototype作用域,而对无状态的bean则应该使用singleton作用域。 [br][br][color=#990000][b]-=-=-=-=- 以下内容由 [i]Jordan[/i] 在 [i]2007年09月13日 02:02pm[/i] 时添加 -=-=-=-=-[/b][/color] 在Spring2.0中除了以前的Singleton和Prototype外又加入了三个新的web作用域,分别为request、session和global session。 如果你希望容器里的某个bean拥有其中某种新的web作用域,除了在bean级上配置相应的scope属性,还必须在容器级做一个额外的初始化配置。即在web应用的web.xml中增加这么一个ContextListener: <web-app> <listener> <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class> </listener> </web-app> 以上是针对Servlet 2.4以后的版本。 比如Request作用域: <bean id="loginAction" class="com.tarena.LoginAction" scope="request"/> |