动态设置spring配置PropertyPlaceholderConfigurer location的路径

  在spring中经常将常用的属性放在properties文件中,然后再spring的配置文件中使用PropertyPlaceholderConfigurer引用properties文件。
对于web项目来说,可以通过相对路径得到配置文件的路径,而对于可执行项目,在团队开发中就需要根据各自的环境来指定properties配置文件的路径了。
对于这种情况可以将配置文件的路径放在java虚拟机JVM的自定义变量中,例如:-Dconfig.path=/yourpath/1.properties
如下在配置文件中使用$config.path来引用JVM自定义变量的值就可以了:

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN""http://www.springframework.org/dtd/spring-beans.dtd"> 
<beans>        
<bean id="configBean"  class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">           
  <property name="location">               
    <value>$config.path</value>           
  </property>       
</bean> 
<bean id="helloBean" class="onlyfun.caterpillar.HelloBean">           
  <property name="helloWord">               
    <value>${onlyfun.caterpillar.helloWord}</value>           
  </property>       
</bean>
</beans>
原文地址:https://www.cnblogs.com/rinack/p/6053942.html