Disable Spring Configuration if one of several profiles is present

There is a simple (undocumented, but I think officially supported) way of doing this:

@Profile("!moduleTest & !featureTest")

By default Spring is using logical OR, this forces it to logical AND.


Try using a condition:

@Configuration
@Conditional(LoadIfNotModuleNorTestProfileCondition.class)
public class UserConfig {
}

LoadIfNotModuleNorTestProfileCondition class:

public class LoadIfNotModuleNorTestProfileCondition implements ConfigurationCondition{

    @Override
    public ConfigurationPhase getConfigurationPhase() {
        return ConfigurationPhase.PARSE_CONFIGURATION;
    }

    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
        String[] activeProfiles = context.getEnvironment().getActiveProfiles();
        for (String profile : activeProfiles) {
            if(profile.equalsIgnoreCase("moduleTest") || profile.equalsIgnoreCase("featureTest")){
                return false;
            }
        }
        return true;
    }

}