java 将系统属性传递给 spring boot

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34297666/
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-11-02 22:43:09  来源:igfitidea点击:

Pass system property to spring boot

javaspringspring-boot

提问by zibi

A library I need to use reads system property in the following way:

我需要使用的库通过以下方式读取系统属性:

System.getProperty("library.system.property")

Is there any way to pass such a property to spring boot while starting the app or do I need to set it in the system?

有什么方法可以在启动应用程序时将这样的属性传递给 spring boot 还是我需要在系统中设置它?

回答by nedenom

You can pass it on the command line:

您可以在命令行上传递它:

java -Dlibrary.system.property=value -jar myapp.jar 

回答by fetta

you can also do it like this:

你也可以这样做:

public static void main(String[] args) {
    System.setProperty("key", "value");
    SpringApplication.run(MyApplication.class);
}

回答by Surasin Tancharoen

Update 2020-01-08

更新 2020-01-08

For spring-boot 2.2.2.RELEASEwhile develop

对于 spring-boot 2.2.2.RELEASE开发时

mvn spring-boot:run -Dspring-boot.run.jvmArguments="-Dmy_-Dmy_system_properties=test1"

For spring-boot 1.5.x.RELEASEor below while develop

对于 spring-boot 1.5.x.RELEASE或以下版本,同时开发

mvn spring-boot:run -Drun.jvmArguments="-Dmy_system_properties=test1"

For run as jar

对于作为jar运行

java -Dmy_system_properties=test1 -jar service.jar 

You can try with a runnable example, here https://www.surasint.com/spring-boot-pass-system-properties-in-command-line/

您可以尝试一个可运行的示例,这里是https://www.surasint.com/spring-boot-pass-system-properties-in-command-line/

回答by Praveen

If you are using spring-boot maven plugin to execute the application, try (no line breaks)

如果您使用 spring-boot maven 插件来执行应用程序,请尝试(无换行符)

mvn spring-boot:run -Drun.jvmArguments="-Xms2048m -Xmx4000m -XX:MaxPermSize=2048m 
-Dlibrary.system.property=value"

Refer maven pluginfor additional details

有关其他详细信息,请参阅maven 插件