Solved

how to use external config file with Spring MVC

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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…
The viewer will learn how to implement Singleton Design Pattern in Java.

758 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

19 Experts available now in Live!

Get 1:1 Help Now