Working with properties files outside war with Spring
Asked Answered
M

2

6

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?

Mehalek answered 1/3, 2012 at 19:18 Comment(0)
C
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!

Coronograph answered 4/3, 2012 at 0:39 Comment(1)
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
F
0

Try

<util:properties id="props" location="file:///path/to/server.properties"/>
Fugate answered 1/3, 2012 at 19:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.