亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

Spring之-管理bean的聲明周期一(InitializingB

系統 1904 0

InitializingBean

??? Spirng的InitializingBean為bean提供了定義初始化方法的方式。InitializingBean是一個接口,它僅僅包含一個方法:afterPropertiesSet()。
    package org.springframework.beans.factory;

/**
 * Interface to be implemented by beans that need to react once all their
 * properties have been set by a BeanFactory: for example, to perform custom
 * initialization, or merely to check that all mandatory properties have been set.
 * 
 * <p>An alternative to implementing InitializingBean is specifying a custom
 * init-method, for example in an XML bean definition.
 * For a list of all bean lifecycle methods, see the BeanFactory javadocs.
 *
 * @author Rod Johnson
 * @see BeanNameAware
 * @see BeanFactoryAware
 * @see BeanFactory
 * @see org.springframework.beans.factory.support.RootBeanDefinition#getInitMethodName
 * @see org.springframework.context.ApplicationContextAware
 */
public interface InitializingBean {
	
	/**
	 * Invoked by a BeanFactory after it has set all bean properties supplied
	 * (and satisfied BeanFactoryAware and ApplicationContextAware).
	 * <p>This method allows the bean instance to perform initialization only
	 * possible when all bean properties have been set and to throw an
	 * exception in the event of misconfiguration.
	 * @throws Exception in the event of misconfiguration (such
	 * as failure to set an essential property) or if initialization fails.
	 */
	void afterPropertiesSet() throws Exception;

}
  

?

?

?Bean實現這個接口,在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配置文件中并不需要對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">
</bean>
</beans>
    
?
編寫測試程序進行測試:
      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");
}
}
    
? 運行上面的程序我們會看到:“LifeCycleBean initializing...”,這說明bean的afterPropertiesSet已經被Spring調用了。
?
??? Spring在設置完一個bean所有的合作者后,會檢查bean是否實現了InitializingBean接口,如果實現就調用bean的afterPropertiesSet方法。

Spring之-管理bean的聲明周期一(InitializingBean和init-method)

?

init-method

??? Spring雖然可以通過InitializingBean完成一個bean初始化后對這個bean的回調,但是這種方式要求bean實現 InitializingBean接口。一但bean實現了InitializingBean接口,那么這個bean的代碼就和Spring耦合到一起了。通常情況下我不鼓勵bean直接實現InitializingBean,可以使用Spring提供的init-method的功能來執行一個bean 子定義的初始化方法。
寫一個java class,這個類不實現任何Spring的接口。定義一個沒有參數的方法init()。

?

      package research.spring.beanfactory.ch4;
public class LifeCycleBean{
public void init(){
System.out.println("LifeCycleBean.init...");
}
}
    

?

在Spring中配置這個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>
    

?

當Spring實例化lifeBean時,你會在控制臺上看到” LifeCycleBean.init...”。
?
?
Spring要求init-method是一個無參數的方法,如果init-method指定的方法中有參數,那么Spring將會拋出 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將中止這個Bean的后續處理,并且拋出一個 org.springframework.beans.factory.BeanCreationException 異常。
?
InitializingBean和init-method可以一起使用,Spring會先處理InitializingBean再處理init-method。
org.springframework.beans.factory.support. AbstractAutowireCapableBeanFactory完成一個Bean初始化方法的調用工作。 AbstractAutowireCapableBeanFactory是XmlBeanFactory的超類,再 AbstractAutowireCapableBeanFactory的invokeInitMethods方法中實現調用一個Bean初始化方法:
org.springframework.beans.factory.support. AbstractAutowireCapableBeanFactory.java:
        //……
//在一個bean的合作者設備完成后,執行一個bean的初始化方法。

protected void invokeInitMethods(String beanName, Object bean, RootBeanDefinition mergedBeanDefinition) throws Throwable {
//判斷bean是否實現了InitializingBean接口

if (bean instanceof InitializingBean) {
if (logger.isDebugEnabled()) {
logger.debug("Invoking afterPropertiesSet() on bean with name '" + beanName + "'");
}
//調用afterPropertiesSet方法

((InitializingBean) bean).afterPropertiesSet();
}
//判斷bean是否定義了init-method

if(mergedBeanDefinition!=null&&mergedBeanDefinition.getInitMethodName() != null) {
//調用invokeCustomInitMethod方法來執行init-method定義的方法

invokeCustomInitMethod(beanName, bean, mergedBeanDefinition.getInitMethodName());
}
}
//執行一個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對象

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 + "'");
}
//判斷方法是否是public

if (!Modifier.isPublic(initMethod.getModifiers())) {
//設置accessible為true,可以訪問private方法。
                     initMethod.setAccessible(true);
}
try {
//反射執行這個方法

initMethod.invoke(bean, (Object[]) null);
}
catch (InvocationTargetException ex) {
throw ex.getTargetException();
}
}
//………..
      
?
通過分析上面的源代碼我們可以看到,init-method是通過反射執行的,而afterPropertiesSet是直接執行的。所以 afterPropertiesSet的執行效率比init-method要高,不過init-method消除了bean對Spring依賴。在實際使用時我推薦使用init-method。
??? 需要注意的是Spring總是先處理bean定義的InitializingBean,然后才處理init-method。如果在Spirng處理InitializingBean時出錯,那么Spring將直接拋出異常,不會再繼續處理init-method。
??? 如果一個bean被定義為非單例的,那么afterPropertiesSet和init-method在bean的每一個實例被創建時都會執行。單例 bean的afterPropertiesSet和init-method只在bean第一次被實例時調用一次。大多數情況下 afterPropertiesSet和init-method都應用在單例的bean上。

裝配 bean 的合作者

查看 bean 是否實現 InitializingBean 接口

調用 afterPropertiesSet 方法

Spring之-管理bean的聲明周期一(InitializingBean和init-method)


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 特级一级全黄毛片免费 | 伊人久久综合视频 | 天天舔天天射 | 美女牲交视频一级毛片 | 色老头老太做爰视频在线观看 | 国产精品久久久久久久成人午夜 | 国产亚洲欧美日韩综合综合二区 | 色噜噜国产精品视频一区二区 | 成人网在线看 | 国产在线视频www色 国产在线视频凹凸分类 | 久久精品免费看 | 91麻豆精品 | 欧美高清理论片在线观看 | 成人免费小视频 | 久久综合精品国产一区二区三区 | 欧美xxx网站| 亚洲欧美一级久久精品 | 日本一级特级毛片视频 | 成人短视频在线 | 99亚洲乱人伦精品 | 欧美伊人久久久久久久久影院 | 亚洲精品色综合久久久 | 波多野结衣3女同在线观看 波多野结衣av1区2区3区 | 国产v片成人影院在线观看 国产v视频 | 成年男女免费视频网站 | 国产伦理精品 | 久久精品一区二区三区不卡 | 福利免费观看 | 伊人精品影院一本到欧美 | 一区二区三区免费视频观看 | 久久精品国产亚洲精品 | 国产成人一区二区三区在线播放 | 欧美性精品hd在线观看 | 欧美日韩大片 | 免费a视频在线观看 | 天天干天天干 | 精品成人免费播放国产片 | 国产精品免费观在线 | 99热这里只有精品6免费 | 久草久草久草 | 欧美一级视频免费看 |