Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/390.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 通过命令行参数设置spring启动属性_Java_Spring Boot_Properties File - Fatal编程技术网

Java 通过命令行参数设置spring启动属性

Java 通过命令行参数设置spring启动属性,java,spring-boot,properties-file,Java,Spring Boot,Properties File,我有两个属性文件: application.yml bootstrap.yml 我为我的应用程序嵌入了jar文件,并使用以下命令启动它:java-jar'jar file path' 我需要用外部位置的文件替代属性文件。 已尝试:-Dspring.config.location=您的/config/dir/参数,但它不起作用 您能告诉我如何用几个属性文件覆盖属性的正确方法吗? 我的bootstrap.yml看起来像: spring: application.name: app_name

我有两个属性文件:

  • application.yml
  • bootstrap.yml
我为我的应用程序嵌入了jar文件,并使用以下命令启动它:
java-jar'jar file path'
我需要用外部位置的文件替代属性文件。
已尝试:
-Dspring.config.location=您的/config/dir/
参数,但它不起作用

您能告诉我如何用几个属性文件覆盖属性的正确方法吗?

我的bootstrap.yml看起来像:

spring:
  application.name: app_name
  profiles:
    active: local
    include:
  cloud.consul.enabled: false
  main:
    web-application-type: none

---
spring:
  profiles: withconsul
  cloud:
    config.allow-override: true
    consul:
      enabled: true
      host: https://consul.evoil.ru
      port: 443
      config:
        enabled: true
        format: YAML
        fail-fast: true

我是否应该重命名有关现有配置文件的属性文件?

除了spring.config.location之外,您还需要使用spring.config.name

java -jar 'jar file path' --spring.config.name=external-prop-file-name --spring.config.location=your/config/dir/

更新:

如果要应用两个外部文件的属性(但bootstrap.yml中的属性优先于application.yml中的属性)然后,您需要将bootstrap.yml文件重命名为application bootstrap.yml,并使用bootstrap配置文件启动应用程序:

java -jar 'jar file path' --spring.profiles.active=bootstrap --spring.config.location=your/config/dir/
在本例中,我省略了--spring.config.name参数,因为它的默认值是应用程序()

更新2:


如果application.yml文件中已激活WithConsour配置文件,并且您不想添加新配置文件,则可以将bootstrap.yml文件重命名为application WithConsour.yml

谢谢,对于2个文件,我应该如何在--spring.config.name=external prop file name中写入参数?我用一个示例更新了答案,以了解您的问题。我已经更新了问题,请检查我是否正确理解您