无处不在的扩展点BeanPostProcessor

无处不在的扩展点BeanPostProcessor


    在spring创建bean的过程中,多次出现了BeanPostProcessor的身影,它们是怎么来的,可以怎么用?本文就对spring中的BeanPostProcessor做个总结。
    
public interface BeanPostProcessor {
	
	Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException;

	
	Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException;
}

     如上所示,BeanPostProcessor接口中其实才定义了两个方法,这两个方法分别在invokeInitMethods的前后调用。还记得invokeInitMethods吗,它回调了bean的afterPropertiesSet方法以及bean的initMethod。除了最基本的BeanPostProcessor,spring中还定义一些扩展的BeanPostProcessor。有MergedBeanDefinitionPostProcessor,它只增加了一个方法在bean实例化之后马上执行。
public interface MergedBeanDefinitionPostProcessor extends BeanPostProcessor {

	void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, Class beanType, String beanName);

}
   InstantiationAwareBeanPostProcessor,它扩展了三个方法,分别在实例化之前、实例化之后依赖注入之前、依赖解析之后而注入之前执行。另外,还有SmartInstantiationAwareBeanPostProcessor、DestructionAwareBeanPostProcessor。
    
     这些BeanPostProcessor各司其职,在bean的整个生命周期的各个阶段发挥着不同的作用,使我们可以在bean的生命周期的每个阶段都可以做一些准备工作或后期处理。讲到这里不得不赞叹spring的强大,spring设计的巧妙。正是由于这些BeanPostProcessor,给我们留出了充足的扩展点,我们才可以以插件式的形式在扩展点植入我们自己的逻辑。对于没有自己写过BeanPostProcessor的同学,只是在配置文件中加入了一些标签就有了对应的BeanPostProcessor,那么这些BeanPostProcessor是怎么来的呢?这要从spring上下文的建立,也就是ApplicationContext的初始化说起,在AbstractApplicationContext的refresh方法中创建了BeanFactory并且对其进行了配置,其中就包括向BeanFactory中注册bean的后置处理器BeanPostProcessor。见如下两个方法:
	protected void registerBeanPostProcessors(ConfigurableListableBeanFactory beanFactory) {
		String[] postProcessorNames = beanFactory.getBeanNamesForType(BeanPostProcessor.class, true, false);

		// Register BeanPostProcessorChecker that logs an info message when
		// a bean is created during BeanPostProcessor instantiation, i.e. when
		// a bean is not eligible for getting processed by all BeanPostProcessors.
		int beanProcessorTargetCount = beanFactory.getBeanPostProcessorCount() + 1 + postProcessorNames.length;
		beanFactory.addBeanPostProcessor(new BeanPostProcessorChecker(beanFactory, beanProcessorTargetCount));

		// Separate between BeanPostProcessors that implement PriorityOrdered,
		// Ordered, and the rest.
		List priorityOrderedPostProcessors = new ArrayList();
		List internalPostProcessors = new ArrayList();
		List orderedPostProcessorNames = new ArrayList();
		List nonOrderedPostProcessorNames = new ArrayList();
		for (String ppName : postProcessorNames) {
			if (isTypeMatch(ppName, PriorityOrdered.class)) {
				BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
				priorityOrderedPostProcessors.add(pp);
				if (pp instanceof MergedBeanDefinitionPostProcessor) {
					internalPostProcessors.add(pp);
				}
			}
			else if (isTypeMatch(ppName, Ordered.class)) {
				orderedPostProcessorNames.add(ppName);
			}
			else {
				nonOrderedPostProcessorNames.add(ppName);
			}
		}

		// First, register the BeanPostProcessors that implement PriorityOrdered.
		OrderComparator.sort(priorityOrderedPostProcessors);
		registerBeanPostProcessors(beanFactory, priorityOrderedPostProcessors);

		// Next, register the BeanPostProcessors that implement Ordered.
		List orderedPostProcessors = new ArrayList();
		for (String ppName : orderedPostProcessorNames) {
			BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
			orderedPostProcessors.add(pp);
			if (pp instanceof MergedBeanDefinitionPostProcessor) {
				internalPostProcessors.add(pp);
			}
		}
		OrderComparator.sort(orderedPostProcessors);
		registerBeanPostProcessors(beanFactory, orderedPostProcessors);

		// Now, register all regular BeanPostProcessors.
		List nonOrderedPostProcessors = new ArrayList();
		for (String ppName : nonOrderedPostProcessorNames) {
			BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
			nonOrderedPostProcessors.add(pp);
			if (pp instanceof MergedBeanDefinitionPostProcessor) {
				internalPostProcessors.add(pp);
			}
		}
		registerBeanPostProcessors(beanFactory, nonOrderedPostProcessors);

		// Finally, re-register all internal BeanPostProcessors.
		OrderComparator.sort(internalPostProcessors);
		registerBeanPostProcessors(beanFactory, internalPostProcessors);

		beanFactory.addBeanPostProcessor(new ApplicationListenerDetector());
	}

	private void registerBeanPostProcessors(
			ConfigurableListableBeanFactory beanFactory, List postProcessors) {

		for (BeanPostProcessor postProcessor : postProcessors) {
			beanFactory.addBeanPostProcessor(postProcessor);
		}
	}
  正是由于ApplicationContext对BeanFactory的增强,使我们在使用ApplicationContext的时候不必关系一些细节就可以获得拥有强大功能的容器。既然 ApplicationContext的BeanPostProcessor 也是直接从Beanfactory中获取的,那么这些BeanPostProcessor 到底是哪里注册到BeanFactory中去的呢?如果不是你自己注册的,那么肯定就是配置文件中的一些标签中引入的了,比如context:component-scan。


你可能感兴趣的:(sping)