简体   繁体   English

使用外部属性覆盖 spring-boot application.properties

[英]Override spring-boot application.properties with external properties

I have a spring-boot application.我有一个 spring-boot 应用程序。 I have 3 properties file:我有 3 个属性文件:

  1. a property file inside the spring-boot jar - myjar.jar called application.properties ( which is package inside the jar ) spring-boot jar 中的一个属性文件 - myjar.jar称为 application.properties (这是 jar 内的包

  2. a property file outside the jar, at the jar location under configurations/global.properties jar 外部的属性文件,位于configuration/global.properties下的jar 位置

  3. a property file outside the jar, at the jar location under configurations/java.properties jar 外部的一个属性文件,位于configuration/java.properties下的jar 位置

The problem is, I'm running the following command:问题是,我正在运行以下命令:

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

And I get an exception:我得到一个例外:

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

in myApplication.java, I have:myApplication.java 中,我有:

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

and in my application.properties I do not have Data.StoresDb.LakeConnectionString, but I do have it in my global.properties, I would expect spring to resolve all files before trying to ser the value Data.StoresDb.LakeConnectionString在我application.properties没有Data.StoresDb.LakeConnectionString,但有它在我的global.properties,我希望春天试图SER值Data.StoresDb.LakeConnectionString之前解决所有文件

You pass the arg/value as a raw Java argument :您将 arg/value 作为原始 Java 参数传递:

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

The argument will be present in the String[] args of the main class but the Spring environment will not be aware about that.该参数将出现在主类的String[] args中,但 Spring 环境不会意识到这一点。
You have to prefix it with -- to make the argument to be Spring environment aware :您必须在它前面加上--以使参数成为 Spring 环境感知:

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

From the official documentation :官方文档

By default SpringApplication will convert any command line option arguments (starting with “--”, eg --server.port=9000) to a property and add it to the Spring Environment默认情况下,SpringApplication 会将任何命令行选项参数(以“--”开头,例如 --server.port=9000)转换为属性并将其添加到 Spring Environment

Or as alternative pass it as a system property :或者作为替代将其作为系统属性传递:

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

As a side note : beware spring.config.location overrides the default locations while spring.config.additional-location introduced in Spring Boot 2 adds the specified locations to the default locations.作为旁注:注意spring.config.location会覆盖默认位置,而 Spring Boot 2 中引入的spring.config.additional-location会将指定位置添加到默认位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 覆盖application.properties以在Spring-boot应用程序中进行集成测试 - Override application.properties for integration tests in spring-boot app 如何在 Spring-Boot 的生产过程中覆盖 application.properties? - How to override application.properties during production in Spring-Boot? 用application.properties覆盖默认的Spring-Boot application.properties - Override default Spring-Boot application.properties with application.properties 使用动态值覆盖 Junit 测试中的默认 Spring-Boot application.properties 设置 - Override default Spring-Boot application.properties settings in Junit Test with dynamic value spring-boot如何添加多个application.properties文件? - How to add multiple application.properties files in spring-boot? spring-boot application.properties中的环境变量错误 - Environment Variables in spring-boot application.properties error Spring-Boot>使用JNDI设置application.properties - Spring-Boot > Setting application.properties using JNDI Spring-Boot中的application.properties属性的UTF-8编码 - UTF-8 encoding of application.properties attributes in Spring-Boot Spring-boot 1.4中的新@DataJpaTest以及与application.properties的关系 - New @DataJpaTest in spring-boot 1.4 and relation with application.properties 如何在Spring Boot中的application.properties中指定外部属性文件? - How to specify external properties files in application.properties in Spring Boot?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM