@Conditionalonexpression on a class object getter

后端 未结 1 1018
情书的邮戳
情书的邮戳 2021-01-24 01:58

I was wondering if there is a way to enable a class using @conditionalonexpression during runtime by evaluating an object of a class which gets its value at runtime.

FOr

相关标签:
1条回答
  • 2021-01-24 02:52

    Yes, it is possible but getexenabled() method should be static.

    Like:

    @SpringBootApplication
    public class So44456388Application {
    
        public static void main(String[] args) {
            SpringApplication.run(So44456388Application.class, args);
        }
    
        public static class Evaluator {
            public static boolean getexenabled() {
                //your logic here
                return true /*false*/;
            }
        }
    
        @Component
        @ConditionalOnExpression("#{T(com.stackoverflow.so44456388.So44456388Application$Evaluator).getexenabled()}")
        public static class RichBean {
            @PostConstruct
            private void init() {
                System.out.println("RichBean");
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题