解決方案一:通過XML配置文件實現:(標簽bean的屬性init-method和destroy-method)
beans.xml:
<beans> <bean id="bean" class="org.spring.tutorial.SimpleBean" init-method="init" destroy-method="destroy" /> </beans>
SimpleBean.java:
package org.spring.tutorial;
public class SimpleBean {
public SimpleBean() {
System.out.println("SimpleBean called");
}
public void init() {
System.out.println("init called");
}
public void destroy() {
System.out.println("destroy called");
}
}
MainApp.java:
package org.spring.tutorial;
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"); // 加載指定XML文件的定義
context.registerShutdownHook(); // 注冊關閉鈎子
}
}
運行之:
SimpleBean called init called 五月 19, 2013 5:59:18 下午 org.springframework.context.support.AbstractApplicationContext doClose INFO: Closing org.springframework.context.support.ClassPathXmlApplicationContext@1286c71: startup date [Sun May 19 17:59:18 CST 2013]; root of context hierarchy destroy called
解決方案二:實現Spring的InitializingBean和DisposableBean。
SimpleBean.java:
package org.spring.tutorial;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
public class SimpleBean implements InitializingBean,DisposableBean {
public SimpleBean() {
System.out.println("SimpleBean called");
}
@Override
public void destroy() throws Exception {
System.out.println("destroy called");
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("afterPropertiesSet called");
}
}
beans.xml:
<beans> <bean id="bean" class="org.spring.tutorial.SimpleBean" /> </beans>
運行結果:
SimpleBean called afterPropertiesSet called 五月 19, 2013 6:03:44 下午 org.springframework.context.support.AbstractApplicationContext doClose INFO: Closing org.springframework.context.support.ClassPathXmlApplicationContext@e77ca4: startup date [Sun May 19 18:03:44 CST 2013]; root of context hierarchy destroy called
解決方案三:利用javax.annotation包的注解PostConstruct和PreDestroy。
SimpleBean.java:
package org.spring.tutorial;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
public class SimpleBean {
public SimpleBean() {
System.out.println("SimpleBean called");
}
@PreDestroy
public void destroy() throws Exception {
System.out.println("destroy called");
}
@PostConstruct
public void afterPropertiesSet() throws Exception {
System.out.println("afterPropertiesSet called");
}
}
beans.xml:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config />
<bean id="bean" class="org.spring.tutorial.SimpleBean" />
</beans>
運行結果:
SimpleBean called 五月 19, 2013 6:06:39 下午 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@431425: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,bean,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; root of factory hierarchy afterPropertiesSet called destroy called
總結:可以利用接口(InitializingBean,DisposableBean),注解(@PreDestroy,@PostConstruct)和XML配置文件(bean的屬性init-method,destroy-method)來實現bean的生命周期。可以根據個人的喜好選擇不同的方案。
