Spring Boot中@Conditional注解介紹
1. @Conditional 注解
@Conditional注解是Spring-context模塊提供了一個(gè)注解,該注解的作用是可以根據(jù)一定的條件來使@Configuration注解標(biāo)記的配置類是否生效,代碼如下:
// // Source code recreated from a .class file by IntelliJ IDEA // (powered by FernFlower decompiler) // package org.springframework.context.annotation; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Target({ElementType.TYPE, ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface Conditional { Class<? extends Condition>[] value(); }
value值為實(shí)現(xiàn)Condition 接口的一個(gè)Class,Spring框架根據(jù)實(shí)現(xiàn)Conditon接口的matches方法返回true或者false來做以下操作,如果matches方法返回true,那么該配置類會(huì)被Spring掃描到容器里, 如果為false,那么Spring框架會(huì)自動(dòng)跳過該配置類不進(jìn)行掃描裝配,使用方法:
實(shí)現(xiàn)Condition接口, 例如在配置文件里配置了dataSource.none=true, 那么表示不需要使用數(shù)據(jù)源,那么Spring在掃描的時(shí)候會(huì)自動(dòng)跳過該配置類。
package com.bing.sh.datasource; import org.springframework.context.annotation.Condition; import org.springframework.context.annotation.ConditionContext; import org.springframework.core.type.AnnotatedTypeMetadata; /** * 如果為返回false,那么Spring會(huì)忽略配置類 */ public class DataSourceCondition implements Condition { @Override public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) { // 此處使用 conditionContext獲取Environment即可。 String configureDataSource = conditionContext.getEnvironment().getProperty("dataSource.none", "false"); return "false".equals(configureDataSource); } }
定義配置類,與@Conditional注解一起使用:
@Configuration @Conditional(value = DataSourceCondition.class) public class CustomDataSourceConfig { }
除了Conditional注解,Spring boot 框架提供了其他conditional系列注解。
2. Springboot擴(kuò)展
SpringBoot的spring-boot-autoconfigure模塊也提供了Conditional系列的相關(guān)注解,這些注解能幫助開發(fā)者根據(jù)一定的條件去裝載需要的Bean。
1) @ConditionalOnClass和@ConditionalOnMissingClass注解
當(dāng)Spring加載的Bean被@ConditionOnClass注解標(biāo)記時(shí),類加載器會(huì)先去先找到指定的Class, 如果沒有找到目標(biāo)Class,那么被ConditionOnClass注解標(biāo)記的類不會(huì)被Spring裝載,相反ConditionalOnMissingBean是指如果沒有找到目標(biāo)Class, 那么就裝載該類。
2) @ConditionalOnBean 和@ConditionalOnMissingBean注解
當(dāng)Spring加載的Bean被@ConditionalOnBean注解標(biāo)記時(shí),接下來會(huì)先找到指定的Bean,如果沒有找到目標(biāo)Bean,那么被@ConditionalOnBean標(biāo)記的類不會(huì)被Spring裝載,相反ConditionalOnMissingBean是指如果沒有Class, 那么就裝載該Bean。
3) @ConditionalOnProperty注解
該注解的作用是解析application.yml/application.properties 里的配置生成條件來生效,也是與@Configuration注解一起使用。
屬性 | 功能 | 其他 |
prefix | 讀取配置里的前綴值為prefix的屬性, 如果沒有返回false | |
name | 讀取屬性配置里的Key值,如果配置了prefix,那么需要先拼接prefix然后匹配havingValue值 | |
havingValue | 匹配屬性里的值 | |
matchIfMissing | 當(dāng)未找到對(duì)應(yīng)的配置時(shí)是否匹配,默認(rèn)為false, 如果為true,沒有找到配置,那么也匹配。 |
使用場(chǎng)景,例如在指定數(shù)據(jù)源時(shí),指定datasource的type。例如包含如下配置使用Hikari數(shù)據(jù)源。
spring.datasource.type=com.zaxxer.hikari.HikariDataSource
在使用時(shí),一般設(shè)置matchIfMissing=false, 這樣條件沒有匹配上的話會(huì)Spring在掃描bean時(shí)會(huì)自動(dòng)跳過該配置類。
到此這篇關(guān)于Spring Boot中@Conditional注解介紹的文章就介紹到這了,更多相關(guān)SpringBoot @Conditional 內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JAVA實(shí)現(xiàn)經(jīng)典游戲坦克大戰(zhàn)的示例代碼
小時(shí)候大家都玩過坦克大戰(zhàn)吧,熟悉的旋律和豐富的關(guān)卡陪伴了我們一整個(gè)寒暑假。本文將通過Java+Swing實(shí)現(xiàn)這一經(jīng)典游戲,感興趣的可以學(xué)習(xí)一下2022-01-01Java中保留兩位小數(shù)的四種方法實(shí)現(xiàn)實(shí)例
今天小編就為大家分享一篇關(guān)于Java中保留兩位小數(shù)的四種方法實(shí)現(xiàn)實(shí)例,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-02-02Java?restTemplate發(fā)送get請(qǐng)求query參數(shù)傳遞問題解決
這篇文章主要為大家介紹了Java?restTemplate發(fā)送get請(qǐng)求query參數(shù)傳遞問題解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11Java?for循環(huán)標(biāo)簽跳轉(zhuǎn)到指定位置的示例詳解
這篇文章主要介紹了Java?for循環(huán)標(biāo)簽跳轉(zhuǎn)到指定位置,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-05-05maven引入kabeja依賴的實(shí)現(xiàn)步驟
本文主要介紹了maven引入kabeja依賴的實(shí)現(xiàn)步驟,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-09-09