如何在启动时在 Tomcat 配置中指定系统属性? [英] How can I specify system properties in Tomcat configuration on startup?

查看:40
本文介绍了如何在启动时在 Tomcat 配置中指定系统属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我可以通过使用 -D 参数传递参数来为 Tomcat 指定系统属性,例如-Dmy.prop=value".

我想知道是否有通过在 context.xml 文件或其他一些 tomcat 配置文件中指定属性值来做到这一点的更简洁的方法.我想这样做是因为,首先,更容易跟踪我的属性,其次,我有多个上下文在运行,但我不知道如何通过 -D 参数指定特定于上下文的属性.

我使用的是 Tomcat 5.5 版.

解决方案

(更新:如果我可以删除这个答案,我会的,虽然因为它被接受了,我不能.我正在更新描述以提供更好的指导和劝阻人们不要使用我在原始答案中概述的糟糕做法).

您可以通过上下文或环境参数指定这些参数,例如在 context.xml 中.请参阅此页面上标题为上下文参数"和环境条目"的部分:

http://tomcat.apache.org/tomcat-5.5-doc/config/context.html

正如@netjeff 指出的,这些值将通过 Context.lookup(String) 方法而不是作为系统参数提供.

另一种指定这些值的方法是在您正在部署的 Web 应用程序的 web.xml 文件中定义变量(见下文).正如@Roberto Lo Giacco 指出的那样,这通常被认为是一种糟糕的做法,因为部署的工件不应该是特定于环境的.但是,如果您真的想这样做,下面是配置片段:

<env-entry-name>SMTP_PASSWORD</env-entry-name><env-entry-type>java.lang.String</env-entry-type><env-entry-value>abc123ftw</env-entry-value></env-entry>

I understand that I can specify system properties to Tomcat by passing arguments with the -D parameter, for example "-Dmy.prop=value".

I am wondering if there is a cleaner way of doing this by specifying the property values in the context.xml file or some other tomcat configuration file. I would like to do this because, first, it is easier to keep track of my properties, and second, I have multiple contexts running and I don't know how I would specify context-specific properties through the -D parameter.

I am using Tomcat version 5.5.

解决方案

(Update: If I could delete this answer I would, although since it's accepted, I can't. I'm updating the description to provide better guidance and discourage folks from using the poor practice I outlined in the original answer).

You can specify these parameters via context or environment parameters, such as in context.xml. See the sections titled "Context Parameters" and "Environment Entries" on this page:

http://tomcat.apache.org/tomcat-5.5-doc/config/context.html

As @netjeff points out, these values will be available via the Context.lookup(String) method and not as System parameters.

Another way to do specify these values is to define variables inside of the web.xml file of the web application you're deploying (see below). As @Roberto Lo Giacco points out, this is generally considered a poor practice since a deployed artifact should not be environment specific. However, below is the configuration snippet if you really want to do this:

<env-entry>
    <env-entry-name>SMTP_PASSWORD</env-entry-name>
    <env-entry-type>java.lang.String</env-entry-type>
    <env-entry-value>abc123ftw</env-entry-value>
</env-entry>

这篇关于如何在启动时在 Tomcat 配置中指定系统属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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