[转]spring的InitializingBean介绍

系统 1793 0

spring的InitializingBean介绍(转)   

2008-12-30 11:12:53 |  分类:   Spring | 字号   订阅

 

 

InitializingBean

    Spirng的InitializingBean为bean提供了定义初始化方法的方式。InitializingBean是一个接口,它仅仅包含一个方法:afterPropertiesSet()。

Bean实现这个接口,在afterPropertiesSet()中编写初始化代码:

 

package research.spring.beanfactory.ch4;import org.springframework.beans.factory.InitializingBean;publicclass LifeCycleBean implements InitializingBean{publicvoid 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;publicclass LifeCycleTest {publicstaticvoid 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方法。

 SHAPE  \* MERGEFORMAT

 

装配bean的合作者

 

 

查看bean是否实现InitializingBean接口

 

 

调用afterPropertiesSet方法

 

 

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;publicclass LifeCycleBean{publicvoid 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的初始化方法。protectedvoid 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-methodif(mergedBeanDefinition!=null&&mergedBeanDefinition.getInitMethodName() !=null) {//调用invokeCustomInitMethod方法来执行init-method定义的方法invokeCustomInitMethod(beanName, bean, mergedBeanDefinition.getInitMethodName());}}//执行一个bean定义的init-method方法protectedvoid 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) {thrownew NoSuchMethodException(

"Couldn't find an init method named '"+ initMethodName +"' on bean with name '"+ beanName +"'");}//判断方法是否是publicif (!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是直接执行的。所

[转]spring的InitializingBean介绍


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论