我不知道你使用的是哪个版本的Tomcat,但是在Tomcat 7的文件 catalina.sh 中,你可以指定变量CATALINA_OPTS这个变量将传递给jvm。
但是,设置环境变量可能不是实现目标的最佳方式。也许最好的办法是创建单独的“app.properties”文件,并将其包含在 applicationContext 中,如下所示:
<context:property-placeholder location="classpath*:app.properties" />
和 catalina.sh 解决方案
# CATALINA_OPTS (Optional) Java runtime options used when the "start",
# "run" or "debug" command is executed.
# Include here and not in JAVA_OPTS all options, that should
# only be used by Tomcat itself, not by the stop process,
# the version command etc.
# Examples are heap size, GC logging, JMX ports etc.
例:
CATALINA_OPTS=“-Dfolder=Dev”
编辑:
对于窗口,它应该是这样的set CATALINA_OPTS="-Dfolder=Dev"
编辑:
在Spring配置中,你可以像${propertyname}一样使用系统属性,也可以包括带有属性定义的文件,以及该文件中定义的所有属性在config中也变得可用。context:property-placeholder
例如,您具有基本集属性:config.properties 和具有 db 连接设置(DEV.properties、UAT.properties、PROD.properties)的文件集。那么,如何为不同的环境包含不同的属性呢?可以通过多种方式完成,例如,在catalina中设置系统属性.bat
set CATALINA_OPTS="-Dbuild=DEV"
和应用程序配置.xml
<context:property-placeholder location="classpath*:${build}.properties, classpath*:config.properties" />
或者,您可以创建不同的构建配置,并在最终 WAR 中为每个构建配置仅包含一个属性(DEV、UAT、PROD)。在应用程序中配置设置如下内容:
<context:property-placeholder location="classpath*:*.properties" />