简体   繁体   中英

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

Consider a simple example as follows:

  1. I have a hierarchy of Pets as follows:

     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 {... }

As seen in the example, the dog profile is the default .

Now, I have another hierarchy as follows (in the same project). Note: This is something I am trying to learn and have no relevance to real world project.

   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 { ... }

As seen, for the GreetingService - I18NEnglishGreetingService is the default Profile

Now, I have application.properties in which I am setting the active profiles as follows:

spring.profiles.active=ES

When I run the application, spring boot fails to start as it fails to find bean implementation for the PetService.

  1. Why doesn't it fall back for the DogPetService which is the default profile?
  2. In the application.properties if I add dog/cat, then it works fine.
  3. If I completely remove the entry spring.profiles.active from the application.properties, then the both the default profiles are utilized...
  4. If the entry spring.profiles.active is added, then is it mandatory to list all the required profiles? Why can't it detect that for some profiles, default is to be used?

Is there any workaround for this?

From the reference guide

You can use a spring.profiles.active Environment property to specify which profiles are active. You can specify the property in any of the ways described earlier in this chapter. For example, you could include it in your application.properties , as shown in the following example:

spring.profiles.active=dev,hsqldb

You could also specify it on the command line by using the following switch: --spring.profiles.active=dev,hsqldb . If no profile is active, a default profile is enabled. The name of the default profile is default and it can be tuned using the spring.profiles.default Environment property, as shown in the following example:

spring.profiles.default=none

You yourself specify which profiles are active, this isn't additive but it replaces it. So if you specify none then the default profile, by default named default (as shown above) is active, else only the profile(s) as specified by you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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