Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如果存在多个配置文件之一,则禁用Spring配置_Spring_Configuration_Profile - Fatal编程技术网

如果存在多个配置文件之一,则禁用Spring配置

如果存在多个配置文件之一,则禁用Spring配置,spring,configuration,profile,Spring,Configuration,Profile,如预期,如果moduleTest是活动配置文件,则不会加载此配置: @Profile({"!moduleTest"}) @Configuration public class UserConfig { } 但是,此配置将加载: 如果moduleTest或featureTest处于活动状态,是否有办法使配置不会加载?尝试使用以下条件: @Configuration @Conditional(LoadIfNotModuleNorTestProfileCondition.class) public

如预期,如果
moduleTest
是活动配置文件,则不会加载此配置:

@Profile({"!moduleTest"})
@Configuration
public class UserConfig {
}
但是,此配置将加载:

如果
moduleTest
featureTest
处于活动状态,是否有办法使配置不会加载?

尝试使用以下条件:

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

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;
    }

}
有一种简单的方法(无文件记录,但我认为官方支持):

@Profile("!moduleTest & !featureTest")

默认情况下,Spring使用逻辑OR,这将强制它使用逻辑AND。

@Configuration
是Springbean,因此请尝试使用
@Conditional
。你需要一双弹簧靴。很好的发现!我明白了:一个概要文件表达式允许表达更复杂的概要文件逻辑,例如“p1&p2”。javadoc中有更多的信息。啊,你是对的,它被提到了,但很容易被忽视。他们应该为这个场景添加一个例子。只需添加一些上下文:在提出这个问题将近两年后,这一变化于年引入:)
@Profile("!moduleTest & !featureTest")