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?
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.
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.
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.
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With