参考文章:
bean实例化过程中在激活Aware,调用后置处理之后最后一步是InitializingBean(初始化bean) 和 init-method(调用bean的初始化方法)
InitializingBean
bean 提供了定义初始化方法的方式,它仅包含了一个方法:#afterPropertiesSet()
方法在
AbstractAutowireCapableBeanFactory.initializeBean里面
try {
// 激活初始化方法
invokeInitMethods(beanName, wrappedBean, mbd);
}
catch (Throwable ex) {
throw new BeanCreationException(
(mbd != null ? mbd.getResourceDescription() : null),
beanName, "Invocation of init method failed", ex);
}
protected void invokeInitMethods(String beanName, final Object bean, @Nullable RootBeanDefinition mbd)
throws Throwable {
// 首先会检查是否是 InitializingBean ,如果是的话需要调用 afterPropertiesSet()
boolean isInitializingBean = (bean instanceof InitializingBean);
if (isInitializingBean && (mbd == null || !mbd.isExternallyManagedInitMethod("afterPropertiesSet"))) {
if (logger.isTraceEnabled()) {
logger.trace("Invoking afterPropertiesSet() on bean with name '" + beanName + "'");
}
// 安全模式
if (System.getSecurityManager() != null) {
......
}
else {
// 属性初始化
((InitializingBean) bean).afterPropertiesSet();
}
}
......
}
代码中可以看 到,当bean实现了InitializingBean后,系统会调用其afterPropertiesSet方法
所以我们可以写个demo:
实现了InitializingBean的类
public class InitializingBeanTest implements InitializingBean {
private String name;
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("InitializingBeanTest initializing...");
this.name = "类中创建的bean";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
xml配置
<bean id="initializingBeanTest" class="com.learn.initbean.Initializingbean.InitializingBeanTest">
<property name="name" value="xml名字"/>
</bean>
</beans>
测试方法
public static void main(String[] args) {
ClassPathResource resource = new ClassPathResource("spring3.xml");
DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(factory);
reader.loadBeanDefinitions(resource);
InitializingBeanTest test = (InitializingBeanTest) factory.getBean("initializingBeanTest");
System.out.println("name :" + test.getName());
}
打印结果
InitializingBeanTest initializing...
14:42:10.643 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Finished creating instance of bean 'initializingBeanTest'
name :类中创建的bean
可以看到类在初始化的时候使用的类名是class中定义的而不是xml中定义了。
init-method
在((InitializingBean) bean).afterPropertiesSet();代码之后我们看到spring还进行了一个方法调用
if (mbd != null && bean.getClass() != NullBean.class) {
String initMethodName = mbd.getInitMethodName();
if (StringUtils.hasLength(initMethodName) &&
!(isInitializingBean && "afterPropertiesSet".equals(initMethodName)) &&
!mbd.isExternallyManagedInitMethod(initMethodName)) {
// 激活用户自定义的初始化方法
invokeCustomInitMethod(beanName, bean, mbd);
}
}
invokeCustomInitMethod(beanName, bean, mbd);执行了用户配置的自定义方法
现在在类上添加一个方法
public void testInit() {
this.name = "testInit中创建的bean";
}
xml添加一个init方法的配置
<bean id="initializingBeanTest" class="com.learn.initbean.Initializingbean.InitializingBeanTest"
init-method="testInit">
<property name="name" value="xml名字"/>
</bean>
</beans>
此时执行结果为
15:13:38.108 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Finished creating instance of bean 'initializingBeanTest'
name :testInit中创建的bean
小结
init-method
指定的方法会在 #afterPropertiesSet()
方法之后执行,如果 #afterPropertiesSet()
方法的执行的过程中出现了异常,则 init-method
是不会执行的,而且由于 init-method
采用的是反射执行的方式,所以 #afterPropertiesSet()
方法的执行效率一般会高些,但是并不能排除我们要优先使用 init-method
,主要是因为它消除了 bean 对 Spring 的依赖,Spring 没有侵入到我们业务代码,这样会更加符合 Spring 的理念。
demo项目地址
https://gitee.com/daifylearn/springLearn