InitializingBean
spring的InitializingBean为bean提供了定义初始化方法的方式。InitializingBean是一个接口,只包含一个方法:afterPropertiesSet():
public interface InitializingBean
{
public abstract void afterPropertiesSet()
throws Exception;
}
用法示例:
Bean实现:
import org.springframework.beans.factory.InitializingBean;
publicclass LifeCycleBean implements InitializingBean{
void afterPropertiesSet() throws Exception {
System.out.println("LifeCycleBean initializing...");
}
}
在xml配置文件中并不需要对bean进行特殊的配置:
<beans>
<bean name ="lifeBean" class ="com.spring.LifeCycleBean"></beans >
</beans>
编写测试程序进行测试:
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("com/spring/applicationcontext.xml"));
factory.getBean("lifeBean");
}
}
运行之后,我们可以看到下面的结果:LifeCycleBean initializing...。说明bean的afterPropertiesSet方法被spring调用了。
spring在装配完一个bean的所有合作者之后,会检查这个bean是否实现了InitializingBean接口,如果实现就调用该bean的afterPropertiesSet方法。
init-method
Spring虽然可以通过InitializingBean完成bean初始化后对这个bean的回调,但是这种方式要求bean实现InitializingBean接口。一但bean实现了InitializingBean接口,那么这个bean的代码就和Spring耦合到一起了。通常情况下我不鼓励bean直接实现InitializingBean,可以使用Spring提供的init-method的功能来执行一个bean子定义的初始化方法。
bean实现:
package com.spring;
public class LifeCycleBean{
publicvoid init(){
System. out .println("LifeCycleBean.init...");
}
}
在Spring配置文件中配置这个bean:
<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的父类DefaultListableBeanFactory的父类,也就是XmlBeanFactory的超类,在AbstractAutowireCapableBeanFactory的invokeInitMethods方法中实现了一个调用Bean初始化的方法:
源代码如下:
//bean装配完成之后,执行bean初始化方法
protected void invokeInitMethods(String beanName, final Object bean, RootBeanDefinition mbd)
throws Throwable
{
//判断bean是否实现了InitializingBean接口
boolean isInitializingBean = bean instanceof InitializingBean;
if(isInitializingBean && (mbd == null || !mbd.isExternallyManagedInitMethod("afterPropertiesSet")))
{
if(logger.isDebugEnabled())
logger.debug((new StringBuilder()).append("Invoking afterPropertiesSet() on bean with name '").append(beanName).append("'").toString());
if(System.getSecurityManager() != null)
try
{
AccessController.doPrivileged(new PrivilegedExceptionAction() {
public Object run()
throws Exception
{
//调用afterPropertiesSet方法
((InitializingBean)bean).afterPropertiesSet();
return null;
}
final Object val$bean;
final AbstractAutowireCapableBeanFactory this$0;
{
this.this$0 = AbstractAutowireCapableBeanFactory.this;
bean = obj;
super();
}
}
, getAccessControlContext());
}
catch(PrivilegedActionException pae)
{
throw pae.getException();
}
else
((InitializingBean)bean).afterPropertiesSet();调用afterPropertiesSet方法
}
if(mbd != null)
{
//判断bean是否定义了init-method方法
String initMethodName = mbd.getInitMethodName();
if(initMethodName != null && (!isInitializingBean || !"afterPropertiesSet".equals(initMethodName)) && !mbd.isExternallyManagedInitMethod(initMethodName))
//调用invokeCustomInitMethod方法来执行init-method定义的方法
invokeCustomInitMethod(beanName, bean, mbd);
}
}
//执行bean定义的init-method方法
protected void invokeCustomInitMethod(String beanName, final Object bean, RootBeanDefinition mbd)
throws Throwable
{
String initMethodName = mbd.getInitMethodName();
//使用方法名,反射Method对象
final Method initMethod = mbd.isNonPublicAccessAllowed() ? BeanUtils.findMethod(bean.getClass(), initMethodName, new Class[0]) : ClassUtils.getMethodIfAvailable(bean.getClass(), initMethodName, new Class[0]);
if(initMethod == null)
{
if(mbd.isEnforceInitMethod())
throw new BeanDefinitionValidationException((new StringBuilder()).append("Couldn't find an init method named '").append(initMethodName).append("' on bean with name '").append(beanName).append("'").toString());
if(logger.isDebugEnabled())
logger.debug((new StringBuilder()).append("No default init method named '").append(initMethodName).append("' found on bean with name '").append(beanName).append("'").toString());
return;
}
if(logger.isDebugEnabled())
logger.debug((new StringBuilder()).append("Invoking init method '").append(initMethodName).append("' on bean with name '").append(beanName).append("'").toString());
if(System.getSecurityManager() != null)
{
AccessController.doPrivileged(new PrivilegedExceptionAction() {
public Object run()
throws Exception
{
ReflectionUtils.makeAccessible(initMethod);
return null;
}
final Method val$initMethod;
final AbstractAutowireCapableBeanFactory this$0;
{
this.this$0 = AbstractAutowireCapableBeanFactory.this;
initMethod = method;
super();
}
}
);
try
{
AccessController.doPrivileged(new PrivilegedExceptionAction() {
public Object run()
throws Exception
{
initMethod.invoke(bean, new Object[0]);//反射执行init-method方法
return null;
}
final Method val$initMethod;
final Object val$bean;
final AbstractAutowireCapableBeanFactory this$0;
{
this.this$0 = AbstractAutowireCapableBeanFactory.this;
initMethod = method;
bean = obj;
super();
}
}
, getAccessControlContext());
}
catch(PrivilegedActionException pae)
{
InvocationTargetException ex = (InvocationTargetException)pae.getException();
throw ex.getTargetException();
}
} else
{
try
{
ReflectionUtils.makeAccessible(initMethod);
initMethod.invoke(bean, new Object[0]);//反射执行init-method方法
}
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方法来完成一些需要在bean初始化时完成的工作。
示例场景:
一个bean在初始化时需要读取项目目录中某个文件夹下的配置文件。
bean配置:
<bean id="testServiceConfig" class="com.lmb.client.TestlServiceConfigImpl">
<property name="folderName" value="test_config" />
</bean>
调用代码:
//bean初始化之后调用afterPropertiesSet方法根据配置目录,读取相应目录下的配置文件
public class TestServiceConfigImpl implements InitializingBean {
private String folderName;
@Override
public void afterPropertiesSet()throws Exception{
folderName = folderName != null ? folderName : "test_config";
reload(folderName);
}
//根据文件目录加载配置文件
public void reload(String fileName) throws Exception {
fileName = fileName.startsWith("/") ? fileName.substring(1) : fileName;
//根据文件名称获取相应的文件
File file = ResourceUtils.getFile("classpath:" + fileName);
if (file.isDirectory()) {
File[] files = file.listFiles();
for (File configFile : files) {
//加载文件……
}
}
}
}