日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

《SpringBoot系列十六》条件装配时ConfigurationCondition和Condition有什么区别?什么时候用ConfigurationCondition?

發(fā)布時間:2024/1/18 javascript 62 豆豆
生活随笔 收集整理的這篇文章主要介紹了 《SpringBoot系列十六》条件装配时ConfigurationCondition和Condition有什么区别?什么时候用ConfigurationCondition? 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

一、前言

針對條件裝配我們討論了如下內(nèi)容:

  • 《SpringBoot系列十一》:精講如何使用@Conditional系列注解做條件裝配
  • 《SpringBoot系列十二》:如何自定義條件裝配(由@ConditionalOnClass推導(dǎo))
  • 《SpringBoot啟動流程六》:SpringBoot自動裝配時做條件裝配的原理(萬字圖文源碼分析)(含@ConditionalOnClass原理)
  • 《SpringBoot系列十三》:圖文精講@Conditional條件裝配實現(xiàn)原理
  • 《SpringBoot系列十四》:@ConditionalOnBean、@ConditionalOnMissingBean注解居然失效了!
  • 在《SpringBoot系列十三》:圖文精講@Conditional條件裝配實現(xiàn)原理一文中,我們知道了條件裝配時是分兩階段(配置類解析、Bean注冊)進(jìn)行的。

    二、ConfigurationCondition

    ConfigurationCondition接口是Spring4.0提供的注解,位于org.springframework.context.annotation包內(nèi),繼承自Condition接口;

    public interface ConfigurationCondition extends Condition {/*** 返回當(dāng)前Condition可以被評估的配置階段* Return the {@link ConfigurationPhase} in which the condition should be evaluated.*/ConfigurationPhase getConfigurationPhase();/*** Condition應(yīng)該被評估的各個配置階段* 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 的枚舉);

  • PARSE_CONFIGURATION:表示在配置類解析階段進(jìn)行Condition的評估;
  • REGISTER_BEAN:表示在注冊Bean階段進(jìn)行Condition的評估;
  • 1、ConfigurationCondition和Condition的區(qū)別?

    1> Condition評估的時機:

  • ConfigurationCondition中提供配置階段的概念,其包含兩個階段:PARSE_CONFIGURATION 和 REGISTER_BEAN,使用ConfigurationCondition接口實現(xiàn)類做Condition條件裝配時,會判斷傳入的配置階段和ConfigurationCondition#getConfigurationPhase()返回的配置階段是否一致,如果不一致則不進(jìn)行Condition評估;以此實現(xiàn)更細(xì)粒度的條件裝配控制。
  • Condition中沒有配置階段的概念,在任何時候都可以使用其進(jìn)行Condition評估;
  • 2> 使用對比:

  • OnBeanCondition實現(xiàn)ConfigurationCondition接口,getConfigurationPhase()返回ConfigurationPhase.REGISTER_BEAN,表示只在注冊Bean階段進(jìn)行Condition評估,其他階段ConditionEvaluator#shouldSkip()方法均直接返回false。
  • OnClassCondition實現(xiàn)Condition接口,配置類加載的任何階段都可以進(jìn)行條件評估。
  • 1)OnBeanCondition

    1> OnBeanCondition類圖:

    2> OnBeanCondition實現(xiàn)的方法:

    2)OnClassCondition

    1> OnBeanCondition類圖:

    2、什么時候用ConfigurationCondition?

    ConfigurationPhase的作用是控制條件評估(過濾)的時機:是在解析配置類的時候 還是在創(chuàng)建Bean的時候。

    一般而言ConfigurationCondition多用于 只在注冊Bean階段才進(jìn)行條件評估的Condition中使用,比如OnBeanCondition。以O(shè)nBeanCondition為例,其中的很多條件評估的依據(jù)只有在注冊Bean階段才會相對更加完整。

    總結(jié)

    以上是生活随笔為你收集整理的《SpringBoot系列十六》条件装配时ConfigurationCondition和Condition有什么区别?什么时候用ConfigurationCondition?的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。