简体   繁体   English

何时使用环境变量与系统属性?

[英]When to use environment variables vs. system properties?

I wonder which of the following is a preferred approach? 我想知道以下哪一种是首选方法?

We can set things up as APP_HOME=/path/to/file ( export in .profile or something along those lines) and access it as System.getenv("APP_HOME") 我们可以设置为APP_HOME=/path/to/file (在.profile export或沿着这些行的东西)并将其作为System.getenv("APP_HOME")

Or, alternatively using properties as -DAPP_HOME=/path/to/file and access it as System.getProperty("APP_HOME") 或者,使用-DAPP_HOME=/path/to/file属性, -DAPP_HOME=/path/to/file其作为System.getProperty("APP_HOME")

Now .. either one will make the value available for the application stand point, but is either approach preferred? 现在..任何一个都可以使值可用于应用程序的立场,但是这两种方法都是首选吗? Why? 为什么? When? 什么时候?

The Javadoc for System.getenv(String) addresses this question directly, saying : System.getenv(String)的Javadoc直接解决了这个问题, 他说

System properties and environment variables are both conceptually mappings between names and values. 系统属性环境变量都是名称和值之间的概念映射。 Both mechanisms can be used to pass user-defined information to a Java process. 这两种机制都可用于将用户定义的信息传递给Java进程。 Environment variables have a more global effect, because they are visible to all descendants of the process which defines them, not just the immediate Java subprocess. 环境变量具有更全局的效果,因为它们对定义它们的进程的所有后代都是可见的,而不仅仅是直接的Java子进程。 They can have subtly different semantics, such as case insensitivity, on different operating systems. 它们可以在不同的操作系统上具有微妙的不同语义,例如不区分大小写。 For these reasons, environment variables are more likely to have unintended side effects. 由于这些原因,环境变量更可能产生意想不到的副作用。 It is best to use system properties where possible. 最好尽可能使用系统属性。 Environment variables should be used when a global effect is desired, or when an external system interface requires an environment variable (such as PATH ). 当需要全局效果时,或者当外部系统接口需要环境变量(例如PATH )时,应使用环境变量。

(emphasis mine). (强调我的)。

If you are using Java 1.3 or 1.4 (and 1.2, IIRC), you should be using system properties, since System.getenv was deprecated. 如果您使用的是Java 1.3或1.4(以及1.2,IIRC),那么您应该使用系统属性,因为不推荐使用System.getenv It was reinstated in Java 1.5. 它在Java 1.5中恢复。 The relevant bug report can be found here . 相关的错误报告可以在这里找到。

You can use both. 你可以使用两者。 Search system properties for the key, and if it's not there, search the environment. 搜索密钥的系统属性,如果不存在,则搜索环境。 This gives you the best of both worlds. 这为您提供了两全其美的体验。

These really aren't the same thing: One requires the value to be set explicitly, and the other not. 这些确实不是一回事:一个需要明确设置值,另一个不需要。 Also, note that the environment is a convenient place to put some strings for interoperability. 另外,请注意环境是一个方便的地方,可以放置一些字符串以实现互操作性。

Can't comment yet at the moment, so I will add a couple of points as an answer. 暂时无法发表评论,所以我会补充几点作为答案。

I agree with the javadoc's saying "It is best to use system properties where possible.", also in my own words previous to seeing this page here that the Java system variables are encapsulated inside the JVM. 我同意javadoc的说法“最好在可能的情况下使用系统属性。”,在我看到此页面之前,我自己的说法是将Java系统变量封装在JVM中。 They are not visible to other processes on the host, and thus less coupled with the host system. 它们对主机上的其他进程不可见,因此与主机系统的耦合较少。

In addition, there are multiple interfaces to set the global environment variables, and so it could be a bit tricky to track all the values being used over time. 此外,有多个接口可以设置全局环境变量,因此跟踪一段时间内使用的所有值可能有点棘手。

One important difference between using Environment Variables (envs) and System properties which should be considered is that the envs could not be changed at runtime/in the running process, BUT System properties could be. 使用环境变量(envs)和应考虑的系统属性之间的一个重要区别是envs无法在运行时/运行过程中更改,但系统属性可能是。 Refer to the Javadoc: 请参阅Javadoc:

https://docs.oracle.com/javase/8/docs/api/java/lang/System.html#setProperties-java.util.Properties- https://docs.oracle.com/javase/8/docs/api/java/lang/System.html#setProperties-java.util.Properties-

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM