spring InitializingBean接口
TransactionTemplate的源碼如下:
public class TransactionTemplate extends DefaultTransactionDefinition
??? ??? implements TransactionOperations, InitializingBean{
??? ??? .
??? ??? .
??? ??? .
??? ??? }
TransactionTemplate繼承了DefaultTransactionDefinition,實現了TransactionOperations,InitializingBean接口。先研究InitializingBean接口
InitializingBean接口為bean提供了初始化方法的方式,它只包括afterPropertiesSet方法,凡是繼承該接口的類,在初始化bean的時候會執行該方法。
測試程序如下:
import org.springframework.beans.factory.InitializingBean;
public class TestInitializingBean implements InitializingBean{
??? @Override
??? public void afterPropertiesSet() throws Exception {
??? ??? System.out.println("ceshi InitializingBean");??? ????
??? }
??? public void testInit(){
??? ??? System.out.println("ceshi 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" ></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程序,打印如下結果:
ceshi InitializingBean
ceshi init-method
由 結果可看出,在spring初始化bean的時候,如果該bean是實現了InitializingBean接口,并且同時在配置文件中指定了init- method,系統則是先調用afterPropertiesSet方法,然后在調用init-method中指定的方法。
這方式在spring中是怎么實現的?
通過查看spring的加載bean的源碼類(AbstractAutowireCapableBeanFactory)可看出其中奧妙
AbstractAutowireCapableBeanFactory類中的invokeInitMethods講解的非常清楚,源碼如下:
protected void invokeInitMethods(String beanName, final Object bean, RootBeanDefinition mbd)
??? ??? ??? throws Throwable {
//判斷該bean是否實現了實現了InitializingBean接口,如果實現了InitializingBean接口,則只掉調用bean的afterPropertiesSet方法
??? ??? boolean isInitializingBean = (bean instanceof InitializingBean);
??? ??? if (isInitializingBean && (mbd == null || !mbd.isExternallyManagedInitMethod("afterPropertiesSet"))) {
??? ??? ??? if (logger.isDebugEnabled()) {
??? ??? ??? ??? logger.debug("Invoking afterPropertiesSet() on bean with name '" + beanName + "'");
??? ??? ??? }
??? ??? ????
??? ??? ??? if (System.getSecurityManager() != null) {
??? ??? ??? ??? try {
??? ??? ??? ??? ??? AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {
??? ??? ??? ??? ??? ??? public Object run() throws Exception {
??? ??? ??? ??? ??? ??? ??? //直接調用afterPropertiesSet
??? ??? ??? ??? ??? ??? ??? ((InitializingBean) bean).afterPropertiesSet();
??? ??? ??? ??? ??? ??? ??? return null;
??? ??? ??? ??? ??? ??? }
??? ??? ??? ??? ??? },getAccessControlContext());
??? ??? ??? ??? } catch (PrivilegedActionException pae) {
??? ??? ??? ??? ??? throw pae.getException();
??? ??? ??? ??? }
??? ??? ??? }??? ??? ??? ????
??? ??? ??? else {
??? ??? ??? ????//直接調用afterPropertiesSet
??? ??? ??? ??? ((InitializingBean) bean).afterPropertiesSet();
??? ??? ??? }
??? ??? }
??? ??? if (mbd != null) {
??? ??? ??? String initMethodName = mbd.getInitMethodName();
??? ??? ????//判斷是否指定了init-method方法,如果指定了init-method方法,則再調用制定的init-method
??? ??? ??? if (initMethodName != null && !(isInitializingBean && "afterPropertiesSet".equals(initMethodName)) &&
??? ??? ??? ??? ??? !mbd.isExternallyManagedInitMethod(initMethodName)) {
??? ??? ??? ??? ????//進一步查看該方法的源碼,可以發現init-method方法中指定的方法是通過反射實現
??? ??? ??? ??? invokeCustomInitMethod(beanName, bean, mbd);
??? ??? ??? }
??? ??? }
??? }
總結:
1:spring為bean提供了兩種初始化bean的方式,實現InitializingBean接口,實現afterPropertiesSet方法,或者在配置文件中同過init-method指定,兩種方式可以同時使用
2:實現InitializingBean接口是直接調用afterPropertiesSet方法,比通過反射調用init-method指定的方法效率相對來說要高點。但是init-method方式消除了對spring的依賴
3:如果調用afterPropertiesSet方法時出錯,則不調用init-method指定的方法。
4:TransactionTemplate實現InitializingBean接口,主要是判斷transactionManager是否已經初始化,如果沒有則拋出異常。源碼如下:
???? public void afterPropertiesSet() {
??? ??? if (this.transactionManager == null) {
??? ??? ??? throw new IllegalArgumentException("Property 'transactionManager' is required");
??? ??? }
??? }
來自:http://blog.csdn.net/mqboss/article/details/7452331
總結
以上是生活随笔為你收集整理的spring InitializingBean接口的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: js 校验验证码 知识点
- 下一篇: Magento利用input type=