Working with property files outside of war using Spring

I am working on a Spring 3.0.5 web application that accesses LDAP and two databases. I have properties with configuration information for the LDAP server and these databases in applicationContext-security.xml and dispatcher-servlet.xml , but I would like each server to have different data properties without changing the file in the WAR. Can I somehow place the file somewhere else on the server and still access it from my application?

+6
source share
2 answers

Add this to your context

 <context:property-placeholder location="${envfile}"/> 

This will load the properties file located in $ {envfile}, a variable that you can set with the Java startup parameter in this way

 -Denvfile="file:/var/server/environment.properties" 

Or maybe in a Tomcat startup script

 CATALINA_OPTS=" -Denvfile=file:/var/server/environment.properties" 

Values ​​can be restored to your controllers using the Springs Value annotation as follows:

 @Values("${myvalue}") private String myValue; 

Please note that Spring 3.1 is required for these features, more details here

Good luck

+6
source

Try

 <util:properties id="props" location="file:///path/to/server.properties"/> 
0
source

Source: https://habr.com/ru/post/909772/


All Articles