Spring console app, load property file from outsid

2020-06-06 07:11发布

I'm using Spring in a console Java application. My application will be deployed like:

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

How do I load the connection.properties in a PropertyPlaceholderConfigurer in application context?

I have tried

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

but it won't work.

I need it this way for my database username/password and other details.

2条回答
兄弟一词,经得起流年.
2楼-- · 2020-06-06 08:00

Add the prefix file: to the location value:

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" value="file:db/connection.properties"/>
</bean>
查看更多
聊天终结者
3楼-- · 2020-06-06 08:03

Specify that the file is on the classpath in the value attribute

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" value="classpath:db/connection.properties"/>
</bean>
查看更多
登录 后发表回答