Spring Bean的生命周期

Bean 的初始化过程

    一个类同时实现了 BeanNameAware, BeanFactoryAware, ApplicationContextAware, BeanPostProcessor, InitializingBean, DisposableBean接口时,Bean 的初始化过程为:.

  1. 调用 BeanNameAwaresetBeanName 方法
  2. 调用 BeanFactoryAwaresetBeanFactory 方法
  3. 调用 ApplicationContextAwaresetApplicationContext
  4. 调用 InitializingBeanafterPropertiesSet 或者没有实现这个接口,但指定了@Bean(initMethod="不加括号的方法名"),会执行这个方法
  5. 调用 BeanPostProcessorpostProcessBeforeInitialization 方法
  6. 调用 BeanPostProcessorpostProcessAfterInitialization 方法
  7. Bean 初始化完成,可以被使用
  8. 容器关闭前,调用 DisposableBeandestroy 方法

    但是在整个系统中,BeanPostProcessor的实现类只需要有一个即可,Spring 检测到它的存在时,每个 Bean 被初始化时,都会调用它的方法。注意,是所有 Bean 都会调用它的方法。
    如果一个类除BeanPostProcessor外,实现了其他的接口,有另外一个类单独实现了BeanPostProcessor接口,那么上面的初始化过程中,第 4 步和第 5 步调换位置。

Spring源码注释如下:

 * <p>Bean factory implementations should support the standard bean lifecycle interfaces
 * as far as possible. The full set of initialization methods and their standard order is:<br>
 * 1. BeanNameAware's {@code setBeanName}<br>
 * 2. BeanClassLoaderAware's {@code setBeanClassLoader}<br>
 * 3. BeanFactoryAware's {@code setBeanFactory}<br>
 * 4. ResourceLoaderAware's {@code setResourceLoader}
 * (only applicable when running in an application context)<br>
 * 5. ApplicationEventPublisherAware's {@code setApplicationEventPublisher}
 * (only applicable when running in an application context)<br>
 * 6. MessageSourceAware's {@code setMessageSource}
 * (only applicable when running in an application context)<br>
 * 7. ApplicationContextAware's {@code setApplicationContext}
 * (only applicable when running in an application context)<br>
 * 8. ServletContextAware's {@code setServletContext}
 * (only applicable when running in a web application context)<br>
 * 9. {@code postProcessBeforeInitialization} methods of BeanPostProcessors<br>
 * 10. InitializingBean's {@code afterPropertiesSet}<br>
 * 11. a custom init-method definition<br>
 * 12. {@code postProcessAfterInitialization} methods of BeanPostProcessors
 *
 * <p>On shutdown of a bean factory, the following lifecycle methods apply:<br>
 * 1. DisposableBean's {@code destroy}<br>
 * 2. a custom destroy-method definition

自测结果:

# Aware 的子接口的顺序跟下面的一致
BeanNameAware...setBeanName
BeanFactoryAware...setBeanFactory
ApplicationContextAware...setApplicationContext
InitializingBean...afterPropertiesSet
BeanPostProcessor...postProcessBeforeInitialization
BeanPostProcessor...postProcessAfterInitialization
com.p7.boot.test.Clazz@2abf4075
DisposableBean...destroy

Bean 实现的接口解析

import org.springframework.beans.factory.BeanNameAware;
public void setBeanName(String beanName) {
	// 在当前类中可以获取到当前 Bean 的名字
}

import org.springframework.beans.factory.BeanFactoryAware;
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
	// 在当前类中可以获取到 BeanFactory 实例
}

import org.springframework.context.ApplicationContextAware;
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
	// 在当前类中可以获取到 ApplicationContext 实例
}

import org.springframework.beans.factory.InitializingBean;
public void afterPropertiesSet() throws Exception {
	// initMethod 方法
}

import org.springframework.beans.factory.DisposableBean;
public void destroy() throws Exception {
	// destroyMethod 方法
}

import org.springframework.beans.factory.config.BeanPostProcessor;
public Object postProcessAfterInitialization(Object bean, String arg1) throws BeansException {
	// 可以对对象进行加工,比如说加个初始化 Bean 时的开始时间
	return bean;
}
public Object postProcessBeforeInitialization(Object bean, String arg1) throws BeansException {
	// 可以对对象进行加工,比如说加个初始化 Bean 时的结束时间
	return bean;
}

你可能感兴趣的:(spring)