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

查看:536
本文介绍了如何在启动时在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>
    <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天全站免登陆