主要工作
上面三个过程,都是在做准备工作,接下来的工作就是要开始干活了。
首先就给用户一个扩展点,AbstractApplicationContext # postProcessBeanFactory(beanFactory) 
try {// Allows post-processing of the bean factory in context subclasses.postProcessBeanFactory(beanFactory);......}......protected void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {}
IOC容器当前的状态:所有的 bean 定义都已经加载完毕,并且都放在缓存中了。但是还没有进行实例化操作。
扩展:
- 通过 postProcessBeanFactory 方法可以实现对 BeanDefinition 数据的操作。
 除此之外可以通过 BeanFactoryPostProcessor 扩展对 BeanDefinition 操作,多个可以通过 Ordered、PriorityOrdered 接口进行排序。
代码演示
重写方法
首先我们先重写 postProcessBeanFactory 方法演示:
/*** 自定义ApplicationContext*/public class MyApplicationContext2 extends ClassPathXmlApplicationContext {public MyApplicationContext2(String configLocation) throws BeansException {super(configLocation);}@Overrideprotected void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {int beanDefinitionCount = beanFactory.getBeanDefinitionCount();System.out.println("beanDefinitionCount:" + beanDefinitionCount);}}
加载bean配置文件,bean2.xml
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN""https://www.springframework.org/dtd/spring-beans-2.0.dtd"><beans><bean id="user" class="cn.lichenghao.entity.User"><property name="name"><value>Rod</value></property><property name="age"><value>31</value></property></bean></beans>
测试下
/*** 测试扩展** @author chenghao.li*/public class App4 {public static void main(String[] args) {MyApplicationContext2 myApplicationContext = new MyApplicationContext2("bean2.xml");User user = myApplicationContext.getBean(User.class);user.sayHello();}}
结果
结果:> Task :spring-lich-test:App4.main()beanDefinitionCount:2hello world!
接口扩展
我们使用扩展点接口,分别打印加载的 bean 定义的个数:
/*** 自定义扩展** @author chenghao.li*/public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor, Ordered {@Overridepublic void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)throws BeansException {int beanDefinitionCount = beanFactory.getBeanDefinitionCount();System.out.println("MyBeanFactoryPostProcessor beanDefinitionCount:" + beanDefinitionCount);}@Overridepublic int getOrder() {return 0;}}
/*** 自定义扩展** @author chenghao.li*/public class MyBeanFactoryPostProcessor2 implements BeanFactoryPostProcessor, PriorityOrdered {@Overridepublic void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)throws BeansException {int beanDefinitionCount = beanFactory.getBeanDefinitionCount();System.out.println("MyBeanFactoryPostProcessor2 beanDefinitionCount:" + beanDefinitionCount);}@Overridepublic int getOrder() {return 1;}}
bean.xml
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN""https://www.springframework.org/dtd/spring-beans-2.0.dtd"><beans><bean id="user" class="cn.lichenghao.entity.User"><property name="name"><value>Rod</value></property><property name="age"><value>31</value></property></bean><bean id="myBeanFactoryPostProcessor" class="cn.lichenghao.context.MyBeanFactoryPostProcessor"/><bean id="myBeanFactoryPostProcessor2" class="cn.lichenghao.context.MyBeanFactoryPostProcessor2"/></beans>
测试下
/*** 测试扩展** @author chenghao.li*/public class App4 {public static void main(String[] args) {MyApplicationContext2 myApplicationContext = new MyApplicationContext2("bean2.xml");User user = myApplicationContext.getBean(User.class);user.sayHello();}}
测试结果
> Task :spring-lich-test:App4.main()beanDefinitionCount:3MyBeanFactoryPostProcessor beanDefinitionCount:3MyBeanFactoryPostProcessor2 beanDefinitionCount:3hello world!
注意事项
- 这个阶段让用户注册自己的扩展取操作 bean 的定义数据,不要在这个阶段去实例化 bean。
 - 通过接口 (BeanFactoryPostProcessor) 注册的扩展,会在下个阶段 (invokeBeanFactoryPostProcessors) 进行调用。
 
