InitializingBean接口爲bean提供了初始化方法的方式,它只包括afterPropertiesSet方法,凡是繼承該接口的類,在初始化bean的時候會執行該方法。
測試程序以下:spring
import org.springframework.beans.factory.InitializingBean; import org.springframework.stereotype.Service; public class InitBean implements InitializingBean{ public void afterPropertiesSet() throws Exception { System.out.println("啓動時自動執行 afterPropertiesSet..."); } public void init(){ System.out.println("init method..."); } }
配置文件以下:spring-mvc
<?xml version="1.0" encoding="UTF-8"?> <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" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:mvc="http://www.springframework.org/schema/mvc" 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.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd" default-lazy-init="true"> <bean id="initBean" class="com.netease.nsip.spring.InitBean" init-method="init"> </bean> </beans>
Main主程序以下:mvc
import org.springframework.context.ApplicationContext; import org.springframework.context.support.FileSystemXmlApplicationContext; public class Main { public static void main(String[] args) { ApplicationContext context = new FileSystemXmlApplicationContext("classpath:/applicationContext-core.xml"); context.getBean("initBean"); } }
運行Main程序,打印以下結果:app
啓動時自動執行 afterPropertiesSet... init method...
這說明在spring初始化bean的時候,若是bean實現了InitializingBean接口,會自動調用afterPropertiesSet方法。
問題:實現InitializingBean接口與在配置文件中指定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); } } }
總結:spa