简体   繁体   English

如果配置文件设置不更改为 application-default.properties,如何忽略 application.properties 值

[英]How to ignore application.properties values if profile set without changing to application-default.properties

I am using spring profiles to load configurations based on the environment and when reading using Autowired Environment object, it seems additional to environment specific values, Spring also pass values from application.properties, the same is mentioned in spring document Profile-specific Properties and also here in this answer , but I cannot change to application-default.properties because of below limitation. I am using spring profiles to load configurations based on the environment and when reading using Autowired Environment object, it seems additional to environment specific values, Spring also pass values from application.properties, the same is mentioned in spring document Profile-specific Properties and also在此答案中,但由于以下限制,我无法更改为 application-default.properties。

The problem/limitation for me is the deployment platform(rules) that I am using, for all environment the environment the identifier will be passed as JVM arguments, but for production it will not be passed, as mentioned below对我来说问题/限制是我正在使用的部署平台(规则),对于所有环境,标识符将作为 JVM arguments 传递,但对于生产它不会传递,如下所述

application-dev.properties --> DEV environment will pass this -Dspring.profiles.active=dev
application-qa.properties --> QA environment will pass this -Dspring.profiles.active=qa
application.properties --> PROD environment won't pass any argument

The option I am having now is to have keys in application.properties with empty values in other environment specific properties file(event not used)and handle in code, other than this is there any other elegant way to handle without change application.properties我现在拥有的选项是在 application.properties 中使用具有空值的键在其他环境特定的属性文件(未使用事件)中并在代码中处理,除此之外还有其他优雅的方法可以在不更改 application.properties 的情况下进行处理

As the linked documentation ( Profile-specific Properties ) says, application.properties is always loaded.正如链接文档( Profile-specific Properties )所说,始终加载application.properties

As it also says, if no active profile has been set (eg using -Dspring.profiles.active ), the default profile is automatically made active.正如它还说的,如果没有设置活动配置文件(例如使用-Dspring.profiles.active ), default配置文件会自动激活。

As the section Adding Active Profiles says, you can have a profile-specific property add active profiles with the spring.profiles.include property.正如添加活动配置文件部分所述,您可以让配置文件特定的属性使用spring.profiles.include属性添加活动配置文件。

This means that you can have the default profile property file activate a prod profile, like this:这意味着您可以让default配置文件属性文件激活prod配置文件,如下所示:

File application-default.properties :文件application-default.properties

spring.profiles.include = prod

If you have that file, then running without -Dspring.profiles.active set will load properties from these files (in order):如果你有那个文件,那么在没有-Dspring.profiles.active设置的情况下运行将从这些文件中加载属性(按顺序):

application-prod.properties
application-default.properties
application.properties

If you run with -Dspring.profiles.active=dev , properties are loaded from these files (in order):如果您使用-Dspring.profiles.active=dev运行,则会从这些文件中加载属性(按顺序):

application-dev.properties
application.properties

By doing that, you now have a well-named profile-specific property file for each of your environments:通过这样做,您现在为每个环境都拥有了一个命名良好的特定于配置文件的属性文件:

application-dev.properties  --> since DEV environment passed -Dspring.profiles.active=dev
application-qa.properties   --> since QA environment passed -Dspring.profiles.active=qa
application-prod.properties --> since PROD environment didn't pass -Dspring.profiles.active

The application.properties file is still loaded, and should contain all non-environment-specific properties, ie all common properties, as well as default values for any global properties that might have environment-specific values (overrides). application.properties文件仍被加载,并且应该包含所有非特定于环境的属性,即所有公共属性,以及可能具有特定于环境的值(覆盖)的任何全局属性的默认值。

The application-<profile>.properties files can add environment-specific properties, and can override global properties that needs a different value than the default specified in the application.properties file. application-<profile>.properties文件可以添加特定于环境的属性,并且可以覆盖需要与application.properties文件中指定的默认值不同的值的全局属性。

暂无
暂无

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

相关问题 如何将 application.properties 与 application-default.properties 合并并包含在 Spring Boot &gt;2.4 的其他测试配置文件中 - How to merge application.properties with application-default.properties and include in other test profiles with Spring Boot >2.4 如何仅加载配置文件特定的属性文件而忽略默认的application.properties? - How to load only profile specific property file and ignore default application.properties? 如何在Spring中使用application.properties设置配置文件? - How to set the Profile using application.properties in Spring? 将 application.properties 中的默认值设置为 @Entity - Set default values from application.properties to an @Entity 如何更改application.properties文件中的配置文件 - How to change profile in application.properties file 如何在未设置application.properties的情况下在控制器中获取上下文路径 - How to get context path in controller without set in application.properties Spring 引导中的配置文件特定 application.properties 文件和多个配置文件的默认值注入 - Profile specific application.properties file in Spring boot and default injection of values for multiple profiles 如何通过 application.properties 注入日期值 - How to inject Date values by application.properties 如何在构造函数中访问application.properties值 - How to access application.properties values in constructor 如何在 Spring 中更改 Application.properties 的值 - how to change values of Application.properties in Spring
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM