Spring注解之@Conditional使用解析
@Conditional
@Conditional注解可以說是SpringBoot的條件注解,表示組件只有在所有指定條件都匹配時(shí)才有資格注冊,條件是可以在 bean 定義注冊之前??以編程方式確定的任何狀態(tài)。
@Conditional注解可以通過以下任何方式使用:
- 作為直接或間接使用@Component注解的任何類的類型級注釋,包括@Configuration類
- 作為元注解,用于編寫自定義構(gòu)造型注釋
- 作為任何@Bean方法的方法級注解
如果@Configuration類被標(biāo)記為@Conditional ,那么與該類關(guān)聯(lián)的所有@Bean方法、 @Import 注解和@ComponentScan注解都將受制于條件。
SpringBoot @ConditionalOn*
SpringBoot提供的@ConditionalOn*的注解都是基于@Conditional注解實(shí)現(xiàn)

@ConditionalOnBean
僅當(dāng)滿足所有指定要求的 bean 已包含在BeanFactory中時(shí)才匹配的Conditional 。
必須滿足所有要求才能匹配條件,但不必由同一個(gè) bean 滿足。
@ConditionalOnClass
僅當(dāng)指定的類在類路徑上時(shí)才匹配的Conditional 。
可以在@Configuration類上安全地指定value() ,因?yàn)樵诩虞d類之前使用 ASM 解析注釋元數(shù)據(jù)。
放置在@Bean方法上時(shí)需要格外小心,考慮將條件隔離在單獨(dú)的Configuration類中,特別是如果方法的返回類型與value()中的值匹配。
@ConditionalOnCloudPlatform
當(dāng)指定的云平臺(tái)處于活動(dòng)狀態(tài)時(shí)匹配的Conditional。
@ConditionalOnExpression
取決于 SpEL 表達(dá)式的值的條件元素的配置注解。
@ConditionalOnJava
根據(jù)運(yùn)行應(yīng)用程序的 JVM 版本匹配的Conditional。
@ConditionalOnJndi
基于 JNDI InitialContext的可用性和查找特定位置的能力匹配的Conditional。
@ConditionalOnMissingBean
僅當(dāng)BeanFactory中已不包含滿足指定要求的 bean 時(shí)才匹配的Conditional 。 條件匹配不必滿足任何要求,并且同一bean不必滿足這些要求。
@ConditionalOnMissingClass
僅當(dāng)指定的類不在類路徑上時(shí)才匹配的Conditional。
@ConditionalOnNotWebApplication
僅在應(yīng)用程序上下文不是 Web 應(yīng)用程序上下文時(shí)匹配的Conditional。
@ConditionalOnProperty
檢查指定屬性是否具有特定值的Conditional 。 默認(rèn)情況下,屬性必須存在于Environment中并且不等于false 。 havingValue()和matchIfMissing()屬性允許進(jìn)一步自定義。havingValue屬性可用于指定屬性應(yīng)具有的值。如果該屬性根本不包含在Environment中,則matchIfMissing()屬性。 默認(rèn)情況下,缺少的屬性不匹配。
@ConditionalOnResource
僅當(dāng)指定資源在類路徑上時(shí)才匹配的Conditional 。
@ConditionalOnSingleCandidate
僅當(dāng)指定類的 bean 已包含在BeanFactory中并且可以確定單個(gè)候選者時(shí)才匹配的Conditional 。 如果BeanFactory中已包含多個(gè)匹配的 bean 實(shí)例但已定義主要候選者,則條件也將匹配; 本質(zhì)上,如果自動(dòng)裝配具有定義類型的 bean,則條件匹配將成功。 該條件只能匹配到目前為止已由應(yīng)用程序上下文處理的 bean 定義,因此,強(qiáng)烈建議僅在自動(dòng)配置類上使用此條件。 如果候選 bean 可能由另一個(gè)自動(dòng)配置創(chuàng)建,請確保使用此條件的 bean 在之后運(yùn)行。
@ConditionalOnWebApplication
當(dāng)應(yīng)用程序是 Web 應(yīng)用程序時(shí)匹配的Conditional 。 默認(rèn)情況下,任何 Web 應(yīng)用程序都會(huì)匹配,但可以使用type()屬性縮小范圍。
@ConditionalOn*實(shí)現(xiàn)原理
以@ConditionalOnBean為例
@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Conditional(OnBeanCondition.class)
public @interface ConditionalOnBean{
//...
}@Conditional作為元注解,OnBeanCondition繼承SpringBootCondition,SpringBootCondition
實(shí)現(xiàn)自org.springframework.context.annotation.Condition。
public abstract class SpringBootCondition implements Condition {
private final Log logger = LogFactory.getLog(getClass());
@Override
public final boolean matches(ConditionContext context,
AnnotatedTypeMetadata metadata) {
String classOrMethodName = getClassOrMethodName(metadata);
try {
ConditionOutcome outcome = getMatchOutcome(context, metadata);
logOutcome(classOrMethodName, outcome);
recordEvaluation(context, classOrMethodName, outcome);
return outcome.isMatch();
}
catch (NoClassDefFoundError ex) {
}
}
}
//org.springframework.boot.autoconfigure.condition.OnBeanCondition
@Override
public ConditionOutcome getMatchOutcome(ConditionContext context,
AnnotatedTypeMetadata metadata) {
ConditionMessage matchMessage = ConditionMessage.empty();
if (metadata.isAnnotated(ConditionalOnBean.class.getName())) {
BeanSearchSpec spec = new BeanSearchSpec(context, metadata,
ConditionalOnBean.class);
MatchResult matchResult = getMatchingBeans(context, spec);
if (!matchResult.isAllMatched()) {
String reason = createOnBeanNoMatchReason(matchResult);
return ConditionOutcome.noMatch(ConditionMessage
.forCondition(ConditionalOnBean.class, spec).because(reason));
}
matchMessage = matchMessage.andCondition(ConditionalOnBean.class, spec)
.found("bean", "beans")
.items(Style.QUOTE, matchResult.getNamesOfAllMatches());
}
//...
return ConditionOutcome.match(matchMessage);
}到此這篇關(guān)于Spring注解之@Conditional使用解析的文章就介紹到這了,更多相關(guān)@Conditional使用解析內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
MyBatis中執(zhí)行相關(guān)SQL語句的方法
本文主要介紹了MyBatis中執(zhí)行相關(guān)SQL語句的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-08-08
SpringBoot啟動(dòng)速度優(yōu)化的5個(gè)技巧分享
作為一名后端開發(fā),日常工作中經(jīng)常與 Spring Boot 框架打交道,而Spring Boot 啟動(dòng)慢絕不是一個(gè)可以忽視的小問題,而是關(guān)系到開發(fā)效率、系統(tǒng)穩(wěn)定性和業(yè)務(wù)發(fā)展的關(guān)鍵痛點(diǎn),本文小編就和大家分享5個(gè)SpringBoot啟動(dòng)慢的優(yōu)化點(diǎn)吧2026-04-04
Java實(shí)現(xiàn)微信掃碼授權(quán)登錄完整步驟
微信掃碼登錄是目前非常流行的第三方登錄方式,這篇文章主要介紹了Java實(shí)現(xiàn)微信掃碼授權(quán)登錄的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2025-11-11
將Java的List結(jié)構(gòu)通過GSON庫轉(zhuǎn)換為JSON的方法示例
GONS是Google在GitHub上開源的Java類庫,提供各種Java對象和JSON格式對象之間的轉(zhuǎn)換功能,將Java的List結(jié)構(gòu)通過GSON庫轉(zhuǎn)換為JSON的方法示例2016-06-06
Java基于JNDI 實(shí)現(xiàn)讀寫分離的示例代碼
本文主要介紹了Java基于JNDI 實(shí)現(xiàn)讀寫分離的示例代碼,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-12-12
SpringBoot項(xiàng)目打包三方JAR的示例代碼
本篇文章主要介紹了SpringBoot項(xiàng)目打包三方JAR的示例代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09
Java實(shí)現(xiàn)的計(jì)時(shí)器【秒表】功能示例
這篇文章主要介紹了Java實(shí)現(xiàn)的計(jì)時(shí)器【秒表】功能,結(jié)合實(shí)例形式分析了Java結(jié)合JFrame框架的計(jì)時(shí)器功能相關(guān)操作技巧,需要的朋友可以參考下2019-02-02

