spring 如何在 SpringBOOT 中传递 JVM 参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33635176/
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
How to pass JVM arguments in SpringBOOT
提问by SpringForLiving
I would like to pass JVM arguments in the main spring boot class where we will be starting the spring boot application.
我想在主 Spring Boot 类中传递 JVM 参数,我们将在其中启动 Spring Boot 应用程序。
Could you please share how to set JVM arguments in spring boot application?
您能否分享如何在 Spring Boot 应用程序中设置 JVM 参数?
I tried the below options and there is no luck
我尝试了以下选项,但没有运气
System.setProperty("http.proxyHost", "X.X.X.X");
System.setProperty("http.proxyPort", "8080");
or you can use this for spring boot
或者你可以用它来做弹簧靴
bootRun {
systemProperties "property1": "value1", "property2": "value2"
}
采纳答案by SpringForLiving
This option looks promising.
这个选项看起来很有希望。
$ java -jar <jar-name> [--key1="value1"]... [--keyN="valueN"]
回答by Kabir
Add JVM arguments with -DargumentName i.e.
添加带有 -DargumentName 的 JVM 参数,即
-DargumentName="value1"
Then in your spring application, you can retrieve the value by doing:
然后在您的 spring 应用程序中,您可以通过执行以下操作来检索该值:
@Value("${argumentName}")
private String myVariable
Hope this helps!
希望这可以帮助!
回答by Bloodysock
Spring Boot has a pretty sophisticated environment variable and config properties management. You can do something as simple as this to pass foo with value "bar" via command line.
Spring Boot 有一个非常复杂的环境变量和配置属性管理。你可以做一些像这样简单的事情来通过命令行传递带有值“bar”的 foo 。
java -jar app.jar --foo="bar"
If you have not already, the link below has everything you need as far as ways of externalization of configuration in a Spring Boot application:
如果您还没有,下面的链接提供了您在 Spring Boot 应用程序中外部化配置方式所需的一切:
http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html
http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html
Update:
更新:
If you are running a Spring Boot application, you should always run it using the pattern below:
如果您正在运行 Spring Boot 应用程序,则应始终使用以下模式运行它:
$ java -jar <jar-name> [--key1="value1"]... [--keyN="valueN"]
And not using the command line suggested in some of the other comments here.
并且不使用此处其他一些评论中建议的命令行。
回答by Krzysztof Szewczyk
JVM arguments you might add in app.conf
file:
您可能会在app.conf
文件中添加的 JVM 参数:
export JAVA_OPTS='-Dyour_param=1234'
source: SPRING DOCS
资料来源:弹簧文档
回答by Nikita Magonov
You can set system properties with the -D switch when you start your application
您可以在启动应用程序时使用 -D 开关设置系统属性
java -DsomeProperty=123 your.package.name.YourMainClassName
Later in your code you can obtain a property value simply by calling
稍后在您的代码中,您可以通过调用简单地获取属性值
System.getPropetry("someProperty")
回答by maciej
You can run your app like this:
你可以像这样运行你的应用程序:
$ java -jar app.jar --someProperty=123
in application call:
在应用程序调用中:
import org.springframework.core.env.Environment;
@Autowired
private Environment env;
String someProperty= env.getProperty("someProperty");
回答by Sourabh Kanojiya
You can run your app like this:
你可以像这样运行你的应用程序:
$ java -server -Dmyproperty=blabla -jar myapp.jar
and you can access the value of myproperty in code.
并且您可以在代码中访问 myproperty 的值。
回答by reos
You have to use the @Valueannotation. For example
您必须使用@Value注释。例如
@Value("#{systemProperties.test}")
You can use it directly in a bean property, I have an example using in a parameter of a method.
您可以直接在 bean 属性中使用它,我有一个在方法参数中使用的示例。
@SpringBootApplication
public class ReviewsMicroserviceApplication {
public static void main(String[] args) {
ApplicationContext ctx = SpringApplication.run(ReviewsMicroserviceApplication.class, args);
System.out.println(ctx.getBean("value"));
}
@Bean
public String value(@Value("#{systemProperties.test}")String value){
return value;
}
}
In order to execute the program you need to add the testproperty to the JVM properties.
为了执行该程序,您需要将test属性添加到JVM 属性中。
-Dtest="hallo reos"