Link to home
Start Free TrialLog in
Avatar of condor888
condor888

asked on

how to use external config file with Spring MVC

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!
ASKER CERTIFIED SOLUTION
Avatar of gurpsbassi
gurpsbassi
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of condor888
condor888

ASKER

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.
You may still need to restart tomcat if the external config changes.
Thanks for your answer!