工作中遇到spring接口中的InitializingBean接口。淺淺的解說一下。
--------------------------------------------------------------------------------------
先探討一下spring初始化bean方式:
//這個時候補充一下spring初始化bean的方式
/**
常用的設定方式有以下三種:
通過實現 InitializingBean/DisposableBean 接口來定制初始化之后/銷毀之前的操作方法;
通過 <bean> 元素的 init-method/destroy-method屬性指定初始化之后 /銷毀之前調用的操作方法;
在指定方法上加上@PostConstruct 或@PreDestroy注解來制定該方法是在初始化之后還是銷毀之前調用。
*/
public class InitSequenceBean implements InitializingBean {
public InitSequenceBean() {
System.out.println("InitSequenceBean: constructor");
}
@PostConstruct
public void postConstruct() {
System.out.println("InitSequenceBean: postConstruct");
}
public void initMethod() {
System.out.println("InitSequenceBean: init-method");
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("InitSequenceBean: afterPropertiesSet");
}
}
並且在配置文件中添加如下Bean定義:<bean class="InitSequenceBean" init-method="initMethod"></bean>
輸出結果:
InitSequenceBean: constructor
InitSequenceBean: postConstruct
InitSequenceBean: afterPropertiesSet
InitSequenceBean: init-method
通過上述輸出結果,說明三種初始化的順序是:
Constructor > @PostConstruct > InitializingBean > init-method
--------------------------------------------------------------------------------------
繼續說明InitializingBean接口:
InitializingBean接口為bean提供了初始化方法的方式,它只包括afterPropertiesSet方法,凡是繼承該接口的類,在初始化bean的時候會執行該方法。
測試程序如下:
import org.springframework.beans.factory.InitializingBean;
public class InitializingBeanT implements InitializingBean{
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("InitializingBean");
}
//用於在spring配置文件中(init-method)用來比較實現InitializingBean和在配置文件中init-method那個先執行
public void testInit(){
System.out.println("init-method");
}
}
同時在配置文件中配置:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="testInitializingBean" class="com.InitializingBeanT" ></bean>
</beans>
main主程序如下:
public class Main {
public static void main(String[] args){
ApplicationContext context = new FileSystemXmlApplicationContext("/src/main/java/com/beans.xml");
}
}
運行Main程序,打印如下結果:
ceshi InitializingBean
這說明在spring初始化bean的時候,如果bean實現了InitializingBean接口,會自動調用afterPropertiesSet方法。
問題:實現InitializingBean接口與在配置文件中指定init-method有什么不同?
修改配置文件,加上init-method配置,修改如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="testInitializingBean" class="com.TestInitializingBean" init-method="testInit"></bean>
</beans>
在配置文件中加入init-method="testInit"。
運行Main程序,打印如下結果:
InitializingBean
init-method
由結果可看出,在spring初始化bean的時候,如果該bean是實現了InitializingBean接口,並且同時在配置文件中指定了init-method,系統則是先調用afterPropertiesSet方法,然后在調用init-method中指定的方法。
總結:
1:spring為bean提供了兩種初始化bean的方式,實現InitializingBean接口,實現afterPropertiesSet方法,或者在配置文件中同過init-method指定,兩種方式可以同時使用
2:實現InitializingBean接口是直接調用afterPropertiesSet方法,比通過反射調用init-method指定的方法效率相對來說要高點。但是init-method方式消除了對spring的依賴
3:如果調用afterPropertiesSet方法時出錯,則不調用init-method指定的方法。