BeanFactoryPostProcessor入门
一、BeanFactoryPostProcessor是什么?
Spring对外提供的一个可在BeanFactory实例化期间,对BeanFactory进行操作的一个扩展接口
二、使用步骤
1.实现BeanFactoryPostProcessor接口及接口中的方法
代码如下(示例):
public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
}
}
2.放入IOC容器中(两种方式)
代码如下(示例):
2.1 放入单例池
AnnotationConfigApplicationContext acac = new AnnotationConfigApplicationContext();
acac.registerBean(MyBeanFactoryPostProcessor.class);
2.2 放入Bean工厂后置处理器集合
AnnotationConfigApplicationContext acac = new AnnotationConfigApplicationContext();
acac.addBeanFactoryPostProcessor(new MyBeanFactoryPostProcessor());