Spring InitializingBean

在总结以前的代码时发现了InitializingBean这个类,能猜到这个类的基本作用,但是还是仔细研究了一下。

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

public interface InitializingBean
{
    public abstract void afterPropertiesSet() throws Exception;
}
在spring 初始化后,执行完所有属性设置方法(即setXxx)后将自动调用 afterPropertiesSet(),在配置文件中无须特别的配置, 但此方式增加了bean对spring 的依赖,应该尽量避免使用。

看如下测试代码:

在xml配置文件中并不需要对bean进行特殊的配置:

    <bean class="com.core.config.TestInitializingBean">
        <property name="configFile">
            <value>material-version-config.xml</value>
        </property>
    </bean>
Bean实现这个接口,在afterPropertiesSet()中编写初始化代码:

package com.core.config;

import org.springframework.beans.factory.InitializingBean;

public class TestInitializingBean implements InitializingBean {

	@Override
	public void afterPropertiesSet() throws Exception {
		System.out.println("do afterPropertiesSet...");
	}
	
	public void setConfigFile(String configFile){
		System.out.println("do setXxx..."+configFile);
	}

}

执行结果:

do setXxx...material-version-config.xml
do afterPropertiesSet...


二、Spring虽然可以通过InitializingBean完成一个bean初始化后对这个bean的回调,但是这种方式要求bean实现 InitializingBean接口。一但bean实现了InitializingBean接口,那么这个bean的代码就和Spring耦合到一起了。通常情况下不鼓励bean直接实现InitializingBean,可以使用Spring提供的init-method的功能来执行一个bean 自定义的初始化方法。
看如下测试代码:

在xml配置文件中需要对bean进行特殊的配置:

    <bean class="com.core.config.TestInit" init-method="init">
        <property name="configFile">
            <value>material-version-config.xml</value>
        </property>
    </bean>
写一个java class,这个类不实现任何Spring的接口。定义一个没有参数的方法init():

package com.core.config;

public class TestInit {

	public void setConfigFile(String configFile){
		System.out.println("do setXxx..."+configFile);
	}

	public void init(){
		System.out.println("do init");
	}
	
}
执行结果:

do setXxx...material-version-config.xml
do 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上。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值