Gruber
Gruber

Reputation: 4558

Configuring Spring MVC applications

In ASP.NET I have .config XML files which configure my applications. It is nice to be able to log on to the server and simply edit the XML when a configuration needs to change. The application self-detects this and restarts automatically.

How can I get the same convenience with Spring MVC?

(Currently I export my MVC applications to .war files which are deployed in Tomcat Web Application Manager. If I need to change settings in for instance root-context.xml I need to export, undeploy and deploy the application again. A tedious and risky operation.)

Upvotes: 1

Views: 336

Answers (2)

Gruber
Gruber

Reputation: 4558

You can create Java properties configuration files with .properties extension, for instance development.properties and production.properties and put them in the /WEB-INF/springfolder. Such a file could contain

myApp.Username = somename

Then reference the file from root-context.xml:

<bean id="applicationProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" value="/WEB-INF/spring/production.properties"/>
</bean>

Use the configuration values in your root-context.xml file with strings like

<property name="username" value="${myApp.Username}" /> 

When you deploy your .war file, you will notice that the folder name of app/WEB-INF/spring/ will appear in your deployment folder on your webserver (for example /var/lib/tomcat7/webapps). Here you can find root-context.xml as well. Although not verified, I assume that if you edit these files in a text editor and restart Tomcat you will achieve what you want.

Upvotes: 0

Yevgeniy
Yevgeniy

Reputation: 2694

you could store the spring-configuration outside of your war (f.e. in the conf folder of your tomcat).

following snippet from web.xml tells spring where to find the configuration file:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>file:/foo/bar/root-context.xml</param-value>
</context-param>

or you could move things, which you need to change often from your root-context.xml to a properties-file and place it then outside the war.

define a properties-placeholder in your spring-configuration to access the properties-file:

<context:property-placeholder location="file:/foo/bar/root-context.properties />

Upvotes: 2

Related Questions