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

查看:353
本文介绍了java - 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属性文件是在什么时候哪里做的

解决方案

先回答在哪
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的初始化时就会调用上述提到的方法。

这篇关于java - PropertyPlaceholderConfigurer如何读取配置文件路径的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆