Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Working with properties files outside war with Spring

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?

like image 726
SJS Avatar asked Mar 01 '12 19:03

SJS


People also ask

How do you read properties filed outside war?

Provide the file name using context param or java system parameter. But, it is again property file location is war dependent. You should change war file to change propert file location. If i am correct the requirement was to change the props in the file , not the file as such.

How do I load external property files into spring boot?

Imagine you have an external config file: application-external. yml in the conf/ dir under your home directory, just add it like this: -Dspring. config. location=file:${home}/conf/application-external.

How could you externalize constants from a spring configuration file into a properties file?

You can use properties files, YAML files, environment variables and command-line arguments to externalize configuration. Property values can be injected directly into your beans using the @Value annotation, accessed via Spring's Environment abstraction or bound to structured objects.


1 Answers

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!

like image 111
Wesley Avatar answered Oct 18 '22 10:10

Wesley