Spring Boot框架中的@Conditional注解示例詳解
1. @Conditional 注解
@Conditional注解是Spring-context模塊提供了一個注解,該注解的作用是可以根據一定的條件來使@Configuration注解標記的配置類是否生效,代碼如下:
//
// 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值為實現Condition 接口的一個Class,Spring框架根據實現Conditon接口的matches方法返回true或者false來做以下操作,如果matches方法返回true,那么該配置類會被Spring掃描到容器里, 如果為false,那么Spring框架會自動跳過該配置類不進行掃描裝配,使用方法:
實現Condition接口, 例如在配置文件里配置了dataSource.none=true, 那么表示不需要使用數據源,那么Spring在掃描的時候會自動跳過該配置類。
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會忽略配置類
*/
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. Spring boot 擴展
SpringBoot的spring-boot-autoconfigure模塊也提供了Conditional系列的相關注解,這些注解能幫助開發(fā)者根據一定的條件去裝載需要的Bean。

1) @ConditionalOnClass和@ConditionalOnMissingClass注解
當Spring加載的Bean被@ConditionOnClass注解標記時,類加載器會先去先找到指定的Class, 如果沒有找到目標Class,那么被ConditionOnClass注解標記的類不會被Spring裝載,相反ConditionalOnMissingBean是指如果沒有找到目標Class, 那么就裝載該類。
2) @ConditionalOnBean 和@ConditionalOnMissingBean注解
當Spring加載的Bean被@ConditionalOnBean注解標記時,接下來會先找到指定的Bean,如果沒有找到目標Bean,那么被@ConditionalOnBean標記的類不會被Spring裝載,相反ConditionalOnMissingBean是指如果沒有Class, 那么就裝載該Bean。
3) @ConditionalOnProperty注解
該注解的作用是解析application.yml/application.properties 里的配置生成條件來生效,也是與@Configuration注解一起使用。
屬性 | 功能 | 其他 |
prefix | 讀取配置里的前綴值為prefix的屬性, 如果沒有返回false | |
name | 讀取屬性配置里的Key值,如果配置了prefix,那么需要先拼接prefix然后匹配havingValue值 | |
havingValue | 匹配屬性里的值 | |
matchIfMissing | 當未找到對應的配置時是否匹配,默認為false, 如果為true,沒有找到配置,那么也匹配。 |
使用場景,例如在指定數據源時,指定datasource的type。例如包含如下配置使用Hikari數據源。
spring.datasource.type=com.zaxxer.hikari.HikariDataSource

在使用時,一般設置matchIfMissing=false, 這樣條件沒有匹配上的話會Spring在掃描bean時會自動跳過該配置類。
到此這篇關于Spring Boot框架中的@Conditional系列注解的文章就介紹到這了,更多相關Spring Boot @Conditional注解內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Spring事務Transaction配置的五種注入方式詳解
這篇文章主要介紹了Spring事務Transaction配置的五種注入方式詳解,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-04-04
springboot+webmagic實現java爬蟲jdbc及mysql的方法
今天小編就為大家分享一篇springboot+webmagic實現java爬蟲jdbc及mysql的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-08-08
springBoot動態(tài)加載jar及如何將類注冊到IOC
在SpringBoot項目中動態(tài)加載jar文件并將其類注冊到IOC容器是一種高級應用方式,,這種方法為SpringBoot項目提供了更靈活的擴展能力,使得項目可以在不修改原有代碼的基礎上增加新的功能模塊,感興趣的朋友一起看看吧2024-11-11

