java - 命令行中的 Spring 属性

标签 java spring command-line properties

我有一个启用了 Spring Profile 的应用程序,它有两个配置文件 [cat|dog] 并且在使用 Maven 的 Idea 中运行所需的配置文件很好:

清理编译包exec:java -Dspring.profiles.active=dog

我希望从命令行运行打包的 jar,但似乎找不到设置配置文件的正确语法。我已经尝试了以下方法,但没有一个是正确的:

java -cp myShadedJar-1.0-SO-static.jar org.my.package.Main --spring.profiles.active=dog

java -cp myShadedJar-1.0-SO-static.jar org.my.package.Main -Drun.arguments="--spring.profiles.active=dog"

java -cp myShadedJar-1.0-SO-static.jar org.my.package.Main -Drun.arguments="spring.profiles.active=dog"

java -cp myShadedJar-1.0-SO-static.jar org.my.package.Main -Dspring.profiles.active=dog

从 CLI 运行时传递 Spring 属性的正确方法是什么?

提前致谢, KA.

最佳答案

根据 Spring Docs您可以设置 JVM 属性或环境变量。

java -jar -Dspring.profiles.active=dog myShadedJar-1.0-SO-static.jar

或(*NIX 系统)

SPRING_PROFILES_ACTIVE=dog java -jar myShadedJar-1.0-SO-static.jar

关于java - 命令行中的 Spring 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42526864/

相关文章:

bash - 如何获得递归的完整路径列表,每个文件一行?

java - springfox-swagger-ui : How to exclude routes from global parameter

java - 如何构建包含 vaadin 路由和静态 html 页面的 Spring boot 项目

java - Spring MVC 3.0 : Is String the preferred type to be used for @PathVariable?

command-line - 如何使用 scalax.io.CommandLineParser?

windows-7 - 无需注销即可启用/禁用 ClearType

Java Resultset.getInt() 未正确返回?

java - TabHost/TabWidget View 未在父级中居中

java - 如何在 Spring Integration Java DSL 中创建 MessageSelector 过滤器?

java - 无论我做什么都无法安装 spring-boot-starter-parent