InitializingBean
InitializingBean接口爲Bean提供了初始化方法,它只包含afterPropertiesSet方法。在spring初始化bean的時候,如果該bean是實現了InitailizingBean接口,則系統會調用afterPropertiesSet方法。
package org.springframework.beans.factory;
public interface InitializingBean {
void afterPropertiesSet() throws Exception;
}
我們知道bean在初始化時還涉及到構造方法、bean定義是的init-method方法、以及@postConstruct註解的方法。加上afterPropertiesSet方法,它們執行的順序是怎麼樣的?
寫個Bean驗證下:
InitializingBeanTest.java
import org.springframework.beans.factory.InitializingBean;
import javax.annotation.PostConstruct;
/**
* 繼承InitializingBean接口
*/
public class InitializingBeanTest implements InitializingBean {
public InitializingBeanTest() {
//構造方法
System.out.println("do InitializingBeanTest >>>>>>>>>>>>");
}
@PostConstruct
public void testPostConstruct(){
System.out.println("do PostConstruct >>>>>>>>>>>>");
}
@Override
public void afterPropertiesSet(){
System.out.println("do afterPropertiesSet >>>>>>>>>>>>");
}
public void testInitMethod(){
System.out.println("do init-method >>>>>>>>>>>>");
}
}
xml配置:
<bean id="myInitializingBean" class="com.test.testQuartz.InitializingBeanTest" init-method="testInitMethod"></bean>
主函數:
public class App
{
public static void main( String[] args )
{
Logger.getLogger("org.test.testQuartz").setLevel(Level.ERROR);
ApplicationContext context = new ClassPathXmlApplicationContext(
"applicationContext.xml");
}
}
output:
do InitializingBeanTest >>>>>>>>>>>>
do PostConstruct >>>>>>>>>>>>
do afterPropertiesSet >>>>>>>>>>>>
do init-method >>>>>>>>>>>>
可見bean初始化時各方法執行順序:
構造方法>@postConstruct>afterPropertiesSet>init-method
注:後兩者順序可通過查看Spring的加載bean的源碼AbstractAutowireCapableBeanFactory.invokeInitMethods方法,分析其具體實現。
DisposableBean
DisposableBean是與InitializingBean相對的一個接口,它只有一個destroy()方法。
package org.springframework.beans.factory;
public interface DisposableBean {
void destroy() throws Exception;
}
給上面的InitializingBean
也實現一下DisposableBean
接口,並實現一個destrory()方法,bean配置一個destroy-method
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.DisposableBean;
import javax.annotation.PostConstruct;
/**
* 繼承InitializingBean接口和DisposableBean接口
*/
public class InitializingBeanTest implements InitializingBean, DisposableBean{
public InitializingBeanTest() {
//構造方法
System.out.println("do InitializingBeanTest >>>>>>>>>>>>");
}
@PostConstruct
public void testPostConstruct(){
System.out.println("do PostConstruct >>>>>>>>>>>>");
}
@Override
public void afterPropertiesSet(){
System.out.println("do afterPropertiesSet >>>>>>>>>>>>");
}
public void testInitMethod(){
System.out.println("do init-method >>>>>>>>>>>>");
}
@Override
public void destroy(){
System.out.println("do destroy >>>>>>>>>>>>");
}
public void testDestroyMethod(){
System.out.println("do destroy-method >>>>>>>>>>>>");
}
}
xml配置:
<bean id="myInitializingBean" class="com.test.testQuartz.InitializingBeanTest" init-method="testInitMethod" destroy-method="testDestroyMethod"></bean>
主函數:
public class App
{
public static void main( String[] args )
{
Logger.getLogger("org.test.testQuartz").setLevel(Level.ERROR);
AbstractApplicationContext context = new ClassPathXmlApplicationContext(
"applicationContext.xml");
context.registerShutdownHook();
}
}
output:
do InitializingBeanTest >>>>>>>>>>>>
do PostConstruct >>>>>>>>>>>>
do afterPropertiesSet >>>>>>>>>>>>
do init-method >>>>>>>>>>>>
do destroy >>>>>>>>>>>>
do destroy-method >>>>>>>>>>>>
也就是說對象銷燬時,會調用DisposableBean的destroy方法,如果對象有配置destroy-method,先執行DisposableBean的destroy方法,後執行destroy-method方法。