Consider a simple example as follows:
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.
- Why doesn't it fall back for the
DogPetService
which is the default profile? - In the
application.properties
if I add dog/cat, then it works fine. - If I completely remove the entry
spring.profiles.active
from the application.properties, then the both the default profiles are utilized... - 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?
CodePudding user response:
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 yourapplication.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 isdefault
and it can be tuned using thespring.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