Java spring有条件加载xml资源

Java spring有条件加载xml资源,java,xml,spring,spring-mvc,Java,Xml,Spring,Spring Mvc,我正在尝试根据属性是否存在动态加载xml配置文件。 我使用spring@Conditional检查属性是否存在 @Configuration @Conditional(CheckPropertyConfiguration.Condition1.class) @ImportResource("webapp/WEB-INF/myContext.xml") public class CheckPropertyConfiguration { public void test(){

我正在尝试根据属性是否存在动态加载xml配置文件。 我使用spring@Conditional检查属性是否存在

@Configuration
@Conditional(CheckPropertyConfiguration.Condition1.class)
@ImportResource("webapp/WEB-INF/myContext.xml")
public class CheckPropertyConfiguration {

    public void test(){
        System.out.println("init .............test");
    }
    static class Condition1 implements ConfigurationCondition {
        @Override
        public ConfigurationPhase getConfigurationPhase() {
            System.out.println("getConfigurationPhase.............");
            System.out.println(System.getProperty("api.key"));
            return ConfigurationPhase.PARSE_CONFIGURATION;
        }
        @Override
        public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
            System.out.println("checking property exist or not: " + System.getProperty("api.key"));
            return System.getProperty("api.key") != null;
        }
    }
}
这个bean在我的applicationContext.xml文件中配置

<bean class="com.cm.service.impl.CheckPropertyConfiguration" init-method="test" />
当应用程序启动时,sysout in test正在运行,并在控制台上显示输出。 但是由于某些原因,spring框架没有调用getConfigurationPhase和matches方法来检查属性是否存在

@Configuration
@Conditional(CheckPropertyConfiguration.Condition1.class)
@ImportResource("webapp/WEB-INF/myContext.xml")
public class CheckPropertyConfiguration {

    public void test(){
        System.out.println("init .............test");
    }
    static class Condition1 implements ConfigurationCondition {
        @Override
        public ConfigurationPhase getConfigurationPhase() {
            System.out.println("getConfigurationPhase.............");
            System.out.println(System.getProperty("api.key"));
            return ConfigurationPhase.PARSE_CONFIGURATION;
        }
        @Override
        public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
            System.out.println("checking property exist or not: " + System.getProperty("api.key"));
            return System.getProperty("api.key") != null;
        }
    }
}

请帮助动态加载我的配置文件。

为什么混合使用xml和java配置?您的xml配置中有或吗?如果没有,则将忽略批注。您是正确的。我被错过了。添加此标记后,它正在工作。如果不使用注释方法,我们可以动态加载xml资源吗?如果可能的话,请给出建议?我不确定条件是否在XML中起作用,我想不会。但是为什么不放弃xml而改用java呢。