原文转自:http://blog.csdn.net/shaozheng1006/article/details/6916940

InitializingBean

    Spirng的InitializingBean为bean提供了定义初始化方法的方式。InitializingBean是一个接口,它仅仅包含一个方法:afterPropertiesSet()。
   在spring 初始化后,执行完所有属性设置方法(即setXxx)将自动调用 afterPropertiesSet(), 在配置文件中无须特别的配置, 但此方式增加了bean对spring 的依赖,应该尽量避免使用
 
Spirng的InitializingBean为bean提供了定义初始化方法的方式。InitializingBean是一个接口,它仅仅包含一个方法:afterPropertiesSet()。

public interface InitializingBean
{

public abstract 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方法。

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;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方法p
rotected 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上。

<二>
init-method 
    在bean中写一个无参无返回值的public 方法 实现bean的初始化。例如
   public void init(){
       // …… 初始化代码
    }
在spring 初始化后,执行完所有属性设置方法(即setXxx)将自动调用 配置文件init-method指定的方法,配置文件如下所示

    <bean name="beanName" class="package.bean"
init-method="init">
</bean>

<三>

initializingBean接口是spring提供的,子类要实现afterPropertiesSet()方法,init-method是用户可以通过属性文件配置的。从这里可以看出实现initializingBean接口是要和spring耦合的,而init-method则不会。但afterPropertiesSet()方法spring可以直接调用执行,init-method指定的方法却需要通过反射来执行,从效率来上讲没有afgerPropertiesSet()快。InitializingBean和init-method可以一起使用,Spring会先处理InitializingBean再处理init-method。
下面是一个小例子,initializingBean接口和init-method都实现了,可以自己随便改。
试验用的bean:
package researchspring.beanfactory;

import org.springframework.beans.factory.InitializingBean;

public class LifeCycleBean implements InitializingBean{
    
    private String name;

public String getName() {
        return name;
    }

public void setName(String name) {
        this.name = name;
    }

public void afterPropertiesSet() throws Exception {
        System.out.println("Implements initializing start.....");
        if(name == null)
            System.out.println("configuration failed!");
        System.out.println("Implements initializing end!");
    }
    
    public void init() {
        System.out.println("init() initializing start.....");
        if(name == null)
            System.out.println("configuration failed!");
        System.out.println("init() initializing end!");
    }
}
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
    
    <bean id="lifeBean" class="researchspring.beanfactory.LifeCycleBean"
        init-method="init" >
    <property name="name" value="apple"></property>
    </bean>
</beans>
测试类:
public class Test {
    public static void main(String[] args) {
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        LifeCycleBean lifeBean = (LifeCycleBean)ac.getBean("lifeBean");
    }

}

×××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××
另外,从网上找了这么一段

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();
}
}
//………..

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

  1. spring init method destroy method

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

  2. Spring报错: org.springframework.beans.factory.support.BeanDefinitionValidationException: Couldn't find an init method named 'init' on bean with name 'car'(待解答)

    在Spring工程里,有一个Car类的bean,Main.java主程序,MyBeanPostProcessor.java是Bean后置处理器. 文件目录结构如下: Car.java package ...

  3. MyBatis与Spring MVC结合时,使用DAO注入出现:Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required

    错误源自使用了这个例子:http://www.yihaomen.com/article/java/336.htm,如果运行时会出现如下错误: Invocation of init method fai ...

  4. Error creating bean with name 'sqlSessionFactory' defined in class path resource [config/spring/applicationContext.xml]: Invocation of init method failed;

    我报的错: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSes ...

  5. Spring InitializingBean and DisposableBean example

    In Spring, InitializingBean and DisposableBean are two marker interfaces, a useful way for Spring to ...

  6. Spring InitializingBean init-method @PostConstruct 执行顺序

    Spring 容器中的 Bean 是有生命周期的,Spring 允许在 Bean 在初始化完成后以及 Bean 销毁前执行特定的操作,常用的设定方式有以下三种:   通过实现 Initializing ...

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

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

  8. 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 ...

  9. Error creating bean with name 'sessionFactory' defined in class path resource [applicationContext.xml]: Invocation of init method failed; nested exception is org.hibernate.HibernateException: Unable t

    spring与hibernate整合然后出现如下错误: org.springframework.beans.factory.BeanCreationException: Error creating ...

随机推荐

  1. Nao 类人机器人 相关资料

    Nao 类人机器人 相关资料: 1.兄妹 PEPPER :在山东烟台生产,http://www.robot-china.com/news/201510/30/26564.html 2.国内机器人领先公 ...

  2. Subgradient Algorithm

    Subgradient是一种可以优化不可微的凸函数的方法. 首先回顾凸函数的定义: $f(y) \geq f(x) + \nabla f(x)^T(y-x), all \hspace{2 pt} x, ...

  3. HDU 3999 The order of a Tree

    The order of a Tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  4. IoC控制反转与DI依赖注入

    IoC控制反转与DI依赖注入 IoC: Inversion of Control IoC是一种模式.目的是达到程序的复用.下面的两篇论文是对IoC的权威解释: InversionOfControl h ...

  5. HTML5[7]: 实现网页版的加载更多

    理所当然,jQuery出场: $(function() { $(window).scroll(function() { if($(this).scrollTop() + $(this).innerHe ...

  6. C#通过ODBC查询HANA数据库数据

    创建HANA的ODBC数据库连接. 默认在控制面板——>管理工具——>数据源(ODBC) 提示:如果系统是64位的,要运行 C:\Windows\SysWOW64\odbcad32.exe ...

  7. Delphi 10.1 Berlin Starter Edition

    Delphi 10.1 Berlin Starter Edition Embarcadero® Delphi 10.1 Berlin Starter is a great way to get sta ...

  8. NPM使用详解(上)

    1.NPM是什么? NPM是JavaScript的包管理工具,在安装NodeJS(什么?你不知道node?来,我们合计合计:https://nodejs.org/)的时候,会自动安装上npm. 要查看 ...

  9. Watch out for these 10 common pitfalls of experienced Java developers & architects--转

    原文地址:http://zeroturnaround.com/rebellabs/watch-out-for-these-10-common-pitfalls-of-experienced-java- ...

  10. Yii中 RBAC(基于角色的访问控制权限)表结构原理分析

    这里有几个概念很重要,我简单用大白话说一下; 权限:就是指用户是否可以执行哪些操作. 如:小张可以发帖.回帖.浏览,小红只能回帖.浏览 角色:就是上面说的一组操作的集合. 如:高级会员有发帖.回帖.删 ...