Java 为 Web 应用程序设置指定的系统属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24582695/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 13:12:07  来源:igfitidea点击:

Set specified system property for web application

javaservletsjvm

提问by hguser

I have an JavaEE application which needs some certain system properties configured during the runtime.

我有一个 JavaEE 应用程序,它需要在运行时配置一些特定的系统属性。

During the development phase, we set the properties in the .pom.xmlto make it work:

在开发阶段,我们在 中设置属性.pom.xml以使其工作:

        <plugin>
            <groupId>org.apache.tomcat.maven</groupId>
            <artifactId>tomcat7-maven-plugin</artifactId>
            <version>2.2</version>
            <configuration>
                <systemProperties>
                <xxx>true</xxx>
                </systemProperties>
            </configuration>
        </plugin>

However I wonder how about if we deploy the application in the production environment?

但是我想知道如果我们在生产环境中部署应用程序怎么样?

I have though of set the property during the initialization of the servlet, but it seems that I can not modify the system property once the jvm is runing(from this post):

我虽然在 servlet 的初始化过程中设置了属性,但是一旦 jvm 运行,我似乎无法修改系统属性(来自这篇文章):

The JVM memory parameters for a Hotspot Java implementation can only be set via the command line options when the JVM is launched / started. Setting them in the system properties either before or after the JVM is launched will have no effect.

Hotspot Java 实现的 JVM 内存参数只能在 JVM 启动/启动时通过命令行选项设置。在 JVM 启动之前或之后在系统属性中设置它们将不起作用。

And I have tried to set the properties in the web.xml(here):

我已经尝试在web.xml(这里) 中设置属性:

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

But it seems like it does not take affect.

不过好像没有什么影响。

Then I wonder how to solve it?

那我想知道怎么解决?

BTW, in the production environment we may run more than one application under a shared tomcat, so we prefer to set the properties under the application context only.

顺便说一句,在生产环境中,我们可能会在一个共享的 tomcat 下运行多个应用程序,因此我们更愿意只在应用程序上下文下设置属性。

采纳答案by tmarwen

Environmental Entries

环境条目

The <env-entry>won't make it for you because Environmental Entriesare availble through JNDIand not through the Systemproperties facade. So if you configure some entry as below in your deployment descriptor file:

<env-entry>不会让你因为Environmental Entries是availble的通过JNDI而不是通过System性外观。因此,如果您在部署描述符文件中配置如下条目:

<env-entry>
  <env-entry-name>myProperty</env-entry-name>
  <env-entry-type>java.lang.String</env-entry-type>
  <env-entry-value>some-value</env-entry-value>
</env-entry>

Accessing the property in codebase as below will return a nullreference:

如下访问代码库中的属性将返回一个null引用:

System.getProperty("myProperty");

Though it should be retrieved with a context lookup as follows:

虽然它应该通过上下文查找来检索,如下所示:

Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");

// Look up for your property "myProperty"
String myPropertyValue = (String) envCtx.lookup("myProperty");

The main advantage is that you will be able to have a property which value will vary from one web application to another. But this way, and as the amount of your deployed artifacts will rise, it will be hard to maintain this and I think, based on my humble experience, it is not advised for production environments.

主要优点是您将能够拥有一个属性,该属性的值会因 Web 应用程序而异。但是这样,随着您部署的工件数量的增加,将很难维持这一点,我认为,根据我的经验,不建议将其用于生产环境。

System Properties

系统属性

If you are targeting a production environment, I advice using system properties (Your code source should then be updated to follow the style). Since you are using Tomcatas an Application Server, you have one way that will let you have centric property definition, that is to declare a file named setenv.sh(.batfor Windows OS) where you export all the needed System Propeties, e.g of content it might have:

如果您的目标是生产环境,我建议使用系统属性(您的代码源应该更新以遵循样式)。由于您Tomcat用作应用程序服务器,因此您有一种方法可以让您拥有中心属性定义,即声明一个名为setenv.sh.bat对于Windows 操作系统)的文件,您可以在其中导出所有需要的系统属性,例如它可能具有的内容:

export CATALINA_OPTS="$CATALINA_OPTS -DmyProperty=some-value"

There is no more additional configuration needed, and this file will be processed by default when launching the JVM instance if present under $CATLINA_HOME/binor $CATLINA_BASE/bin.

不需要更多的额外配置,如果这个文件存在于$CATLINA_HOME/bin$CATLINA_BASE/bin下,那么在启动 JVM 实例时默认会处理这个文件。

回答by Kalpesh Soni

you are confusing web.xml env entry with system environment with java system properties

您将 web.xml env 条目与具有 java 系统属性的系统环境混淆

ask your ee server admin to add those in server definition such as

请您的 ee 服务器管理员在服务器定义中添加这些内容,例如

-Dpropname=true

-Dpropname=true