Is it possible to cancel (!) The spring profile collection?

Is it possible to configure a bean so that it is not used by a group of profiles? I can currently do this (I believe):

@Profile("!dev, !qa, !local")

Is there a more accurate notation to achieve this? Suppose I have many profiles. Also, if I have a Mock and a specific implementation of a service (or something else), can I just annotate one of them and assume that the other will be used in all other cases? In other words, this, for example, is necessary:

@Profile("dev, prof1, prof2")
public class MockImp implements MyInterface {...}

@Profile("!dev, !prof1, !prof2") //assume for argument sake that there are many other profiles
public class RealImp implements MyInterface {...}

Can I just annotate one of them and insert an annotation @Primaryin its place on the other?

In essence, I want this:

@Profile("!(dev, prof1, prof2)")

Thanks in advance!

+4
3

@Conditional.

:

public abstract class ProfileCondition extends SpringBootCondition {
    @Override
    public ConditionOutcome getMatchOutcome(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
        if (matchProfiles(conditionContext.getEnvironment())) {
            return ConditionOutcome.match("A local profile has been found.");
        }
        return ConditionOutcome.noMatch("No local profiles found.");
    }

    protected abstract boolean matchProfiles(final Environment environment);
}

public class DevProfileCondition extends ProfileCondition {
   private boolean matchProfiles(final Environment environment) {    
        return Arrays.stream(environment.getActiveProfiles()).anyMatch(prof -> {
            return prof.equals("dev") || prof.equals("prof1")) || prof.equals("prof2"));
        });
    }
}

public class ProdProfileCondition extends ProfileCondition {
   private boolean matchProfiles(final Environment environment) {    
        return Arrays.stream(environment.getActiveProfiles()).anyMatch(prof -> {
            return !prof.equals("dev") && !prof.equals("prof1")) && !prof.equals("prof2"));
        });
    }
}

@Conditional(value = {DevProfileCondition.class})
public class MockImpl implements MyInterface {...}

@Conditional(value = {ProdProfileCondition.class})
public class RealImp implements MyInterface {...}

aproach Springboot.

+4

, , , , beans /mock beans . :

  • beans
  • beans

, . , Spring @Profile OR ( AND, ). Spring , (, , ..), . OR , AND . , , , .

, . . beans, , , @Profile. beans . beans, , @Configuration ( Spring), @Profile, . , , .

:

@Profile("dev")
public class MockImp implements MyInterface {...}

@Profile("prof1")
public class MockImp implements MyInterface {...}

@Profile("prof2")
public class MockImp implements MyInterface {...}

@Profile("the-last-profile") //you should define an additional profile, not rely on excluding as described before
public class RealImp implements MyInterface {...}

, ​​ @Primary beans. 2 beans , @Primary, Spring. @Primary beans, , bean , ( ). , , . .

TL; DR: , . bean @Profile .

+1

RealImp @Profile("Production") ( ), .

@Primary ?

, @Primary RealImp, , , , , bean ( ) @Profile("Production"), @Primary, .

-1

Source: https://habr.com/ru/post/1673809/


All Articles