【spring的InitializingBean的 afterPropertiesSet 方法 和 init-method配置的 区别联系】

InitializingBean

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

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

package research.spring.beanfactory.ch4; import org.springframework.beans.factory.InitializingBean; class LifeCycleBean implements InitializingBean{

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">

>

< /beans >

编写测试程序进行测试:

package research.spring.beanfactory.ch4; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.core.io.ClassPathResource; public class LifeCycleTest { static void main(String[] args) { XmlBeanFactory factory= newXmlBeanFactory( 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 方法

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的初始化方法。 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) {

thrownew 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上。

InitializingBean和init-method的更多相关文章

  1. SSH项目练习的时候报错:[applicationContext.xml]: Invocation of init method failed;

    这里是控制台的报错信息:org.springframework.beans.factory.BeanCreationException: Error creating bean with name ' ...

  2. The init method

    The init method is a special method that gets invoked when an object is instantiated. Its full name ...

  3. Error creating bean with name 'sessionFactory' defined in class path resource [applicationContext.xml]: Invocation of init method failed; neste

    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFacto ...

  4. Invocation of init method failed; nested exception is org.hibernate.HibernateException: could not instantiate RegionFactory [org.hibernate.cache.impl

    严重: Exception sending context initialized event to listener instance of class org.springframework.we ...

  5. java代码中init method和destroy method的三种使用方式

    在java的实际开发过程中,我们可能常常需要使用到init method和destroy method,比如初始化一个对象(bean)后立即初始化(加载)一些数据,在销毁一个对象之前进行垃圾回收等等. ...

  6. MyBatis笔记----报错:Error creating bean with name 'sqlSessionFactory' defined in class path resource [com/ij34/mybatis/applicationContext.xml]: Invocation of init method failed; nested exception is org.sp

    四月 05, 2017 4:51:02 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRef ...

  7. org.springframework.beans.factory.BeanCreationException,Invocation of init method failed,Context initialization failed

    G:\javaanzhuang\apache-tomcat-\bin\catalina.bat run [-- ::,] Artifact ssm_qingmu02_web:war exploded: ...

  8. Invocation of init method failed; nested exception is java.text.ParseException: '?' can only be specfied for Day-of-Month or Day-of-Week.

    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cronTrigger' ...

  9. spring init method destroy method

    在java的实际开发过程中,我们可能常常需要使用到init method和destroy method,比如初始化一个对象(bean)后立即初始化(加载)一些数据,在销毁一个对象之前进行垃圾回收等等. ...

  10. aused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [applicationContext.xml]: Invocation of init method fai

    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'roleDaoImpl' ...

随机推荐

  1. 【解题报告】[动态规划] RQNOJ PID106 / 最大加权矩形

    原题地址:http://www.rqnoj.cn/problem/106 解题思路: 一维的情况下求最大字串和的状态转移方程是:s[i]=max{s[i-1]+a[i],a[i]} 二维的情况下,只要 ...

  2. Edit Control的Enter响应函数

    Edit Control的Enter响应函数   在dialog中添加edit control ,选择“Multi_Line mode”   MFC Class Wizard中添加Virtual Fu ...

  3. NPOI 2.0导出word(docx格式)

    大名鼎鼎的NPOI用来导出EXCEL的文章园子里面有很多,可是用来导出WORD文档的文章大都含糊不清,最近刚好完成一个导出WORD文档的需求,在此分享下. NPOI里面认为word文档的最基本的结构是 ...

  4. 关于“怎么比较两个SQL查询语句产生的结果集是否相同”搜集到的一些资料

    一. 如果你这两个结果集都没有close,可以这样: rs.last(); rs2.last(); if (rs.getRow() != rs2.getRow()) { //记录数不等,即结果不等 } ...

  5. 4. 2D绘制与控件绘制

    绘制基本图形和文本 绘制图形和文本的基本方法 drawPoint(绘制点).drawLine(绘制直线).drawCircle(绘制圆) drawArc(绘制弧).drawText(绘制文本) pac ...

  6. Free Candies

    题意: 有4堆东西,每堆有n个每个有一个颜色,现在有一个篮子最多能装5个不同的颜色的东西,每次都从堆顶拿,当篮子出现两个相同颜色,可以获得这两个东西,求获得的最大数量 分析: 因为就4推,可以把各堆的 ...

  7. bzoj1833 digit

    这道题其实挺水,只是写的时候需要想清楚.我的方法是: 1.将[a,b]转化为[0,b+1)-[0,a) 2.预处理出非0的v在区间[0,10^p)出现次数以及0在区间[0,10^p)出现数 3.将一个 ...

  8. Office2013版的破解之路

    追着潮流,我还是更新了我的所有软件,2013版早就下载了,因为一直破解的问题没有装,这次终于找到必成功的办法. 1.准备工作: 下载office2013的官方版即可,官方版里不包含project和vi ...

  9. [LeetCode] Missing Number (A New Questions Added Today)

    Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...

  10. arm tiny6410双网卡桥接问题

    这几天做实验,想用arm tiny6410板子做个mesh网络节点.该板子本身自带一个lan网卡,我自己配了一个tp-link的usb无线网卡.其中wlan网卡工作在adhoc模式下,作为mesh骨干 ...