javascript
《SpringBoot系列十六》条件装配时ConfigurationCondition和Condition有什么区别?什么时候用ConfigurationCondition?
一、前言
針對條件裝配我們討論了如下內容:
在《SpringBoot系列十三》:圖文精講@Conditional條件裝配實現原理一文中,我們知道了條件裝配時是分兩階段(配置類解析、Bean注冊)進行的。
二、ConfigurationCondition
ConfigurationCondition接口是Spring4.0提供的注解,位于org.springframework.context.annotation包內,繼承自Condition接口;
public interface ConfigurationCondition extends Condition {/*** 返回當前Condition可以被評估的配置階段* Return the {@link ConfigurationPhase} in which the condition should be evaluated.*/ConfigurationPhase getConfigurationPhase();/*** Condition應該被評估的各個配置階段* The various configuration phases where the condition could be evaluated.*/enum ConfigurationPhase {/*** 配置類解析階段* The {@link Condition} should be evaluated as a {@code @Configuration}* class is being parsed.* <p>If the condition does not match at this point, the {@code @Configuration}* class will not be added.*/PARSE_CONFIGURATION,/*** Bean注冊階段* The {@link Condition} should be evaluated when adding a regular* (non {@code @Configuration}) bean. The condition will not prevent* {@code @Configuration} classes from being added.* <p>At the time that the condition is evaluated, all {@code @Configuration}* classes will have been parsed.*/REGISTER_BEAN}}ConfigurationCondition中的getConfigurationPhase()方法,用于返回ConfigurationPhase配置階段(ConfigurationPhase 的枚舉);
1、ConfigurationCondition和Condition的區別?
1> Condition評估的時機:
2> 使用對比:
1)OnBeanCondition
1> OnBeanCondition類圖:
2> OnBeanCondition實現的方法:
2)OnClassCondition
1> OnBeanCondition類圖:
2、什么時候用ConfigurationCondition?
ConfigurationPhase的作用是控制條件評估(過濾)的時機:是在解析配置類的時候 還是在創建Bean的時候。
一般而言ConfigurationCondition多用于 只在注冊Bean階段才進行條件評估的Condition中使用,比如OnBeanCondition。以OnBeanCondition為例,其中的很多條件評估的依據只有在注冊Bean階段才會相對更加完整。
總結
以上是生活随笔為你收集整理的《SpringBoot系列十六》条件装配时ConfigurationCondition和Condition有什么区别?什么时候用ConfigurationCondition?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2022资料员-通用基础(资料员)考试题
- 下一篇: 计算机考试准考证是什么样子