xxw1754352621 / java-dev

愿景:提升自我知识容量,扩展自我知识边界
Apache License 2.0
6 stars 2 forks source link

@Conditional(ConditionalOnConsulEnabled.OnConsulEnabledCondition.class) 和 ConfigurationCondition类的使用 #19

Open xxw1754352621 opened 5 years ago

xxw1754352621 commented 5 years ago

不启动consul

spring.cloud.consul.enabled=false

AllNestedConditions 1 matched 1 did not; NestedCondition on ConditionalOnConsulEnabled.OnConsulEnabledCondition.FoundClass @ConditionalOnClass found required class 'com.ecwid.consul.v1.ConsulClient'; NestedCondition on ConditionalOnConsulEnabled.OnConsulEnabledCondition.FoundProperty @ConditionalOnProperty (spring.cloud.consul.enabled) found different value in property 'spring.cloud.consul.enabled' (ConditionalOnConsulEnabled.OnConsulEnabledCondition)

xxw1754352621 commented 5 years ago

ConfigurationCondition 提供更细的颗粒度,控制配置类是否需要加载( offers more fine-grained control)

过滤类的阶段不一样

1.ConfigurationPhase.PARSE_CONFIGURATION(解析配置类的时候)

2.ConfigurationPhase.REGISTER_BEAN(当注册bean的时候,非配置类non {@code @Configuration}) bean)

xxw1754352621 commented 5 years ago

OnConsulEnabledCondition 继承 AllNestedConditions,必须实现OnConsulEnabledCondition 所有的嵌套条件才能成功注入和解析 这个类有两个条件:

@ConditionalOnProperty(value = "spring.cloud.consul.enabled", matchIfMissing = true)
static class FoundProperty {}

@ConditionalOnClass(ConsulClient.class)
static class FoundClass {}

所以上面的提示解析为,找到了ConsulClient客户端,没有匹配配置 参考: https://leokongwq.github.io/2017/05/28/springboot-autoconfiguration.html