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
我需要为我正在处理的RMI应用程序设置codebase,并使用第一个成功完成此操作
try{ ResourceBundle config = ResourceBundle.getBundle(myApp”); String codeBaseUrl = config.getString(codeBaseUrl”); System.setProperty(java.rmi.server.codebase”,codeBaseUrl); } catch(Excetion e){ e.printStackTrace(); }
java -Djava.rmi.server.codebase = http://192.168.1.1/path/to/codebase ...
这两种方法都允许在不需要的情况下更改codebase重新编译,但System.setProperty方法允许代码库被包含到属性文件和使用相同的启动命令。
大多数教程/文档我'读到这个使用-D方法让我相信这被认为是最佳实践,但我一直无法找到任何解释为什么我应该使用另一个。
-D被认为是设置系统属性(如codebase)的最佳实践,它会带来什么好处/它会避免哪些陷阱?
解决方案
比较两者:
-D 可配置 - 它是在runtime指定的
资源包通过 System.setProperty()仍然是runtime,但它是通过一个超出启动命令的文件进行编辑的。
首先,通过在运行时指定设置来驱动灵活行为总是比硬编码行为更好(除非行为实际上不灵活 - 即除非有这样的值,否则不要使用配置) 。
使用文件的主要缺点是它可能包含敏感数据,如密码,系统管理员不希望永久存放在磁盘上。如果密码是作为启动命令的一部分输入的,那么它是短暂的并且更加安全(会话命令历史记录不能承受)。
使用文件的好处是你可以建立正确的设置,他们留在文件中。您甚至可以通过源代码管理来管理文件。
还有另一个更安全的选项,即启动时要求输入密码在命令行输入,不留痕迹。
http://www.it1352.com/987063.html
org.springframework.boot.SpringApplication#configureHeadlessProperty
private void configureHeadlessProperty() { System.setProperty(SYSTEM_PROPERTY_JAVA_AWT_HEADLESS, System.getProperty( SYSTEM_PROPERTY_JAVA_AWT_HEADLESS, Boolean.toString(this.headless))); }