Spring中bean的作用範圍
阿新 • • 發佈:2017-08-30
pre 方法 context () 釋放資源 應用 生命 產生 ron
singleton作用域:
Spring的scope的默認值是singleton
Spring 只會為每一個bean創建一個實例,並保持bean的引用.
<bean id="bean的id" class="bean的包名.類名" scope="singleton"/> 或 <bean id="bean的id" class="bean的包名.類名" singleton="true"/>
Spring1以上提供
prototype作用域:
每一次請求(將其註入到另一個bean中,或執行getBean()方法)都會產生一個新的bean實例,相當於new操作.
Spring一旦將Bean實例交給(註入)調用者,就不再持有這個bean的引用。就無法再執行bean定義的destroy-method.
清除prototype作用域的bean對象並釋放資源,是調用者的職責。
<bean id="bean的id" class="bean的包名.類名" scope="prototype"/> 或 <beanid="bean的id" class="bean的包名.類名" singleton="false"/>
Spring1以上提供
request作用域:
表示該針對每一次HTTP請求都會產生一個新的bean,僅適用於WebApplicationContext環境。
<bean id="bean的id" class="bean的包名.類名" scope="request"/>
Spring1以上提供
session作用域:
表示該針對每一次HTTP請求都會產生一個新的bean,僅適用於WebApplicationContext環境。
<bean id="bean的id" class="bean的包名.類名" scope="session"/>
Spring1以上提供
globalSession作用域:
它僅僅在基於portlet的web應用中才有意義。Portlet規範定義了全局Session的概念,它被所有構成某個portlet web應用的各種不同的portlet所共享。在global session作用域中定義的bean被限定於全局portlet Session的生命周期範圍內。如果你在web中使用global session作用域來標識bean,那麽,web會自動當成session類型來使用。
<bean id="bean的id" class="bean的包名.類名" scope="globalSession"/>
Spring1以上提供
Spring中bean的作用範圍