如何在SpringBOOT中传递JVM参数

16

我想在启动Spring Boot应用程序的主类中传递JVM参数。

请问如何在Spring Boot应用程序中设置JVM参数?

我尝试了以下选项,但没有成功。

System.setProperty("http.proxyHost", "X.X.X.X");
System.setProperty("http.proxyPort", "8080");

或者您可以在Spring Boot中使用此方法。

bootRun {
    systemProperties "property1": "value1", "property2": "value2"
}
11个回答

35

使用 -DargumentName 添加JVM参数,例如:

-DargumentName="value1"

然后在您的Spring应用程序中,您可以通过执行以下操作检索该值:

@Value("${argumentName}")
private String myVariable

希望这能帮到你!


如何在Eclipse中添加它? - Thirumal

17

Spring Boot拥有相当精密的环境变量和配置属性管理。您可以通过命令行执行像这样简单的操作,以传递值为“bar”的foo。

java -jar app.jar --foo="bar"
如果您还没有,下面的链接包含了在Spring Boot应用程序中进行配置外部化的所有必要内容: http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html 更新:
如果您正在运行Spring Boot应用程序,您应该始终使用以下模式运行它:
$ java -jar <jar-name> [--key1="value1"]... [--keyN="valueN"]

不使用其他评论中建议的命令行方式。


7

你可能需要在app.conf文件中添加JVM参数:

export JAVA_OPTS='-Dyour_param=1234'

source: SPRING DOCS


5
您可以在启动应用程序时使用-D开关设置系统属性。
java -DsomeProperty=123 your.package.name.YourMainClassName

在您的代码中,您只需调用相应的属性即可获取该属性的值。

System.getPropetry("someProperty")

5

您可以这样运行您的应用:

$ java -server -Dmyproperty=blabla -jar myapp.jar

你可以在代码中访问myproperty的值。


1
“-D” 是什么意思? - pheromix
4
注意:在-jar myapp.jar之后提供的选项将不会被考虑。 - Rémi.B

4
你可以按照以下方式运行你的应用程序:
$ java -jar app.jar --someProperty=123

在应用程序中调用:

import org.springframework.core.env.Environment;
@Autowired
private Environment env;

String someProperty= env.getProperty("someProperty");

从安全角度来看,这是否安全? - ion

3

您需要使用@Value注解。例如:

@Value("#{systemProperties.test}")

您可以直接在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;
    }
}

为了执行程序,您需要将测试属性添加到JVM属性中。
 -Dtest="hallo reos"

2
当我使用Maven插件运行Spring Boot应用程序时,我必须使用-Dspring-boot.run.jvmArguemtns=xxx设置属性,但这实际上并没有设置系统属性,而是设置了Spring属性(在application.xml中定义)。
以下是启动带有属性的Spring Boot应用程序的命令:
mvn spring-boot:run -D"spring-boot.run.jvmArguments=-Dserver.port=8399 -Dactivemq.url=http://localhost:61616"

0

enter image description here

package com.example.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

@SpringBootApplication
public class DemoApplication {

    @Autowired
    MyFantasticClass myFantasticClass;
    
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
    
    @Bean
    CommandLineRunner commandLineRunner() {
        return args -> {
            System.out.println(myFantasticClass.getMyFantasticProperty());
        };
    }
}

@Component
class MyFantasticClass{
    @Value("${myFantasticProperty}")
    String myFantasticProperty;
    
    String getMyFantasticProperty() {
        return myFantasticProperty;
    }
}

0

我正在阅读this的答案,解释了application.properties,然后我开始使用application.properties来设置与我的环境相关的变量。

bootstrap.properties:

slack.channel=test

bootstrap.yml:

slack: 
  channel: ${slack.channel}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接