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.
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!
I put a config.properties under src/main/resources.
a=1
c=2
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");
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You may still need to restart tomcat if the external config changes.
ASKER
Thanks for your answer!
ASKER