Spring:从命令行覆盖一个application.property [英] Spring: overriding one application.property from command line

查看:59
本文介绍了Spring:从命令行覆盖一个application.property的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有默认变量值的application.properties文件.我希望能够在使用mvn spring-boot:run运行时更改其中之一.我找到了如何更改整个文件的方法,但是我只想更改其中一个或两个属性.

I have an application.properties file with default variable values. I want to be able to change ONE of them upon running with mvn spring-boot:run. I found how to change the whole file, but I only want to change one or two of these properties.

推荐答案

您可以将各个属性作为命令行参数来传递.例如,如果要设置server.port,则在启动可执行jar时可以执行以下操作:

You can pass in individual properties as command-line arguments. For example, if you wanted to set server.port, you could do the following when launching an executable jar:

java -jar your-app.jar --server.port=8081

或者,如果您在Spring Boot 2.x中使用mvn spring-boot:run:

Alternatively, if you're using mvn spring-boot:run with Spring boot 2.x:

mvn spring-boot:run -Dspring-boot.run.arguments="--server.port=8081"

或者,如果您使用的是Spring Boot 1.x:

Or, if you're using Spring Boot 1.x:

mvn spring-boot:run -Drun.arguments="--server.port=8081"

您还可以在应用程序的pom.xml中为spring-boot:run配置参数,因此不必每次都在命令行上指定它们:

You can also configure the arguments for spring-boot:run in your application's pom.xml so they don't have to be specified on the command line every time:

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
        <arguments>
            <argument>--server.port=8085</argument>
        </arguments>
    </configuration>
</plugin>

这篇关于Spring:从命令行覆盖一个application.property的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆