Solved

how to use external config file with Spring MVC

Posted on 2016-09-09
4
108 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Android studio getdrawable(int) is deprecated 4 85
Stream live video from Raspberry Pi camera 22 157
eclipse shortcuts 9 55
SHA2 certs for IIS AND Java? 2 88
There are numerous questions about how to setup an IBM HTTP Server to be administered from WebSphere Application Server administrative console. I do hope this article will wrap things up and become a reference for this task. You need three things…
Meet the world's only “Transparent Cloud™” from Superb Internet Corporation. Now, you can experience firsthand a cloud platform that consistently outperforms Amazon Web Services (AWS), IBM’s Softlayer, and Microsoft’s Azure when it comes to CPU and …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

773 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