ITEEDU

Spring Gossip: BeanFactoryPostProcessor

在BeanFactory加载Bean定义档的所有内容,但还没正式产生Bean实例之前,您可以对该BeanFactory进行一些处理,您只要实作 org.springframework.beans.factory.config.BeanFactoryPostProcessor:

package org.springframework.beans.factory.config;
public interface BeanFactoryPostProcessor {
	public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException;
}

假设您有一个SomClass实作了BeanFactoryPostProcessor,则您可以在Bean定义档中定义它:

....
<beans>
	<bean id="beanFactoryModifier" class="onlyfun.caterpillar.SomeClass"/>
	<bean id="helloBean" class="onlyfun.caterpillar.HelloBean">
		....
	</beans>

使用ApplicationContext时,若Bean定义档中有定义实作BeanFactoryPostProcessor的类别,则ApplicationContext会自动应用。

在Spring中有几个BeanFactoryPostProcessor的实作实例,像是org.springframework.beans.factory.config.PropertyPlaceholderConfigurerorg.springframework.beans.factory.config.PropertyOverrideConfigurerorg.springframework.beans.factory.config.CustomEditorConfigurer