免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
Spring內(nèi)核研究-管理bean的聲明周期一(InitializingBean和init-method)
Spring內(nèi)核研究-管理bean的聲明周期一(InitializingBean和init-method)

InitializingBean

    Spirng的InitializingBean為bean提供了定義初始化方法的方式。InitializingBean是一個(gè)接口,它僅僅包含一個(gè)方法:afterPropertiesSet()。


Bean實(shí)現(xiàn)這個(gè)接口,在afterPropertiesSet()中編寫初始化代碼:
package research.spring.beanfactory.ch4;import org.springframework.beans.factory.InitializingBean;public class LifeCycleBean implements InitializingBean{public void afterPropertiesSet() throws Exception {System.out.println("LifeCycleBean initializing...");}}
 
在xml配置文件中并不需要對(duì)bean進(jìn)行特殊的配置:
xml version="1.0" encoding="UTF-8"?>DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN""http://www.springframework.org/dtd/spring-beans.dtd"><beans><bean name="lifeBean" class="research.spring.beanfactory.ch4.LifeCycleBean">bean>beans>

 
編寫測(cè)試程序進(jìn)行測(cè)試:
package research.spring.beanfactory.ch4;import org.springframework.beans.factory.xml.XmlBeanFactory;import org.springframework.core.io.ClassPathResource;public class LifeCycleTest {public static void main(String[] args) {XmlBeanFactory factory=new XmlBeanFactory(new ClassPathResource(
"research/spring/beanfactory/ch4/context.xml"));factory.getBean("lifeBean");}}

    運(yùn)行上面的程序我們會(huì)看到:“LifeCycleBean initializing...”,這說(shuō)明bean的afterPropertiesSet已經(jīng)被Spring調(diào)用了。
 
    Spring在設(shè)置完一個(gè)bean所有的合作者后,會(huì)檢查bean是否實(shí)現(xiàn)了InitializingBean接口,如果實(shí)現(xiàn)就調(diào)用bean的afterPropertiesSet方法。

 SHAPE  \* MERGEFORMAT

裝配bean的合作者

查看bean是否實(shí)現(xiàn)InitializingBean接口

調(diào)用afterPropertiesSet方法


init-method

    Spring雖然可以通過(guò)InitializingBean完成一個(gè)bean初始化后對(duì)這個(gè)bean的回調(diào),但是這種方式要求bean實(shí)現(xiàn) InitializingBean接口。一但bean實(shí)現(xiàn)了InitializingBean接口,那么這個(gè)bean的代碼就和Spring耦合到一起了。通常情況下我不鼓勵(lì)bean直接實(shí)現(xiàn)InitializingBean,可以使用Spring提供的init-method的功能來(lái)執(zhí)行一個(gè)bean 子定義的初始化方法。
寫一個(gè)java class,這個(gè)類不實(shí)現(xiàn)任何Spring的接口。定義一個(gè)沒(méi)有參數(shù)的方法init()。
package research.spring.beanfactory.ch4;public class LifeCycleBean{public void init(){System.out.println("LifeCycleBean.init...");}}
 
在Spring中配置這個(gè)bean:
xml version="1.0" encoding="UTF-8"?>DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN""http://www.springframework.org/dtd/spring-beans.dtd"><beans><bean name="lifeBean" class="research.spring.beanfactory.ch4.LifeCycleBean"
 init-method="init">bean>beans>

當(dāng)Spring實(shí)例化lifeBean時(shí),你會(huì)在控制臺(tái)上看到” LifeCycleBean.init...”。
 
 
Spring要求init-method是一個(gè)無(wú)參數(shù)的方法,如果init-method指定的方法中有參數(shù),那么Spring將會(huì)拋出java.lang.NoSuchMethodException
 
init-method指定的方法可以是public、protected以及private的,并且方法也可以是final的。
 
init-method指定的方法可以是聲明為拋出異常的,就像這樣:

       final protected void init() throws Exception{

           System.out.println("init method...");

           if(true) throw new Exception("init exception");

    }
如果在init-method方法中拋出了異常,那么Spring將中止這個(gè)Bean的后續(xù)處理,并且拋出一個(gè)org.springframework.beans.factory.BeanCreationException異常。
 
InitializingBean和init-method可以一起使用,Spring會(huì)先處理InitializingBean再處理init-method。
org.springframework.beans.factory.support. AbstractAutowireCapableBeanFactory完成一個(gè)Bean初始化方法的調(diào)用工作。 AbstractAutowireCapableBeanFactory是XmlBeanFactory的超類,再 AbstractAutowireCapableBeanFactory的invokeInitMethods方法中實(shí)現(xiàn)調(diào)用一個(gè)Bean初始化方法:
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.java:
//……//在一個(gè)bean的合作者設(shè)備完成后,執(zhí)行一個(gè)bean的初始化方法。protected void invokeInitMethods(String beanName, Object bean, RootBeanDefinition mergedBeanDefinition)
 throws Throwable {
//判斷bean是否實(shí)現(xiàn)了InitializingBean接口if (bean instanceof InitializingBean) {if (logger.isDebugEnabled()) {logger.debug("Invoking afterPropertiesSet() on bean with name ‘" + beanName + "");}//調(diào)用afterPropertiesSet方法((InitializingBean) bean).afterPropertiesSet();}//判斷bean是否定義了init-methodif(mergedBeanDefinition!=null&&mergedBeanDefinition.getInitMethodName() != null) {//調(diào)用invokeCustomInitMethod方法來(lái)執(zhí)行init-method定義的方法invokeCustomInitMethod(beanName, bean, mergedBeanDefinition.getInitMethodName());}}//執(zhí)行一個(gè)bean定義的init-method方法protected void invokeCustomInitMethod(String beanName, Object bean, String initMethodName)throws Throwable {if (logger.isDebugEnabled()) {logger.debug("Invoking custom init method ‘" + initMethodName +"‘ on bean with name ‘" + beanName + "");}//使用方法名,反射Method對(duì)象Method initMethod = BeanUtils.findMethod(bean.getClass(), initMethodName, null);if (initMethod == null) {throw new NoSuchMethodException(
"
Couldn‘t find an init method named ‘" + initMethodName + "‘ on bean with name ‘" + beanName + "");}//判斷方法是否是publicif (!Modifier.isPublic(initMethod.getModifiers())) {//設(shè)置accessible為true,可以訪問(wèn)private方法。 initMethod.setAccessible(true);}try {//反射執(zhí)行這個(gè)方法initMethod.invoke(bean, (Object[]) null);}catch (InvocationTargetException ex) {throw ex.getTargetException();}}//………..

    通過(guò)分析上面的源代碼我們可以看到,init-method是通過(guò)反射執(zhí)行的,而afterPropertiesSet是直接執(zhí)行的。所以 afterPropertiesSet的執(zhí)行效率比init-method要高,不過(guò)init-method消除了bean對(duì)Spring依賴。在實(shí)際使用時(shí)我推薦使用init-method。
    需要注意的是Spring總是先處理bean定義的InitializingBean,然后才處理init-method。如果在Spirng處理InitializingBean時(shí)出錯(cuò),那么Spring將直接拋出異常,不會(huì)再繼續(xù)處理init-method。
    如果一個(gè)bean被定義為非單例的,那么afterPropertiesSet和init-method在bean的每一個(gè)實(shí)例被創(chuàng)建時(shí)都會(huì)執(zhí)行。單例 bean的afterPropertiesSet和init-method只在bean第一次被實(shí)例時(shí)調(diào)用一次。大多數(shù)情況下 afterPropertiesSet和init-method都應(yīng)用在單例的bean上。
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
spring的InitializingBean的afterPropertiesSet方法和init-method配置的區(qū)別聯(lián)系
SpringBean初始化過(guò)程
Spring中Bean的生命周期(轉(zhuǎn))
spring InitializingBean接口
Spring8:一些常用的Spring Bean擴(kuò)展接口
Spring AbstractRoutingDataSource afterPropertiesSet方法
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服