I am working on a Spring 3.0.5 web application that accesses LDAP and two databases. I have a properties with configuration information for the LDAP server and that databases, in applicationContext-security.xml
and dispatcher-servlet.xml
, but I would like to make it so each server can have different data properties without changing a file in the WAR. Can I somehow put a file somewhere else on the server and still access it from within my application?
Asked
Active
Viewed 5,688 times
6

Perception
- 79,279
- 19
- 185
- 195

SJS
- 5,607
- 19
- 78
- 105
2 Answers
6
Add this to your context
<context:property-placeholder location="${envfile}"/>
This will load the properties file located at ${envfile}, a variable you can set with Java's startup paramater like this
-Denvfile="file:/var/server/environment.properties"
Or maybe in Tomcat's startup script
CATALINA_OPTS=" -Denvfile=file:/var/server/environment.properties"
Values can be retrieved in your controllers using Springs Value annotation like this:
@Values("${myvalue}")
private String myValue;
Please note that these features require Spring 3.1, more information here
Good luck!

Wesley
- 693
- 4
- 9
-
I am using Spring version 4. Seems there is no Values annotation. Using Value annotation doesn't populate my values. Also, I am getting message on application startup that property file has been loaded. 2017-04-05 17:27:51 INFO PropertySourcesPlaceholderConfigurer:172 - Loading properties file from URL [file:c://java//resources//googleDocsValues.properties]. Any suggestions ? – Ankit Apr 05 '17 at 12:11
0
Try
<util:properties id="props" location="file:///path/to/server.properties"/>

Michael-O
- 18,123
- 6
- 55
- 121