PropertyPlaceholderConfigurer如何读取配置文件路径的?

PropertyPlaceholderConfigurer如何读取配置文件路径的

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  <property name="locations">
   <list>
    <value>/WEB-INF/mail.properties</value>  
    <value>classpath:conf/jdbc.properties</value
   </list>
  </property>
</bean>

这个配置中的locations,我在PropertyPlaceholderConfigurer中没有发现有这个属性,dubug进行查看PropertyPlaceholderConfigurer发现Properties类中已经有jdbc.properties中的数据了。
我现在想知道PropertyPlaceholderConfigurer加载conf/jdbc.properties属性文件是在什么时候哪里做的

阅读 5.6k
1 个回答

先回答在哪
PropertiesLoaderSupport这个抽象类,是PropertyPlaceholderConfigurer父类,Properties文件的位置是在这里设置。

public void setLocation(Resource location) {
    this.locations = new Resource[] {location};
}

再回答什么时候
如果了解一点spring源码,会看到spring容器启动都是通过调用refresh方法实现。在refresh中有一个方法调用invokeBeanFactoryPostProcessors(beanFactory)的作用就是去实现BeanFactoryPostProcessor这个接口的方法

void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException;

PropertyPlaceholderConfigurer的一个父类PropertyResourceConfigurer就实现了这个接口,所以spring在完成BeanFactory的初始化时就会调用上述提到的方法。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题