詳解Java的Spring架構中bean的定義以及生命週期_java

來源:互聯網
上載者:User

bean的定義
形成應用程式的骨幹是由Spring IoC容器所管理的對象稱為bean。bean被執行個體化,組裝,並通過Spring IoC容器所管理的對象。這些bean由容器提供,例如,在XML的<bean/>定義,已經看到了前幾章的形式配置中繼資料建立。

bean定義包含所需要的容器要知道以下稱為配置中繼資料的資訊:

  1. 如何建立一個bean
  2. Bean 生命週期的詳細資料
  3. Bean 依賴關係

上述所有配置中繼資料轉換成一組的下列屬性構成每個bean的定義。

Spring配置中繼資料
Spring IoC容器完全由在此配置中繼資料實際寫入的格式解耦。有下列提供的配置中繼資料的Spring容器三個重要的方法:

  1. 基於XML的設定檔
  2. 基於註解的配置
  3. 基於Java的配置

我們已經看到了基於XML的配置中繼資料如何提供給容器,但讓我們看到了不同的bean定義,包括延遲初始化,初始化方法和銷毀方法基於XML設定檔的另一個樣本:

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"  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-3.0.xsd">  <!-- A simple bean definition -->  <bean id="..." class="...">    <!-- collaborators and configuration for this bean go here -->  </bean>  <!-- A bean definition with lazy init set on -->  <bean id="..." class="..." lazy-init="true">    <!-- collaborators and configuration for this bean go here -->  </bean>  <!-- A bean definition with initialization method -->  <bean id="..." class="..." init-method="...">    <!-- collaborators and configuration for this bean go here -->  </bean>  <!-- A bean definition with destruction method -->  <bean id="..." class="..." destroy-method="...">    <!-- collaborators and configuration for this bean go here -->  </bean>  <!-- more bean definitions go here --></beans>

bean的生命週期
Spring bean的生命週期是很容易理解。當一個bean執行個體化時,它可能需要執行一些初始化把它轉換成可用狀態。類似地,當bean不再需要,並且從容器中取出,一些清理的工作可能也需要做。

不過,還有把bean背後的執行個體化和銷毀時間之間的情境發生的活動,但是本章將只討論其中兩個是需要在bean的初始化和銷毀的時候,重要bean的生命週期回調方法。

要定義安裝和拆卸一個bean,我們只是聲明了初始化方法和/或銷毀,方法的參數<bean>。在init-method屬性指定一個方法,是被調用bean後立即執行個體化。同樣,銷毀方法規定了被調用當bean被從容器中取出之前的方法。

初始化回調:
org.springframework.beans.factory.InitializingBean 介面指定一個單一的方法:

void afterPropertiesSet() throws Exception;
因此,可以簡單地實現上述介面和初始化工作可以在裡面afterPropertiesSet() 方法,如下所示:

public class ExampleBean implements InitializingBean {  public void afterPropertiesSet() {   // do some initialization work  }}

在基於XML的配置中繼資料的情況下,可以使用init-method 屬性來指定具有void無參數簽名的方法的名稱。例如:

<bean id="exampleBean"      class="examples.ExampleBean" init-method="init"/>

下面是類的定義:

public class ExampleBean {  public void init() {   // do some initialization work  }}

銷毀回調
org.springframework.beans.factory.DisposableBean介面指定一個單一的方法:

void destroy() throws Exception;

因此,你可以簡單地實現上述介面和定稿工作可以做裡面的destroy() 方法,如下所示:

public class ExampleBean implements DisposableBean {  public void destroy() {   // do some destruction work  }}

在基於XML的配置中繼資料的情況下,您可以使用destroy-method屬性來指定具有void無參數簽名的方法的名稱。例如:

<bean id="exampleBean"      class="examples.ExampleBean" destroy-method="destroy"/>

下面是類的定義:

public class ExampleBean {  public void destroy() {   // do some destruction work  }}

如果您在非web應用環境中使用Spring的IoC容器,例如在案頭富用戶端環境; 註冊關閉鉤子在JVM中。這樣做可以確保順利關機,並讓所有的資源都被釋放調用singleton bean上的相關destroy方法。

建議不要使用的InitializingBean或者DisposableBean的回調,因為XML配置提供極大的靈活性在命名你的方法方面。

例如:
使用Eclipse IDE,然後按照下面的步驟來建立一個Spring應用程式:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.