bean的作用域有5種:我們常用的就兩種,分別爲singleton,prototype,下面我介紹一下五種作用域的適用環境:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
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.xsd">
<!-- bean 作用域
* singleton 單例,只創建一個實例。默認值,在Spring IoC容器中僅存在一個Bean實例,Bean以單例方式存在,整合servlet時用
* prototype 多例,每次從容器中調用Bean時,都返回一個新的實例,即每次調用getBean()時 ,相當於執行new XxxBean(),整合struts時用
* request,request範圍,request.setAttribute,每次HTTP請求都會創建一個新的Bean,該作用域僅適用於WebApplicationContext環境
* session,session範圍,session.setAttribute,同一個HTTP Session 共享一個Bean,不同Session使用不同Bean,僅適用於WebApplicationContext 環境
* globalSession , prolet 分佈式門戶,sso(單點登錄)將不同的應用的數據保存到globalSession中,達到數據共享,該作用域僅適用於WebApplicationContext 環境
-->
<bean id="person" class="cn.adwo.Person" scope="prototype"></bean>
</beans>