- Spring核心基礎
- Spring - 首頁
- Spring - 概覽
- Spring - 架構
- Spring - 環境設定
- Spring - Hello World示例
- Spring - IoC容器
- Spring - Bean定義
- Spring - Bean作用域
- Spring - Bean生命週期
- Spring - Bean後置處理器
- Spring - Bean定義繼承
- Spring - 依賴注入
- Spring - 注入內部Bean
- Spring - 注入集合
- Spring - Bean自動裝配
- 基於註解的配置
- Spring - 基於Java的配置
- Spring - Spring中的事件處理
- Spring - Spring中的自定義事件
- Spring - 使用Spring框架進行AOP
- Spring - JDBC框架
- Spring - 事務管理
- Spring - Web MVC框架
- Spring - 使用Log4J進行日誌記錄
- Spring問答
- Spring - 問答
- Spring有用資源
- Spring - 快速指南
- Spring - 有用資源
- Spring - 討論
Spring - Bean生命週期
Spring Bean的生命週期很容易理解。當例項化一個Bean時,可能需要執行一些初始化操作以使其處於可用狀態。類似地,當不再需要Bean並將其從容器中移除時,可能需要進行一些清理操作。
雖然,在Bean例項化和銷燬之間發生的幕後活動有一系列列表,但本章將只討論兩個重要的Bean生命週期回撥方法,這些方法在Bean初始化和銷燬時是必需的。
要為Bean定義設定和拆卸,我們只需使用init-method和/或destroy-method引數宣告<bean>。init-method屬性指定一個方法,該方法將在Bean例項化後立即被呼叫。類似地,destroy-method指定一個方法,該方法在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註冊一個關閉鉤子。這樣做可以確保優雅地關閉並呼叫單例Bean的相關destroy方法,以便釋放所有資源。
建議您不要使用InitializingBean或DisposableBean回撥,因為XML配置在命名方法方面提供了更大的靈活性。
示例
讓我們準備好一個可用的Eclipse IDE,並按照以下步驟建立一個Spring應用程式:
| 步驟 | 描述 |
|---|---|
| 1 | 建立一個名為SpringExample的專案,並在建立的專案中的src資料夾下建立一個名為com.tutorialspoint的包。 |
| 2 | 使用新增外部JAR選項新增所需的Spring庫,如Spring Hello World示例章節中所述。 |
| 3 | 在com.tutorialspoint包下建立Java類HelloWorld和MainApp。 |
| 4 | 在src資料夾下建立Beans配置檔案Beans.xml。 |
| 5 | 最後一步是建立所有Java檔案和Bean配置檔案的內容,並按如下所述執行應用程式。 |
以下是HelloWorld.java檔案的內容:
package com.tutorialspoint;
public class HelloWorld {
private String message;
public void setMessage(String message){
this.message = message;
}
public void getMessage(){
System.out.println("Your Message : " + message);
}
public void init(){
System.out.println("Bean is going through init.");
}
public void destroy() {
System.out.println("Bean will destroy now.");
}
}
以下是MainApp.java檔案的內容。在這裡,您需要註冊一個關閉鉤子registerShutdownHook()方法,該方法在AbstractApplicationContext類上宣告。這將確保優雅地關閉並呼叫相關的destroy方法。
package com.tutorialspoint;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {
public static void main(String[] args) {
AbstractApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
obj.getMessage();
context.registerShutdownHook();
}
}
以下是用於init和destroy方法的配置檔案Beans.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">
<bean id = "helloWorld" class = "com.tutorialspoint.HelloWorld" init-method = "init"
destroy-method = "destroy">
<property name = "message" value = "Hello World!"/>
</bean>
</beans>
建立原始檔和Bean配置檔案後,讓我們執行應用程式。如果您的應用程式一切正常,它將列印以下訊息:
Bean is going through init. Your Message : Hello World! Bean will destroy now.
預設初始化和銷燬方法
如果您有很多具有相同名稱的初始化和/或銷燬方法的Bean,則無需在每個Bean上都宣告init-method和destroy-method。相反,框架提供了使用<beans>元素上的default-init-method和default-destroy-method屬性配置這種情況的靈活性,如下所示:
<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"
default-init-method = "init"
default-destroy-method = "destroy">
<bean id = "..." class = "...">
<!-- collaborators and configuration for this bean go here -->
</bean>
</beans>