繁体   English   中英

在有多个配置文件的情况下,未选择 Spring Boot 中的默认配置文件

[英]Default profile in Spring Boot is not getting selected in case of multiple profiles

考虑一个简单的例子如下:

  1. 我有一个宠物层次结构如下:

     public interface PetService{.... } // Now this service is implemented by DogPetService and CatPerService as follows: @Service @Profile("cat") public class CatPetService implements PetService {... } @Profile({"dog", "default"}) @Service public class DogPetService implements PetService {... }

如示例中所示, dog profile默认的。

现在,我有另一个层次结构如下(在同一个项目中)。 注意:这是我正在尝试学习的东西,与现实世界的项目无关。

   public interface GreetingService { ... }
   // The Greeting service is implemented by two classes as follows:
   
   @Profile({"EN", "default"} )
   @Service
   public class I18NEnglishGreetingService implements GreetingService { ... }
   
   @Profile("ES")
   @Service
   public class I18NSpanishGreetingService implements GreetingService { ... }

如图所示,对于GreetingService - I18NEnglishGreetingService是默认配置文件

现在,我有application.properties ,我在其中设置活动配置文件如下:

spring.profiles.active=ES

当我运行该应用程序时,spring 引导无法启动,因为它无法找到 PetService 的 bean 实现。

  1. 为什么它不返回默认配置文件DogPetService
  2. application.properties中,如果我添加狗/猫,那么它工作正常。
  3. 如果我从 application.properties 中完全删除条目spring.profiles.active ,那么这两个默认配置文件都会被使用......
  4. 如果添加条目spring.profiles.active ,那么是否必须列出所有必需的配置文件? 为什么它不能检测到某些配置文件使用默认值?

有什么解决方法吗?

来自参考指南

您可以使用spring.profiles.active Environment属性来指定哪些配置文件处于活动状态。 您可以使用本章前面描述的任何方式指定属性。 例如,您可以将其包含在您的application.properties中,如以下示例所示:

spring.profiles.active=dev,hsqldb

您还可以使用以下开关在命令行上指定它: --spring.profiles.active=dev,hsqldb 如果没有配置文件处于活动状态,则会启用默认配置文件。 默认配置文件的名称是default ,可以使用spring.profiles.default Environment属性对其进行调整,如以下示例所示:

spring.profiles.default=none

您自己指定哪些配置文件处于活动状态,这不是附加的,而是替代它。 因此,如果您不指定,则默认配置文件(默认名为default (如上所示))处于活动状态,否则只有您指定的配置文件

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM