Spring控制台应用程序,从JAR外部加载属性文件

时间:2012-11-01 12:26:58

标签: java spring configuration

我在控制台Java应用程序中使用Spring。 我的应用程序将部署如下:

folder/myJar.jar
folder/db/connection.properties

如何在应用程序上下文中的PropertyPlaceholderConfigurer中加载 connection.properties

我试过了

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" value="db/connection.properties"/>
</bean>

但它不起作用。

我需要这样来获取我的数据库用户名/密码和其他详细信息。

2 个答案:

答案 0 :(得分:10)

将前缀file:添加到位置值:

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" value="file:db/connection.properties"/>
</bean>

答案 1 :(得分:-1)

指定文件位于值属性

中的类路径中
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" value="classpath:db/connection.properties"/>
</bean>
相关问题