
Spring bean的生命周期很容易理解。實(shí)例化bean時(shí),可能需要執(zhí)行一些初始化以使其進(jìn)入可用狀態(tài)。同樣,當(dāng)不再需要bean并將其從容器中刪除時(shí),可能需要進(jìn)行一些清理。

這里將僅討論兩個(gè)重要的Bean生命周期回調(diào)方法,這些方法在Bean初始化及其銷毀時(shí)是必需的。
要定義bean的設(shè)置和拆卸,只需使用initmethod和destroy-method參數(shù)聲明<bean> 。
- init-method屬性指定在實(shí)例化后立即在Bean上調(diào)用的方法。
- destroymethod指定一種在將bean從容器中刪除之前被調(diào)用的方法。
初始化回調(diào)
org.springframework.beans.factory.InitializingBean接口指定一個(gè)方法-
void afterPropertiesSet() throws Exception;
可以簡(jiǎn)單地實(shí)現(xiàn)上述接口,并且可以在afterPropertiesSet()方法內(nèi)部完成初始化工作,如下所示:
public class ExampleBean implements InitializingBean {
public void afterPropertiesSet() {
// do some initialization work
}
}對(duì)于基于XML的配置元數(shù)據(jù),可以使用init-method屬性指定具有無(wú)效無(wú)參數(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接口指定一個(gè)單一的方法-
void destroy() throws Exception;
可以簡(jiǎn)單地實(shí)現(xiàn)上述接口,并且可以在destroy()方法內(nèi)完成終結(jié)工作,如下所示:
public class ExampleBean implements DisposableBean {
public void destroy() {
// do some destruction work
}
}對(duì)于基于XML的配置元數(shù)據(jù),可以使用destroy-method屬性指定具有無(wú)效無(wú)參數(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)境中,向JVM注冊(cè)了一個(gè)關(guān)閉掛鉤。這樣做可以確保正常關(guān)機(jī),并在您的Singleton bean上調(diào)用相關(guān)的destroy方法,以便釋放所有資源。
建議不要使用InitializingBean或DisposableBean回調(diào),因?yàn)閄ML配置在命名方法方面具有很大的靈活性。

例子
讓我們擁有一個(gè)可以正常運(yùn)行的Eclipse IDE,并執(zhí)行以下步驟來(lái)創(chuàng)建一個(gè)Spring應(yīng)用程序:
HelloWorld.java文件的內(nèi)容-
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文件的內(nèi)容。在這里,需要注冊(cè)一個(gè)在AbstractApplicationContext類上聲明的關(guān)閉鉤子registerShutdownHook()方法。這將確保正常關(guān)機(jī)并調(diào)用相關(guān)的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();
}
}以下是初始化和銷毀方法所需的配置文件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>完成創(chuàng)建源和Bean配置文件后,運(yùn)行該應(yīng)用程序。它將顯示以下消息-
Bean is going through init.
Your Message : Hello World!
Bean will destroy now.
默認(rèn)的初始化和銷毀方法
如果有太多具有相同名稱的初始化和/或銷毀方法的bean,則無(wú)需在每個(gè)單獨(dú)的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> 本文使用 文章同步助手 同步