一、PropertyPlaceholderConfigurer代码分支property-placeholder-configurer经常需要将配置信息配置在properties文件中然后在XML文件中以占位符的方式引用。实现思路很简单在bean实例化之前编辑BeanDefinition解析XML文件中的占位符然后用properties文件中的配置值替换占位符。而BeanFactoryPostProcessor具有编辑BeanDefinition的能力因此PropertyPlaceholderConfigurer继承自BeanFactoryPostProcessor。测试 car.propertiesbrandlamborghini?xml version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beans xmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xmlns:contexthttp://www.springframework.org/schema/context xsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd bean classorg.springframework.beans.factory.PropertyPlaceholderConfigurer property namelocation valueclasspath:car.properties / /bean bean idcar classorg.springframework.test.bean.Car property namebrand value${brand} / /bean /beanspublicclassPropertyPlaceholderConfigurerTest{Testpublicvoidtest()throwsException{ClassPathXmlApplicationContextapplicationContextnewClassPathXmlApplicationContext(classpath:property-placeholder-configurer.xml);CarcarapplicationContext.getBean(car,Car.class);assertThat(car.getBrand()).isEqualTo(lamborghini);}}二、代码追踪2.1 前期提要且PropertyPlaceholderConfigurer是实现了BeanFactoryPostProcessor2.2 代码追踪1、新建上下文2、调用构造函数新建上下文3、refresh方法4、这里是重点在bean实例化之前执行BeanFactoryPostProcessor5、在bean实例化之前执行BeanFactoryPostProcessor6、org.springframework.beans.factory.PropertyPlaceholderConfigurer#postProcessBeanFactory做了什么7、替换占位符的过程来看一下经过resolvePropertyValues就替换陈这个了2.3 注意点疑问1PropertyPlaceholderConfigurer是定义在xml里了但是上面第【5】步是“在bean实例化之前执行BeanFactoryPostProcessor”呀为啥走到第【6】步location有值了答因为在org.springframework.context.support.AbstractApplicationContext#invokeBeanFactoryPostProcessors方法调用org.springframework.beans.factory.ListableBeanFactory#getBeansOfType时已经实例化了BeanFactoryPostProcessor对象了1、获取BeanFactoryPostProcessor对象2、获取BeanFactoryPostProcesso类型r的bean对象3、这里就是实例化bean对象了4、后面我就不一一追下去了有兴趣可以自己追一下。。。