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?
Working with properties files outside war with Spring
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!
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 ? –
Karame
Try
<util:properties id="props" location="file:///path/to/server.properties"/>
© 2022 - 2024 — McMap. All rights reserved.