前提,java工程是jar项目,想将数据库链接等放到对应的config目录
项目是通过spring进行管理的,applicationContext.xml加载jdbc的属性文件,采用classpath之类来加载是不行的
通过如下方式:
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>file:${user.dir}/config/jdbc.properties</value>
<value>file:${user.dir}/config/constants.properties</value>
</list>
</property>
</bean>
但是有个缺点,只有进入到jar项目的根目录启动才能识别到属性文件。
我们也可以通过启动项目时,设定user.dir的路径为项目所在的地址。