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!
Java EEJavaJava App ServersApache Web ServerApplication Servers

Avatar of undefined
Last Comment
condor888

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
gurpsbassi

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
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.
gurpsbassi

You may still need to restart tomcat if the external config changes.
condor888

ASKER
Thanks for your answer!
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy