将 VM 参数传递给 Apache Tomcat [英] Pass VM Argument to Apache Tomcat

查看:27
本文介绍了将 VM 参数传递给 Apache Tomcat的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为-Dfolder"的虚拟机参数的 webProject

I have a webProject with a VM Argument called "-Dfolder"

我像这样在 applicationContext 上使用该参数:

I use that argument on applicationContext like this:

<value>file:${FNET_CORE_CONFIG}/conf/${folder}/jdbc.properties</value>

在 Eclipse 中,为了测试,我使用运行配置"来设置这样的值:

In Eclipse, for testing, i use "Run Configuration" to set the value like this:

-Dfolder=Dev

现在,我想在 Apache Tomcat 上测试我的 webapp,所以我需要设置/发送文件夹 VM Argument.

Now, I want to test my webapp on Apache Tomcat so I need to set/send the folder VM Argument.

我是怎么做到的?

我必须使用 setenv.sh 吗?如何?.有人能给我举个例子吗?

I have to use setenv.sh? How?. Can someone give me and example?

感谢和抱歉我的英语

推荐答案

我不知道你使用的是什么版本的 Tomcat,但是在 Tomcat 7 文件 catalina.sh 中你可以指定变量 CATALINA_OPTS 并且这个变量会传递给 jvm.

I don't know what version of Tomcat you using, but in Tomcat 7 in file catalina.sh you can specify variable CATALINA_OPTS and this variable will pass to jvm.

但也许设置环境变量并不是实现目标的最佳方式.也许最好的方法是创建单独的app.properties"文件,并将其包含在 applicationContext 中,如下所示:

But maybe setting environment variable isn't a best way to achive your goal. Maybe best will be creation of separate "app.properties" file, and including it in applicationContext like this:

<context:property-placeholder location="classpath*:app.properties" />

以及 catalina.sh 的解决方案

And solution for 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"

CATALINA_OPTS="-Dfolder=Dev"

对于 Windows,它应该类似于 set CATALINA_OPTS="-Dfolder=Dev"

for windows it should be something like set CATALINA_OPTS="-Dfolder=Dev"

在 Spring 配置中,您可以像 ${propertyname} 一样使用系统属性,也可以包含带有属性定义的文件,带有 context:property-placeholder,并且所有在该文件中定义的属性也变为可在配置中使用.

In Spring configuration you can use system property just like ${propertyname}, and also can include file with property definition, with context:property-placeholder, and all defined in that file properties also become avaliable in config.

例如,您有基本集属性:config.properties,以及具有数据库连接设置的文件集(DEV.properties、UAT.properties、PROD.properties).那么,如何为不同的环境包含不同的属性?可以通过多种方式实现,例如在catalina.bat中设置系统属性

For example, you have base set properties: config.properties, and set of files with db connection settings (DEV.properties, UAT.properties, PROD.properties). So, how can you include different properties for different environment? It can be done it many ways, for example, set system properties in catalina.bat

set CATALINA_OPTS="-Dbuild=DEV"

并在 applicationConfig.xml 中

and in applicationConfig.xml

<context:property-placeholder location="classpath*:${build}.properties, classpath*:config.properties" />

或者您可以创建不同的构建配置,并在最终 WAR 中为每个构建配置仅包含一个属性(DEV、UAT、PROD).在 applicationConfig 中设置如下:

Or you can create different build configuration and include in final WAR only one properties (DEV, UAT, PROD) for each build configuration. In applicationConfig set something like:

<context:property-placeholder location="classpath*:*.properties" />

这篇关于将 VM 参数传递给 Apache Tomcat的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆