简体   繁体   English

Spring boot - 使用配置文件时未读取默认属性文件

[英]Spring boot - Default properties file not being read when using profile

I have a spring boot 2 application with 2 property files:我有一个带有 2 个属性文件的 Spring Boot 2 应用程序:

  • application.properties应用程序属性
  • application-dev.properties应用程序-dev.properties

When I run the application in IntellijIdea using the dev profile, Spring reads both the profile specific file and the default file.当我使用 dev 配置文件在 IntellijIdea 中运行应用程序时,Spring 会读取配置文件特定文件和默认文件。

Once I build an executable jar file, I created a new application-prod.properties outside of the jar, with production environment properties and execute the application with --spring.profiles.active=prod expecting it to read this file AND the default application.properties , but it is only reading the file outside the jar (application-prod.properties).一旦我构建了一个可执行的 jar 文件,我就在 jar 之外创建了一个新的 application-prod.properties,具有生产环境属性,并使用--spring.profiles.active=prod执行应用程序,期望它读取这个文件和默认application.properties ,但它只是读取 jar (application-prod.properties) 外的文件。

Should Spring Boot read both files or am I expecting something that it does not do by default? Spring Boot 应该读取这两个文件,还是我期待它默认不做的事情?

If it should, what can I be missing?如果应该,我会缺少什么?

As mentioned by Michael, Spring boot supports property hierarchies .正如迈克尔所说,Spring boot 支持属性层次结构

I tried this and it works as expected, you can find a reference project in here我试过了,它按预期工作,你可以在这里找到一个参考项目

A few things that I can think about:我能想到的几点:

  • Extract the contents of your jar file and confirm that the file named application.properties is bundled in there.提取 jar 文件的内容并确认名为 application.properties 的文件捆绑在其中。

  • You may be overwriting the spring.config.location property and using filenames instead of directories您可能会覆盖 spring.config.location 属性并使用文件名而不是目录

From Spring Boot documentation来自Spring Boot 文档

If you have specified any files in spring.config.location, profile-specific variants of those files are not considered.如果您在 spring.config.location 中指定了任何文件,则不会考虑这些文件的特定于配置文件的变体。 Use directories in spring.config.location if you want to also use profile-specific properties.如果您还想使用特定于配置文件的属性,请使用 spring.config.location 中的目录。

  • If you override the default spring.config.location then the application may not be able to find the default application.properties file.如果您覆盖默认的 spring.config.location,则应用程序可能无法找到默认的 application.properties 文件。

By default, application.properties will be used to run the Spring Boot application.默认情况下,application.properties 将用于运行 Spring Boot 应用程序。 While running the JAR file, you need to specify the spring active profile.在运行 JAR 文件时,您需要指定 spring 活动配置文件。 only one file will be read只会读取一个文件

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

相关问题 不读取junit spring引导配置文件属性 - junit spring boot profile properties not being read spring 在 application.properties 中使用 spring.profile.default 时启动未加载默认配置文件 - spring boot not loading default profile while using spring.profile.default in application.properties 在Spring Boot中未从外部化属性文件中读取自定义值 - Custom values not being read from externalized properties file in Spring Boot Spring 引导配置文件未选择属性文件 - Spring boot profile not picking the properties file 在 Spring Boot 中使用 spring 配置文件读取多个属性文件 - Read Multiple Properties File using spring profiles in Spring Boot Spring 引导中的配置文件特定 application.properties 文件和多个配置文件的默认值注入 - Profile specific application.properties file in Spring boot and default injection of values for multiple profiles Spring默认配置文件属性 - Spring default profile properties Spring Boot:Spring 总是为属性分配默认值,尽管它存在于 .properties 文件中 - Spring Boot : Spring always assigns default value to property despite of it being present in .properties file Spring Boot - 使用 Constants 类读取自定义属性文件 - Spring Boot - Read custom properties file using Constants class 如何使用Spring Boot使用动态文件读取外部属性 - How to read external properties with dynamic file using Spring Boot
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM