Solved

how to use external config file with Spring MVC

Posted on 2016-09-09
4
89 Views
Last Modified: 2016-09-13
I am using Spring MVC framework to develop my Java Web application.
I put a config.properties under src/main/resources.
a=1
c=2

Open in new window

In my Spring MVC controller, I use code like below to retrieve the config value.
                        InputStream inputStream = getClass().getClassLoader().getResourceAsStream(propFileName);
			if(null != inputStream) {
				prop.load(inputStream);
			} else {
				throw new FileNotFoundException("property file not found in the classpath");
			}
			
			a = prop.getProperty("a");
			b = prop.getProperty("b");

Open in new window


And then I packaged the project with "mvn clean package" and deployed the war into Tomcat's webapps folder.

However, with this approach, my config.properties is packaged into the war and it is hard to change.

So would some one please provide a way to solve this problem? Can I use spring to read external config file? Please note that I need to read the config file every time the controller method is called instead of loading it when spring starts. Thanks!
0
Comment
Question by:condor888
  • 2
  • 2
4 Comments
 
LVL 15

Accepted Solution

by:
gurpsbassi earned 500 total points
ID: 41790919
well, if you want to change your config, you will need to stop and start tomcat to modify the config file.

Or

make the config file reference system properties (JVM props) that you can change outside of tomcat.

Or

Make tomcat reference the config file as a set of JNDI bindings which would mean tomcat points to the file in a certain file location outside the war. (not done this with tomcat before but works ok with jboss/wildfly)

Or

make sure the config file gets deployed into web-inf/classes
0
 

Author Comment

by:condor888
ID: 41791026
Thanks, gurpsbassi. Basically what I need is probably deploying the war and config file separately and letting the war be able to find the config file. So When tomcat is running, I can change the config file so that different response may be returned due to the change of the config file while tomcat doesn't need to be restarted.
0
 
LVL 15

Expert Comment

by:gurpsbassi
ID: 41791199
You may still need to restart tomcat if the external config changes.
0
 

Author Closing Comment

by:condor888
ID: 41797247
Thanks for your answer!
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In Solr 4.0 it is possible to atomically (or partially) update individual fields in a document. This article will show the operations possible for atomic updating as well as setting up your Solr instance to be able to perform the actions. One major …
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now