2012-11-01 220 views
0

我在一个控制台Java应用程序中使用Spring的外荷载属性文件。 我将应用程序部署,如:春控制台应用程序,从JAR

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

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

我已经试过

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

,但它不会工作。

我需要它这样对我的数据库的用户名/密码和其他细节。

+0

添加“”到你的班级路径? –

回答

10

类路径前缀file:添加到位置值:

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

那不是从文件系统的根目录开始?因此必须存放在/db/connection.properties或C:/db/connection.properties?我想你想要相对于类路径。也许我错过了一些东西。 –

+0

它相对于项目文件夹@ kmb385。 'file:/ db/properties'将意味着你在UNIX/Windows文件系统中分别引用的内容。 – Vikdor

+0

刚刚在spring文档中找到它,没有附加到FileSystemApplicationContext(即, FileSystemApplicationContext不是实际的ResourceLoader)的FileSystemResource将按照您的预期处理绝对对象和 相对路径。相对路径是相对于当前工作目录,而 绝对路径相对于所述文件系统的根。 –

-1

指定该文件是在价值属性

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