【问题标题】:Override spring-boot application.properties with external properties用外部属性覆盖 spring-boot application.properties
【发布时间】:2020-06-22 04:50:59
【问题描述】:

我有一个 spring-boot 应用程序。 我有 3 个属性文件:

  1. spring-boot jar 中的一个属性文件 - myjar.jar 名为 application.properties(这是 jar 中的包

  2. jar 外部的属性文件,位于 configurations/global.properties 下的 jar 位置

  3. jar 外部的属性文件,位于 configurations/java.properties 下的 jar 位置

问题是,我正在运行以下命令:

java -Dlogging.config=logback.xml -jar "myjar.jar" spring.config.location=classpath:/application.properties,file:./configurations/global.properties,file:./configurations/java.properties

我得到一个例外:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myApplication': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'Data.StoresDb.LakeConnectionString' in value "${Data.StoresDb.LakeConnectionString}"
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:405)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1422)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:594)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517)
        at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323)
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321)
        at org.springframework.beans.fa

myApplication.java, 我有:

@Value("${Data.StoresDb.LakeConnectionString}")
String dataLakeStoreDb;

在我的 application.properties没有有 Data.StoresDb.LakeConnectionString,但我在我的 global.properties ,我希望 spring 在尝试提供值 Data.StoresDb.LakeConnectionString 之前解析所有文件

【问题讨论】:

    标签: spring spring-boot application.properties


    【解决方案1】:

    您将 arg/值作为原始 Java 参数传递:

    java -jar "myjar.jar" spring.config.location=...
    

    参数将出现在主类的 String[] args 中,但 Spring 环境不会意识到这一点。
    您必须在它前面加上 -- 以使参数能够感知 Spring 环境:

    java -jar "myjar.jar" --spring.config.location=...
    

    来自official documentation

    默认情况下 SpringApplication 将转换任何命令行选项 属性的参数(以“--”开头,例如 --server.port=9000) 并将其添加到 Spring 环境中

    或者作为替代将其作为系统属性传递:

    java -Dspring.config.location=... -jar "myjar.jar" 
    

    附带说明:注意 spring.config.location 会覆盖默认位置,而 Spring Boot 2 中引入的 spring.config.additional-location 会将指定位置添加到默认位置。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-05-17
      • 1970-01-01
      • 2018-04-12
      • 2020-11-09
      • 1970-01-01
      • 2017-01-24
      • 1970-01-01
      相关资源
      最近更新 更多