@Conditionalonexpression对类对象的getter

时间:2017-06-09 11:14:44

标签: java spring spring-annotations

我想知道是否有办法在运行时通过评估在运行时获取其值的类的对象来使用@conditionalonexpression启用类。

例如:

{{1}}

propertyobject实例在运行时填充值(在程序开头说) 这有可能实现吗?

1 个答案:

答案 0 :(得分:1)

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