0

I have a web application built with Spring. It connects to a database using JDBC and the JDBC properties (URL, username, password, and so on) are all currently hard-coded.

The web app is deployed as a WAR file and runs on Tomcat 8.5

I want to move these properties to a myapp.properties file that is outside of the deployed WAR file, where a user can tweak those properties to point to their own database.

On application startup, if the web app cannot find the file, I still want the application to start up and display a friendly "Sorry I can't connect to the database because the properties file is missing" message (rather than just have the web app fail to run).

I thought about putting myapp.properties in the Tomcat conf directory (ex: /bin/apache-tomcat-8.5.53/conf/myapp.properties) and always load it from there, but I don't know how to programmatically determine that location of the tomcat conf folder.

Any ideas on how I should approach this?

  • Does this answer your question? [how to read properties file in spring project?](https://stackoverflow.com/questions/14570477/how-to-read-properties-file-in-spring-project) – Arvind Kumar Avinash Apr 16 '20 at 17:13
  • @ArvindKumarAvinash Thanks for the link, but no it doesn't. That answer describes how to put properties in the Spring application context XML, but that is inside the WAR file. I need the properties file to live outside of the WAR file so it can be edited by the user (without opening the WAR file). –  Apr 16 '20 at 17:26

1 Answers1

0

I found a solution that works for me. I was able to read the 'catalina.home' environment variable to programmatically determine the location of tomcat.

Then I load my properties file from the tomcat/conf directory.

Full code below, seems to work well.

String catalinaHome = System.getProperties().getProperty("catalina.home");
String myappPropertiesFile = catalinaHome + File.separator + "conf" + File.separator + "myapp.properties";

Properties props = new Properties();
props.load(new FileInputStream(myappPropertiesFile));

String jdbcDriver = props.getProperty("jdbcDriver");
String jdbcUrl = props.getProperty("jdbcUrl");