![](/img/trans.png)
[英]Profile specific application.properties file in Spring boot and default injection of values for multiple profiles
[英]Default profile in Spring Boot is not getting selected in case of multiple profiles
考虑一个简单的例子如下:
我有一个宠物层次结构如下:
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 实现。
DogPetService
?application.properties
中,如果我添加狗/猫,那么它工作正常。spring.profiles.active
,那么这两个默认配置文件都会被使用......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.