BeanPostProcessor 接口允许在 Spring Bean Factory 返回 Bean instance 时修改 Bean 的创建过程。这是影响 Bean 生命周期的一部分。

Bean life cycle

要了解 BeanPostProcessor 接口就不得不提及 Bean 的生命周期。

Life cycle callbacks

Two groups:

  • Post-initialization
  • Pre-destruction

Life cycle:

  • Instantiation
  • Populate Properties
  • BeanNameAware’s setBeanName()
  • BeanFactoryAware’s setBeanFactory()
  • Pre-initialization BeanPostProcessors
  • InitializingBeans’ afterPropertiesSet()
  • Call custom init-method
  • Post-initialization BeanPostProcessors
  • Bean is ready to use ![[202101151422-how to become smarter]] Container is shutdown:

  • DisposableBean’s destroy()
  • Call custom destroy-method


Spring 提供了这些方法可以在生命周期过程中回调。

  • InitializingBean 和 DisposableBean
  • Spring 提供的一系列 *Aware 接口
  • 配置文件中自定义 init()distroy() 方法
  • 注解 @PostConstruct@PreDestroy

InitializingBean 和 DisposableBean


import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;

public class DemoBean implements InitializingBean, DisposableBean
	//Other bean attributes and methods

	public void afterPropertiesSet() throws Exception
		//Bean initialization code

	public void destroy() throws Exception
		//Bean destruction code

Aware interfaces

  • ApplicationContextAware 任何 bean 想要 ApplicationContext 启动时被通知可以实现该接口
  • ApplicationEventPublisherAware
  • BeanClassLoaderAware
  • BeanFactoryAware
  • BeanNameAware
  • BootstrapContextAware
  • LoadTimeWeaverAware
  • MessageSourceAware
  • NotificationPublisherAware
  • PortletConfigAware
  • PortletContextAware
  • ResourceLoaderAware
  • ServletConfigAware
  • ServletContextAware

Custom init() and destroy() methods

定义单个 Bean:

 <bean id="demoBean" class="info.einverne.deme.DemoBean"


<beans default-init-method="customInit" default-destroy-method="customDestroy">
    <bean id="demoBean" class="info.einverne.demo.DemoBean"></bean>

@PostConstruct and @PreDestroy

  • @PostConstruct annotated method will be invoked after the bean has been constructed using default constructor and just before it’s instance is returned to requesting object.
  • @PreDestroy annotated method is called just before the bean is about be destroyed inside bean container.


public class CustomBeanPostProcessor implements BeanPostProcessor {

  public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {"BeanPostProcessor postProcessBeforeInitialization for:" + beanName);
	return bean;

  public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {"BeanPostProcessor postProcessAfterInitialization for:gg" + beanName);
	return bean;