设置java系统属性的最佳实践是什么,-D或System.setProperty()? [英] What is best practice for setting java system properties, -D or System.setProperty()?

查看:2524
本文介绍了设置java系统属性的最佳实践是什么,-D或System.setProperty()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要为我正在处理的RMI应用程序设置代码库,并使用第一个成功完成此操作

 尝试{
ResourceBundle config = ResourceBundle.getBundle(myApp);
String codeBaseUrl = config.getString(codeBaseUrl);
System.setProperty(java.rmi.server.codebase,codeBaseUrl);
} catch(例外e){
e.printStackTrace();
}

以及稍后使用

  java -Djava.rmi.server.codebase = http://192.168.1.1/path/to/codebase ... 




这两种方法都允许在不需要的情况下更改代码库重新编译,但System.setProperty方法允许代码库被包含到属性文件和使用相同的启动命令。



大多数教程/文档我'读到这个使用-D方法让我相信这被认为是最佳实践,但我一直无法找到任何解释为什么我应该使用另一个。



-D被认为是设置系统属性(如代码库)的最佳实践,它会带来什么好处/它会避免哪些陷阱?

解决方案

(编辑 - 我误读了这个问题)



比较两者:




  • -D 可配置 - 它是在运行时指定的

  • 资源包通过 System.setProperty()仍然是运行时,但它是通过一个超出启动命令的文件进行编辑的。



首先,通过在运行时指定设置来驱动灵活行为总是比硬编码行为更好(除非行为实际上不灵活 - 即除非有这样的值,否则不要使用配置) 。



使用文件的主要缺点是它可能包含敏感数据,如密码,系统管理员不希望永久存放在磁盘上。如果密码是作为启动命令的一部分输入的,那么它是短暂的并且更加安全(会话命令历史记录不能承受)。



使用文件的好处是你可以建立正确的设置,他们留在文件中。您甚至可以通过源代码管理来管理文件。





还有另一个更安全的选项,即启动时要求输入密码在命令行输入,不留痕迹。


I need to set the codebase for the RMI application I'm working on at the moment and have done this successfully using first

try{
  ResourceBundle config = ResourceBundle.getBundle("myApp");
  String codeBaseUrl = config.getString("codeBaseUrl");
  System.setProperty("java.rmi.server.codebase", codeBaseUrl);
} catch (Exception e) {
  e.printStackTrace();
}

and later using

java -Djava.rmi.server.codebase=http://192.168.1.1/path/to/codebase ...

on the command line.

Both of these approaches allow for the codebase to be changed without the need to recompile, but the System.setProperty approach allows codebase to be bunlded into a properties file and the same launch command to be used.

Most of the tutorials/documentation I've read on this uses the -D approach leading me to believe this is accepted as best practice, but I've been unable to find anything that explains WHY I should use over the other.

Is -D considered best practice for setting system properties such as codebase, and what benefits does this give / what pitfalls does it avoid?

解决方案

(Edited - I mis-read the question)

Comparing the two:

  • -D is configurable - it's specified at runtime
  • A resource bundle via System.setProperty() is still "runtime", but it's edited via a file which lives beyond the start-up command

Firstly, driving flexible behaviour by specifying settings at runtime is always preferable to hard-coding behaviour (unless the behaviour isn't actually flexible - ie don't use configuration unless there is value in doing so).

The main downside to using a file is that it may contain sensitive data, like passwords, that sysadmins don't want permanently lying around on disk. If the password is entered as part of the start-up command, it's ephemeral and much more secure (session command history not withstanding).

The upside to using a file is that you can establish the correct settings and they stay in the file. You can even manage the file via source control.


There's another even safer option, which is the have the start-up ask for passwords to be entered on the command line, which leaves no trace.

这篇关于设置java系统属性的最佳实践是什么,-D或System.setProperty()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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