Spring IOC之后置处理器分析(四)

目录

  • 一、BeanPostProcessor
  • 二、InstantiationAwareBeanPostProcessor
  • 三、BeanFactoryPostProcessor
  • 四、BeanDefinitionRegistryPostProcessor

一、BeanPostProcessor

Bean的后置处理器,主要在bean初始化前后工作。

public interface BeanPostProcessor {
	// 在初始化方法(如:afterPropertiesSet 或 init-method)执行前触发
	Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException;

	// 在初始化方法(如:afterPropertiesSet 或 init-method)执行后触发
	Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException;
}

二、InstantiationAwareBeanPostProcessor

InstantiationAwareBeanPostProcessor继承于BeanPostProcessor,主要在实例化Bean前后工作; AOP创建代理对象就是通过该接口实现。

public interface InstantiationAwareBeanPostProcessor extends BeanPostProcessor {
	// 对象实例化前
	Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException;

	// 对象实例化后
	boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException;

	PropertyValues postProcessPropertyValues(
			PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) throws BeansException;
}

三、BeanFactoryPostProcessor

Bean工厂的后置处理器,在Bean定义(BeanDefinition)加载完成后,Bean尚未初始化前执行。

public interface BeanFactoryPostProcessor {
	/**
	 * Modify the application context's internal bean factory after its standard
	 * initialization. All bean definitions will have been loaded, but no beans
	 * will have been instantiated yet. This allows for overriding or adding
	 * properties even to eager-initializing beans.
	 * @param beanFactory the bean factory used by the application context
	 * @throws org.springframework.beans.BeansException in case of errors
	 */
	void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException;
}

四、BeanDefinitionRegistryPostProcessor

BeanDefinitionRegistryPostProcessor继承于BeanFactoryPostProcessor。其自定义的方法postProcessBeanDefinitionRegistry会在Bean定义(BeanDefinition)将要加载,Bean尚未初始化前真执行,即在BeanFactoryPostProcessor的postProcessBeanFactory方法前被调用。

public interface BeanDefinitionRegistryPostProcessor extends BeanFactoryPostProcessor {
	/**
	 * Modify the application context's internal bean definition registry after its
	 * standard initialization. All regular bean definitions will have been loaded,
	 * but no beans will have been instantiated yet. This allows for adding further
	 * bean definitions before the next post-processing phase kicks in.
	 * @param registry the bean definition registry used by the application context
	 * @throws org.springframework.beans.BeansException in case of errors
	 */
	void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException;
}

你可能感兴趣的:(spring)