SpringBoot?實(shí)現(xiàn)自定義的?@ConditionalOnXXX?注解示例詳解
實(shí)現(xiàn)一個(gè)自定義的 @Conditional 派生注解
自定義一個(gè)注解,繼承 @Conditional 注解
// 派生注解 @Retention(RetentionPolicy.RUNTIME) @Target({ ElementType.TYPE, ElementType.METHOD }) @Documented @Conditional(CustomConditional.class) public @interface ConditionalOnCustom { String[] value() default {}; }
注解的處理類
public class CustomConditional implements Condition { @Override public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { // 獲取到自定義注解中的 value 值 String[] properties = (String[]) metadata.getAnnotationAttributes("smoketest.test.condition.ConditionalOnCustom").get("value"); // 遍歷自定義屬性的 value 值 for (String property : properties) { // 獲取定義在配置文件中的值,并且 equals("customBean") 則返回 true if ("customBean".equals(context.getEnvironment().getProperty(property))) { return true; } } return false; } }
使用注解
@Component @ConditionalOnCustom({"smoketest.test.condition.bean"}) public class ConditionalUse { }
application.properties 中配置變量
smoketest.test.condition.bean = customBean
獲取 ConditionalUse 對象
@SpringBootApplication @ConfigurationPropertiesScan public class SampleTestApplication { public static void main(String[] args) { ConfigurableApplicationContext context = SpringApplication.run(SampleTestApplication.class, args); ConditionalUse bean = context.getBean(ConditionalUse.class); System.out.println(bean); } }
程序啟動(dòng)可以看到成功獲取 ConditionalUse 對象
Conditional 派生注解的類如何注入到 spring 容器
@Conditional 注解在 spring 的 ConfigurationClassParse 類中會調(diào)用 ConditionEvaluator.shouldSkip() 方法進(jìn)行判斷,Condition 接口的 matches() 是否返回 true,如果返回 true,就實(shí)例化對象,并注冊到 spring 容器中
- shouldSkip() 這個(gè)方法執(zhí)行的邏輯主要是如果是解析階段則跳過,如果是注冊階段則不跳過
- 如果是在注冊階段即 REGISTER_BEAN 階段的話,此時(shí)會得到所有的 Condition 接口的具體實(shí)現(xiàn)類并實(shí)例化這些實(shí)現(xiàn)類,然后再執(zhí)行下面關(guān)鍵的代碼進(jìn)行判斷是否需要跳過
if ((requiredPhase == null || requiredPhase == phase) && !condition.matches(this.context, metadata)) { return true; }
- 上面代碼最重要的邏輯是調(diào)用 Condition 接口的具體實(shí)現(xiàn)類的 matches() 方法,若 matches() 返回 false,則跳過,不進(jìn)行注冊 bean 的操作
- 若 matches() 返回 true,則不跳過,進(jìn)行注冊 bean 的操作
到此這篇關(guān)于SpringBoot 實(shí)現(xiàn)自定義的 @ConditionalOnXXX 注解示例詳解的文章就介紹到這了,更多相關(guān)SpringBoot @ConditionalOnXXX 注解內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解決spring AOP中自身方法調(diào)用無法應(yīng)用代理的問題
這篇文章主要介紹了解決spring AOP中自身方法調(diào)用無法應(yīng)用代理的問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08Mybatis?TypeHandler接口及繼承關(guān)系示例解析
這篇文章主要為大家介紹了Mybatis?TypeHandler接口及繼承關(guān)系示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02URLConnection發(fā)送HTTP請求的方法_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要介紹了URLConnection發(fā)送HTTP請求的方法,主要介紹了如何通過Java(模擬瀏覽器)發(fā)送HTTP請求,有興趣的可以了解一下2017-07-07詳解Java 自動(dòng)裝箱與拆箱的實(shí)現(xiàn)原理
本篇文章主要介紹了詳解Java 自動(dòng)裝箱與拆箱的實(shí)現(xiàn)原理,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-04-04淺談springboot內(nèi)置tomcat和外部獨(dú)立部署tomcat的區(qū)別
這篇文章主要介紹了淺談springboot內(nèi)置tomcat和外部獨(dú)立部署tomcat的區(qū)別,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-10-10java 中 String format 和Math類實(shí)例詳解
這篇文章主要介紹了java 中 String format 和Math類實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-06-06Springboot?上傳文件或頭像(MultipartFile、transferTo)
本文主要介紹了Springboot?上傳文件或頭像(MultipartFile、transferTo),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04