關(guān)于在spring 容器初始化 bean 和銷毀前所做的操作定義方式有三種:

成都創(chuàng)新互聯(lián)公司堅持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:做網(wǎng)站、網(wǎng)站建設(shè)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時代的丘北網(wǎng)站設(shè)計、移動媒體設(shè)計的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!
第一種:通過注解@PostConstruct 和 @PreDestroy 方法 實現(xiàn)初始化和銷毀bean之前進行的操作
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
public class DataInitializer{
@PostConstruct
public void initMethod() throws Exception {
System.out.println("initMethod 被執(zhí)行");
}
@PreDestroy
public void destroyMethod() throws Exception {
System.out.println("destroyMethod 被執(zhí)行");
}
} 第二種是:通過 在xml中定義init-method 和 destory-method方法
public class DataInitializer{
public void initMethod() throws Exception {
System.out.println("initMethod 被執(zhí)行");
}
public void destroyMethod() throws Exception {
System.out.println("destroyMethod 被執(zhí)行");
}
} 第三種是: 通過bean實現(xiàn)InitializingBean和 DisposableBean接口
import org.springframework.beans.factory.DisposableBean;
public class DataInitializer implements InitializingBean,DisposableBean{
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("afterPropertiesSet 被執(zhí)行");
}
@Override
public void destroy() throws Exception {
System.out.println("destroy 被執(zhí)行");
}
} 其中第一種和第二種是同一種形式,只不過一種xml配置,另外一種采用注解形式罷了,有很大區(qū)別的是第三種,如果同一個bean同時采用兩種方式初始化的時候執(zhí)行某個方法,首先在執(zhí)行順序上就會體現(xiàn)出來。
先執(zhí)行afterPropertiesSet(),后執(zhí)行initMethod()
這里我們看下源碼
這方式在spring中是怎么實現(xiàn)的?
通過查看spring的加載bean的源碼類(AbstractAutowireCapableBeanFactory)可看出其中奧妙
AbstractAutowireCapableBeanFactory類中的invokeInitMethods講解的非常清楚,源碼如下:
protected void invokeInitMethods(String beanName, final Object bean, RootBeanDefinition mbd)
throws Throwable {
//判斷該bean是否實現(xiàn)了實現(xiàn)了InitializingBean接口,如果實現(xiàn)了InitializingBean接口,則只掉調(diào)用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 {
//直接調(diào)用afterPropertiesSet
((InitializingBean) bean).afterPropertiesSet();
return null;
}
},getAccessControlContext());
} catch (PrivilegedActionException pae) {
throw pae.getException();
}
}
else {
//直接調(diào)用afterPropertiesSet
((InitializingBean) bean).afterPropertiesSet();
}
}
if (mbd != null) {
String initMethodName = mbd.getInitMethodName();
//判斷是否指定了init-method方法,如果指定了init-method方法,則再調(diào)用制定的init-method
if (initMethodName != null && !(isInitializingBean && "afterPropertiesSet".equals(initMethodName)) &&
!mbd.isExternallyManagedInitMethod(initMethodName)) {
//進一步查看該方法的源碼,可以發(fā)現(xiàn)init-method方法中指定的方法是通過反射實現(xiàn)
invokeCustomInitMethod(beanName, bean, mbd);
}
}
總結(jié):
1:spring為bean提供了兩種初始化bean的方式,實現(xiàn)InitializingBean接口,實現(xiàn)afterPropertiesSet方法,或者在配置文件中同過init-method指定,兩種方式可以同時使用
2:實現(xiàn)InitializingBean接口是直接調(diào)用afterPropertiesSet方法,比通過反射調(diào)用init-method指定的方法效率相對來說要高點。但是init-method方式消除了對spring的依賴
3:如果調(diào)用afterPropertiesSet方法時出錯,則不調(diào)用init-method指定的方法。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。
網(wǎng)站名稱:spring啟動加載程序的幾種方法介紹
標題URL:http://chinadenli.net/article0/jggjoo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供用戶體驗、移動網(wǎng)站建設(shè)、定制開發(fā)、定制網(wǎng)站、做網(wǎng)站、Google
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)