配置Spring MVC应用程序

时间:2012-11-14 13:01:07

标签: tomcat spring-mvc configuration

在ASP.NET中,我有.config个XML文件来配置我的应用程序。很高兴能够登录到服务器并在配置需要更改时简单地编辑XML。应用程序会自动检测并自动重启。

如何使用Spring MVC获得同样的便利?

(目前我将我的MVC应用程序导出到部署在Tomcat Web应用程序管理器中的.war文件。如果我需要更改例如root-context.xml中的设置,我需要导出,取消部署和部署应用程序再次。繁琐而危险的操作。)

2 个答案:

答案 0 :(得分:2)

你可以在你的战争之外存储弹簧配置(例如,在tomcat的conf文件夹中)。

来自web.xml的以下片段告诉spring在哪里可以找到配置文件:

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

或者你可以移动一些东西,你需要经常从root-context.xml移动到属性文件,然后把它放在战争之外。

在spring-configuration中定义一个properties-placeholder来访问properties-file:

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

答案 1 :(得分:0)

您可以使用.properties扩展名创建Java properties configuration files,例如development.propertiesproduction.properties,并将其放入/WEB-INF/spring文件夹。这样的文件可以包含

myApp.Username = somename

然后从root-context.xml

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

使用root-context.xml文件中的配置值以及

等字符串
<property name="username" value="${myApp.Username}" /> 

部署.war文件时,您会注意到文件夹name of app/WEB-INF/spring/将出现在您的网络服务器上的部署文件夹中(例如/var/lib/tomcat7/webapps)。在这里您也可以找到root-context.xml。虽然未经验证,但我认为如果您在文本编辑器中编辑这些文件并重新启动Tomcat,您将实现您想要的目标。