参数 | 使用方式 | 示例 | 代码获取方式 |
---|---|---|---|
VM Options | 必须以 -D 、 -X 、 -XX 开头,每个参数用空格隔开 ,使用最多的就是 -Dkey=value | -Dvm.key=VmKey -Dvm.key2=VmKey2 | String key = System.getProperty(“vm.key”); |
Program Arguments | 为我们传入main方法的字符串数组args[],用 空格 隔开 它通常以 -- 开头 | –spring.profiles.active=dev3 | main(String[] args) |
Environment Variable | 其优先级低于 VM options ,即如果VM options 有一个变量和 Environment variable中的变量的key相同,则以VM options 中为准, 以 分号 分割多个 | env.key=env_james;server.servlet.context-path=/test;server.port=8080 | String envKey = System.getenv(“env.key”); |
测试
@SpringBootApplication
public class SpringbootDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootDemoApplication.class, args);
System.out.println("***************args***************");
//打印传进来的 program arguments
for (String arg : args) {
System.out.println(arg);
}
System.out.println("***************System.getProperties***************");
//获取java相关的环境变量
String key = System.getProperty("vm.key");
System.out.println("System.getProperties(vm.key):=======>" + key);
Properties properties = System.getProperties();
System.out.println("System.getProperties():=======>" + properties);
System.out.println("***************System.getenv***************");
//获取运行jvm的平台相关环境变量
Map<String, String> env = System.getenv();
System.out.println("System.getenv():----->" + env);
String envKey = System.getenv("env.key");
System.out.println("System.getenv(env.key)=======>" + envKey);
}
}
结果:
***************args***************
p.key=Program_Key
p.name=ProgramName
p.age=18
***************System.getProperties***************
System.getProperties(vm.key):=======>VmKey
System.getProperties():=======>{sun.desktop=windows, awt.toolkit=sun.awt.windows.WToolkit, java.specification.version=11, sun.cpu.isalist=amd64, sun.jnu.encoding=GBK, java.vm.vendor=Amazon.com Inc., sun.arch.data.model=64, user.variant=, java.vendor.url=https://aws.amazon.com/corretto/, catalina.useNaming=false, user.timezone=Asia/Shanghai, os.name=Windows 10, java.vm.specification.version=11, sun.java.launcher=SUN_STANDARD, user.country=CN, spring.application.admin.enabled=true, sun.java.command=com.james.springbootdemo.SpringbootDemoApplication p.key=Program_Key p.name=ProgramName p.age=18, com.sun.management.jmxremote=, jdk.debug=release, sun.cpu.endian=little, spring.liveBeansView.mbeanDomain=, user.home=C:\Users\Andy, user.language=zh, java.specification.vendor=Oracle Corporation, java.version.date=2020-10-20, java.home=D:\jdk\jdk11.0.9_11, file.separator=\, spring.output.ansi.enabled=always, java.vm.compressedOopsMode=Zero based, line.separator=
, java.specification.name=Java Platform API Specification, java.vm.specification.vendor=Oracle Corporation, FILE_LOG_CHARSET=UTF-8, java.awt.graphicsenv=sun.awt.Win32GraphicsEnvironment, java.awt.headless=true, user.script=, sun.management.compiler=HotSpot 64-Bit Tiered Compilers, java.runtime.version=11.0.9+11-LTS, user.name=James, path.separator=;, os.version=10.0, java.runtime.name=OpenJDK Runtime Environment, file.encoding=UTF-8, spring.beaninfo.ignore=true, java.vm.name=OpenJDK 64-Bit Server VM, vm.key=VmKey, java.vendor.version=Corretto-11.0.9.11.2, java.version=11.0.9, os.arch=amd64, java.vm.specification.name=Java Virtual Machine Specification, PID=15712, java.awt.printerjob=sun.awt.windows.WPrinterJob, sun.os.patch.level=, CONSOLE_LOG_CHARSET=UTF-8, java.vm.info=mixed mode, java.vendor=Amazon.com Inc., java.vm.version=11.0.9+11-LTS, java.rmi.server.randomIDs=true, sun.io.unicode.encoding=UnicodeLittle, java.class.version=55.0}
***************System.getenv***************
System.getenv():----->{PROCESSOR_LEVEL=6, SESSIONNAME=Console, ALLUSERSPROFILE=C:\ProgramData, PROCESSOR_ARCHITECTURE=AMD64, CLASSPATH=.;D:\jdk\jdk11.0.9_11\lib;D:\jdk\jdk11.0.9_11\lib\tools.jar, WXDRIVE_START_ARGS=--wxdrive-setting=0 --disable-gpu --disable-software-rasterizer --enable-features=NetworkServiceInProcess, SystemRoot=C:\WINDOWS, NUMBER_OF_PROCESSORS=12}
System.getenv("env.key")=======>env_james