Spring 扩展 -BeanFactoryPostProcessor
作者:生
- 2023-10-10 上海
本文字数:669 字
阅读完需:约 2 分钟
public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
System.out.println("执行postProcessBeanFactory方法");
}
}
class TestMyBeanFactoryPostProcessor {
public static void main(String[] args) {
ApplicationContext context = new MyClassPathXmlApplicationContext("applicationContext.xml");
}
}
复制代码
两种注入方式
xml 注入
<bean class="com.learn.MyBeanFactoryPostProcessor" ></bean>
复制代码
继承 ClassPathXmlApplicationContext 重写 customizeBeanFactory 方法进行注入
public class MyClassPathXmlApplicationContext extends ClassPathXmlApplicationContext {
/**
* 构造函数-必须要定义
*
* @param configLocations
* @throws BeansException
*/
public MyClassPathXmlApplicationContext(String... configLocations) throws BeansException {
super(configLocations);
}
@Override
protected void customizeBeanFactory(DefaultListableBeanFactory beanFactory) {
super.addBeanFactoryPostProcessor(new MyBeanFactoryPostProcessor());
super.customizeBeanFactory(beanFactory);
}
}
复制代码
测试
ApplicationContext context = new MyClassPathXmlApplicationContext("applicationContext.xml");
// ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
复制代码
测试结果>控制台输出: 执行 postProcessBeanFactory 方法
划线
评论
复制
发布于: 刚刚阅读数: 5
生
关注
还未添加个人签名 2019-08-17 加入
还未添加个人简介
评论