詳解Java的Spring框架中bean的定義以及生命周期
bean的定義
形成應(yīng)用程序的骨干是由Spring IoC容器所管理的對象稱為bean。bean被實例化,組裝,并通過Spring IoC容器所管理的對象。這些bean由容器提供,例如,在XML的<bean/>定義,已經(jīng)看到了前幾章的形式配置元數(shù)據(jù)創(chuàng)建。
bean定義包含所需要的容器要知道以下稱為配置元數(shù)據(jù)的信息:
- 如何創(chuàng)建一個bean
- Bean 生命周期的詳細(xì)信息
- Bean 依賴關(guān)系
上述所有配置元數(shù)據(jù)轉(zhuǎn)換成一組的下列屬性構(gòu)成每個bean的定義。

Spring配置元數(shù)據(jù)
Spring IoC容器完全由在此配置元數(shù)據(jù)實際寫入的格式解耦。有下列提供的配置元數(shù)據(jù)的Spring容器三個重要的方法:
- 基于XML的配置文件
- 基于注解的配置
- 基于Java的配置
我們已經(jīng)看到了基于XML的配置元數(shù)據(jù)如何提供給容器,但讓我們看到了不同的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的生命周期是很容易理解。當(dāng)一個bean實例化時,它可能需要執(zhí)行一些初始化把它轉(zhuǎn)換成可用狀態(tài)。類似地,當(dāng)bean不再需要,并且從容器中取出,一些清理的工作可能也需要做。
不過,還有把bean背后的實例化和銷毀時間之間的場景發(fā)生的活動,但是本章將只討論其中兩個是需要在bean的初始化和銷毀的時候,重要bean的生命周期回調(diào)方法。
要定義安裝和拆卸一個bean,我們只是聲明了初始化方法和/或銷毀,方法的參數(shù)<bean>。在init-method屬性指定一個方法,是被調(diào)用bean后立即實例化。同樣,銷毀方法規(guī)定了被調(diào)用當(dāng)bean被從容器中取出之前的方法。
初始化回調(diào):
org.springframework.beans.factory.InitializingBean 接口指定一個單一的方法:
void afterPropertiesSet() throws Exception;
因此,可以簡單地實現(xiàn)上述接口和初始化工作可以在里面afterPropertiesSet() 方法,如下所示:
public class ExampleBean implements InitializingBean {
public void afterPropertiesSet() {
// do some initialization work
}
}
在基于XML的配置元數(shù)據(jù)的情況下,可以使用init-method 屬性來指定具有void無參數(shù)簽名的方法的名稱。例如:
<bean id="exampleBean"
class="examples.ExampleBean" init-method="init"/>
下面是類的定義:
public class ExampleBean {
public void init() {
// do some initialization work
}
}
銷毀回調(diào)
org.springframework.beans.factory.DisposableBean接口指定一個單一的方法:
void destroy() throws Exception;
因此,你可以簡單地實現(xiàn)上述接口和定稿工作可以做里面的destroy() 方法,如下所示:
public class ExampleBean implements DisposableBean {
public void destroy() {
// do some destruction work
}
}
在基于XML的配置元數(shù)據(jù)的情況下,您可以使用destroy-method屬性來指定具有void無參數(shù)簽名的方法的名稱。例如:
<bean id="exampleBean"
class="examples.ExampleBean" destroy-method="destroy"/>
下面是類的定義:
public class ExampleBean {
public void destroy() {
// do some destruction work
}
}
如果您在非web應(yīng)用環(huán)境中使用Spring的IoC容器,例如在桌面富客戶端環(huán)境; 注冊關(guān)閉鉤子在JVM中。這樣做可以確保正常關(guān)機,并讓所有的資源都被釋放調(diào)用singleton bean上的相關(guān)destroy方法。
建議不要使用的InitializingBean或者DisposableBean的回調(diào),因為XML配置提供極大的靈活性在命名你的方法方面。
例如:
使用Eclipse IDE,然后按照下面的步驟來創(chuàng)建一個Spring應(yīng)用程序:

相關(guān)文章
將List集合中的map對象轉(zhuǎn)為List<對象>形式實例代碼
這篇文章主要介紹了將List集合中的map對象轉(zhuǎn)為List<對象>形式實例代碼,具有一定借鑒價值,需要的朋友可以參考下2018-01-01
Java獲取http和https協(xié)議返回的json數(shù)據(jù)
本篇文章主要介紹了Java獲取http和https協(xié)議返回的json數(shù)據(jù) ,本篇文章提供兩個方法,幫助各位如何獲取http和https返回的數(shù)據(jù)。有興趣的可以了解一下。2017-01-01

