如何在Spring Boot中使用AllNestedConditions

问题描述:

我试过如下;如何在Spring Boot中使用AllNestedConditions

public class MySwitch extends AllNestedConditions { 

    public MySwitch(ConfigurationPhase configurationPhase) { 
     super(configurationPhase); 
    } 

    @ConditionalOnProperty(name = "EnableSomething") 
    static class OnProperty { 
    } 
} 

,但我得到这样的错误: Failed to Instantiate as no default Constructor Found

这样做的正确方法是什么?

您必须使用超级构造函数来告诉弹簧在哪个阶段应该考虑条件。在你的情况下,它应该看起来像这样:

public class MySwitch extends AllNestedConditions { 

    public MySwitch() { 
     super(ConfigurationPhase.REGISTER_BEAN); 
    } 

    @ConditionalOnProperty(name = "EnableSomething") 
    static class OnProperty { 
    } 
} 
+0

工作!谢谢!! – Raj