Spring系列:第二篇-Spring容器三種實例化bean的方式以及Bean的作用域與生命週期

作者:Damniel

郵箱:[email protected]

微博:

博客:https://blog.csdn.net/bulletoo_(轉載請說明出處)

------------------------------------華麗的分割線------------------------------------------------------

一、Spring容器三種實例化bean的方式 

1.使用類構造器實例化
<bean id="personService" class="cn.lius.service.impl.PersonServiceBean"></bean>
2.使用靜態工廠方法實例化
<bean id="personService2" class="cn.lius.service.impl.PersonServiceBeanFactory" factory-method="createPersonServiceBean"/>
public class PersonServiceBeanFactory {
	public static PersonServiceBean createPersonServiceBean(){
		return new PersonServiceBean();
	}
	public PersonServiceBean createPersonServiceBean2(){
		return new PersonServiceBean();
	}
}


3.使用實例工廠方法實例化

<bean id="personServiceFactory" class="cn.lius.service.impl.PersonServiceBeanFactory"/>
<bean id="personService3" factory-bean="personServiceFactory" factory-method="createPersonServiceBean2"/>

public class PersonServiceBeanFactory {
	public static PersonServiceBean createPersonServiceBean(){
		return new PersonServiceBean();
	}
	
	public PersonServiceBean createPersonServiceBean2(){
		return new PersonServiceBean();
	}
}

二、Bean的作用域

1.singleton

       bean的作用域默認爲單例,作用域爲單例時,Spring IOC容器中只會存在一個共享的bean實例,並且所有對bean的請求,只要id與該bean定義相匹配,則只會返回bean的同一實例。這個單一實例會被存儲到單例緩存(singleton cache)中,並且所有針對該bean的後續請求和引用都 將返回被緩存的對象實例,這裏注意的是singleton作用域和GOF設計模式中的單例是完全不同的,單例設計模式表示一個ClassLoader中 只有一個class存在,而這裏的singleton則表示一個容器對應一個bean,也就是說當一個bean被標識爲singleton時 候,spring的IOC容器中只會存在一個該bean。

    配置實例:

<bean id="personService" class="cn.lius.service.impl.PersonServiceBean"/> 
<bean id="personService" class="cn.lius.service.impl.PersonServiceBean" scope="singleton"/> 
<bean id="personService" class="cn.lius.service.impl.PersonServiceBean" singleton="true"/> 

2.prototype

        prototype作用域部署的bean,每一次請求(將其注入到另一個bean中,或者以程序的方式調用容器的 getBean()方法)都會產生一個新的bean實例,相當與一個new的操作,對於prototype作用域的bean,有一點非常重要,那就是Spring不能對一個prototype bean的整個生命週期負責,容器在初始化、配置、裝飾或者是裝配完一個prototype實例後,將它交給客戶端,隨後就對該prototype實例不聞不問了。不管何種作用域,容器都會調用所有對象的初始化生命週期回調方法,而對prototype而言,任何配置好的析構生命週期回調方法都將不會被調用。 清除prototype作用域的對象並釋放任何prototype bean所持有的昂貴資源,都是客戶端代碼的職責。(讓Spring容器釋放被singleton作用域bean佔用資源的一種可行方式是,通過使用 bean的後置處理器,該處理器持有要被清除的bean的引用。)

        配置實例:

  <bean id="personService" class="cn.lius.service.impl.PersonServiceBean" scope="prototype"/>

3.request    

        request表示該針對每一次HTTP請求都會產生一個新的bean,同時該bean僅在當前HTTP request內有效,配置實例:
request、session、global session使用的時候首先要在初始化web的web.xml中做如下配置:
<web-app>
   ...
  <listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
  </listener>
   ...
</web-app>
如果是Servlet2.4以前的web容器,那麼你要使用一個javax.servlet.Filter的實現: 
<web-app>
 ..
 <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>
   ...
</web-app>

最後配置bean的作用域:

<bean id="personService" class="cn.lius.service.impl.PersonServiceBean" scope="request"/>

4.session

    session作用域表示該針對每一次HTTP請求都會產生一個新的bean,同時該bean僅在當前HTTP session內有效

    配置實例:

    和request配置實例的前提一樣,配置好web啓動文件就可以如下配置: 

<bean id="personService" class="cn.lius.service.impl.PersonServiceBean" scope="session"/>

5.global session

    global session作用域類似於標準的HTTP Session作用域,不過它僅僅在portlet的web應用中才有意義。Portlet規範定義了全局Session的概念,它被所有構成某個 portlet web應用的各種不同的portlet所共享。在global session作用域中定義的bean被限定於全局portlet Session的生命週期範圍內。如果你在web中使用global session作用域來標識bean,那麼web會自動當成session類型來使用。

    配置實例:
    和request配置實例的前提一樣,配置好web啓動文件就可以如下配置: 
<bean id="personService" class="cn.lius.service.impl.PersonServiceBean" scope="global session"/>

三、Bean的生命週期

(1).單例情況(scope="singleton"):

       1.由spring容器創建對象 ,bean在容器創建的同時進行實例化(可以通過懶加載延遲)(用構造函數驗證)
       2.spring容器調用初始化方法
(init-method
(bean在例化的時同時調用)
       3.客戶端調用該對象的其他某些方法 (自定義的方法)
       4.關閉spring容器的時候,執行摧毀方法(destroy-method)

(2).多例情況(scope="prototype"):

        1.由spring容器創建對象, bean在容器創建後調用獲取bean的getBean方法時候才實例化
       2.spring容器調用初始化方法(init-method(bean在例化的時同時調用)

       3.客戶端調用該對象的其他某些方法 (自定義的方法)
       4.關閉spring容器的時候不會 執行摧毀方法destroy-method

    配置實例:
    
 <bean id="personService" class="cn.lius.service.impl.PersonServiceBean"  init-method="init" destroy-method="destory"/>
 <bean id="personService2" class="cn.lius.service.impl.PersonServiceBean"  init-method="init" destroy-method="destory" scope="prototype"/>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章