Spring的InitializingBean是我们常用的接口之一,它是在Bean初始化完调用的,利用这一点,我们可以在系统刚启动时做一些事情。
1. 定义一个InitializingBean接口实现类
/**
* 自定义InitializingBean
* 调用: Bean初始化完之后 AbstractAutowireCapableBeanFactory.invokeInitMethods,只会调用一次
* 执行顺序:
* 构造方法 -> @PostConstruct -> afterPropertiesSet -> initMethod -> @PreDestroy -> destroyMethod
*
*/
// @Configuration
public class MyInitializingBean implements InitializingBean, DisposableBean {
public MyInitializingBean() {
System.out.println("--------MyInitializingBean:construct---------");
}
/**
* CommonAnnotationBeanPostProcessor(InitDestroyAnnotationBeanPostProcessor):postProcessBeforeInitialization
*/
@PostConstruct
public void postConstruct() {
System.out.println("--------MyInitializingBean:@PostConstruct---------");
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("--------InitializingBean:afterPropertiesSet---------");
}
public void initMethod() {
System.out.println("--------MyInitializingBean:initMethod---------");
}
/**
* CommonAnnotationBeanPostProcessor(InitDestroyAnnotationBeanPostProcessor):postProcessBeforeDestruction
*/
@PreDestroy
public void preDestory() {
System.out.println("--------MyInitializingBean:@PreDestroy---------");
}
@Override
public void destroy() throws Exception {
System.out.println("--------InitializingBean:DisposableBean---------");
}
public void destroyMethod() {
System.out.println("--------MyInitializingBean:destroyMethod---------");
}
}
2.加载MyInitializingBean,为例顺便测试initMethod和destroyMethod,我们采用@Bean注解方式加载
@Configuration
public class MyAutoConfiguration {
@Bean(initMethod="initMethod", destroyMethod="destroyMethod")
public MyInitializingBean myInitializingBean() {
return new MyInitializingBean();
}
}
3. 测试类
@SpringBootApplication
public class Application {
public static void main(String[] args) {
ConfigurableApplicationContext applicationContext = SpringApplication.run(Application.class, args);
applicationContext.close();
}
}
4.测试结果
--------MyInitializingBean:construct---------
--------MyInitializingBean:@PostConstruct---------
--------InitializingBean:afterPropertiesSet---------
--------MyInitializingBean:initMethod---------
--------MyInitializingBean:@PreDestroy---------
--------InitializingBean:DisposableBean---------
--------MyInitializingBean:destroyMethod---------
5. 源码分析
调用是在spring容器实例化完Bean之后调用的,不清楚的可以参考spring-boot bean初始化源码分析(一)和spring-boot bean初始化源码分析(二)两篇文章,下面再回顾一下:
protected Object doCreateBean(final String beanName, final RootBeanDefinition mbd, final @Nullable Object[] args)
throws BeanCreationException {
// Instantiate the bean.
BeanWrapper instanceWrapper = null;
if (mbd.isSingleton()) {
instanceWrapper = this.factoryBeanInstanceCache.remove(beanName);
}
if (instanceWrapper == null) {
instanceWrapper = createBeanInstance(beanName, mbd, args);
}
// Initialize the bean instance.
Object exposedObject = bean;
try {
// 对bean成员变量进行赋值、 依赖进行注入
populateBean(beanName, mbd, instanceWrapper);
// 调用spring内置的各种模板方法
// 1.BeanNameAware,BeanClassLoaderAware,BeanFactoryAware
// 2.遍历BeanPostProcessor.postProcessBeforeInitialization方法
// 3.调用InitializingBean.afterPropertiesSet方法
// 4.调用init方法
// 5.遍历BeanPostProcessor.postProcessAfterInitialization方法
exposedObject = initializeBean(beanName, exposedObject, mbd);
}
catch (Throwable ex) {
if (ex instanceof BeanCreationException && beanName.equals(((BeanCreationException) ex).getBeanName())) {
throw (BeanCreationException) ex;
}
else {
throw new BeanCreationException(
mbd.getResourceDescription(), beanName, "Initialization of bean failed", ex);
}
}
return exposedObject;
}
protected Object initializeBean(final String beanName, final Object bean, @Nullable RootBeanDefinition mbd) {
// 1. 调用Aware方法
if (System.getSecurityManager() != null) {
AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
invokeAwareMethods(beanName, bean);
return null;
}, getAccessControlContext());
}
else {
invokeAwareMethods(beanName, bean);
}
// 2. 调用BeanPostProcessor接口的postProcessBeforeInitialization方法
Object wrappedBean = bean;
if (mbd == null || !mbd.isSynthetic()) {
wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);
}
try {
// 3. 调用InitializingBean接口的afterPropertiesSet方法,调用@Bean注解定义的initMethod方法
invokeInitMethods(beanName, wrappedBean, mbd);
}
catch (Throwable ex) {
throw new BeanCreationException(
(mbd != null ? mbd.getResourceDescription() : null),
beanName, "Invocation of init method failed", ex);
}
// 4. 调用BeanPostProcessor接口的postProcessAfterInitialization方法
if (mbd == null || !mbd.isSynthetic()) {
wrappedBean = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);
}
return wrappedBean;
}
private void invokeAwareMethods(final String beanName, final Object bean) {
if (bean instanceof Aware) {
// 1. 如果bean实现了BeanNameAware接口,就调用setBeanName方法
if (bean instanceof BeanNameAware) {
((BeanNameAware) bean).setBeanName(beanName);
}
// 2. 如果bean实现了BeanClassLoaderAware接口,就调用setBeanClassLoader方法
if (bean instanceof BeanClassLoaderAware) {
ClassLoader bcl = getBeanClassLoader();
if (bcl != null) {
((BeanClassLoaderAware) bean).setBeanClassLoader(bcl);
}
}
// 3. 如果bean实现了BeanFactoryAware接口,就调用setBeanFactory方法
if (bean instanceof BeanFactoryAware) {
((BeanFactoryAware) bean).setBeanFactory(AbstractAutowireCapableBeanFactory.this);
}
}
}
public Object applyBeanPostProcessorsBeforeInitialization(Object existingBean, String beanName)
throws BeansException {
// 调用BeanPostProcessor接口的postProcessBeforeInitialization方法
Object result = existingBean;
for (BeanPostProcessor beanProcessor : getBeanPostProcessors()) {
Object current = beanProcessor.postProcessBeforeInitialization(result, beanName);
if (current == null) {
return result;
}
result = current;
}
return result;
}
protected void invokeInitMethods(String beanName, final Object bean, @Nullable RootBeanDefinition mbd)
throws Throwable {
// 1. 调用InitializingBean接口的afterPropertiesSet方法
boolean isInitializingBean = (bean instanceof InitializingBean);
if (isInitializingBean && (mbd == null || !mbd.isExternallyManagedInitMethod("afterPropertiesSet"))) {
if (logger.isDebugEnabled()) {
logger.debug("Invoking afterPropertiesSet() on bean with name '" + beanName + "'");
}
if (System.getSecurityManager() != null) {
try {
AccessController.doPrivileged((PrivilegedExceptionAction<Object>) () -> {
((InitializingBean) bean).afterPropertiesSet();
return null;
}, getAccessControlContext());
}
catch (PrivilegedActionException pae) {
throw pae.getException();
}
}
else {
((InitializingBean) bean).afterPropertiesSet();
}
}
// 2.调用@Bean注解定义的initMethod方法
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);
}
}
}
@Override
public Object applyBeanPostProcessorsAfterInitialization(Object existingBean, String beanName)
throws BeansException {
// 调用BeanPostProcessor接口的postProcessAfterInitialization方法
Object result = existingBean;
for (BeanPostProcessor beanProcessor : getBeanPostProcessors()) {
Object current = beanProcessor.postProcessAfterInitialization(result, beanName);
if (current == null) {
return result;
}
result = current;
}
return result;
}
6. 小结
a. BeanNameAware,BeanClassLoaderAware,BeanFactoryAware
b. 遍历BeanPostProcessor.postProcessBeforeInitialization方法
c. 调用InitializingBean.afterPropertiesSet方法
d. 调用init方法
e. 遍历BeanPostProcessor.postProcessAfterInitialization方法