Java 基于属性文件中属性的Spring导入资源-不使用Spring概要文件-条件上下文加载

Java 基于属性文件中属性的Spring导入资源-不使用Spring概要文件-条件上下文加载,java,spring,spring-mvc,Java,Spring,Spring Mvc,我想根据属性文件中属性的值导入资源,有几个上下文文件,spring应该只加载那些值设置为true的文件。对于不同的客户,这些值是不同的 <import if{property} = true resource="classpath*:prod-config.xml"/> 我不能在这里使用spring配置文件。请提出建议。您可以使用Spring 4实现这种行为 例如: @Configuration @Conditional(MyConditionalProd.class) @Im

我想根据属性文件中属性的值导入资源,有几个上下文文件,spring应该只加载那些值设置为true的文件。对于不同的客户,这些值是不同的

<import if{property} = true resource="classpath*:prod-config.xml"/>

我不能在这里使用spring配置文件。请提出建议。

您可以使用Spring 4实现这种行为

例如:

@Configuration
@Conditional(MyConditionalProd.class)
@ImportResource("classpath*:prod-config.xml")
public class MyConditionalProdConfig {}
MyConditionalProd将实现条件逻辑:

public class MyConditionalProd implements ConfigurationCondition{

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

    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
        String property = context.getEnvironment().getProperty("someProperty");
        if("prod".equals(property)) {
            return true;
        }else{
            return false;
        }
    }

}

这真的很有帮助,而且在每个上下文配置文件中,我们可以对不同的包进行上下文组件扫描。