Spring Boot conditional注解用法詳解
1、conditional注解介紹
含義: 基于條件的注解
作用: 根據(jù)是否滿足某一個(gè)特定條件來(lái)決定是否創(chuàng)建某個(gè)特定的bean
意義: Springboot實(shí)現(xiàn)自動(dòng)配置的關(guān)鍵基礎(chǔ)能力
2、常見conditional注解
@ConditionalOnBean 框架中存在某個(gè)Bean時(shí)生效
@ConditionalOnMissingBean 在Bean不存在時(shí)生效
@ConditionalOnClass框架中存在某個(gè)Class時(shí)生效
@ConditionalOnMissingClass在Class不存在時(shí)生效
@ConditionalOnWebApplication 當(dāng)前是web環(huán)境
@ConditionalOnNotWebApplication 當(dāng)前不是web環(huán)境
@ConditionalOnProperty 當(dāng)前框架中是否包含特定的屬性
@ConditionalOnJava 當(dāng)前是否存在某個(gè)Java版本
3、Conditional的使用
1) 創(chuàng)建A.java,增加注解ConditionalOnProperty,表示系統(tǒng)中有這個(gè)屬性才實(shí)例化A
@Component
@ConditionalOnProperty("com.example.condition")
public class A {
}
2) 創(chuàng)建測(cè)試類
@RunWith(SpringRunner.class)
@SpringBootTest
@Import(MyBeanImport.class)
public class ConditionTest implements ApplicationContextAware {
private ApplicationContext applicationContext;
@Test
public void testA() {
System.out.println(applicationContext.getBean(A.class));
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
}
3、運(yùn)行測(cè)試類
拋出異常,表示沒有找到A這個(gè)類。

然后在application.properties文件中增加屬性

再次運(yùn)行測(cè)試。成功。

4、A類中有個(gè)注解ConditionOnProperty

1) 進(jìn)入注解ConditionOnProperty。里面有一個(gè)@Conditional注解

2) 進(jìn)入@Conditional注解。里面的value是Class類型,并且繼承自Condition接口

3) 進(jìn)入Condition接口。里面只有一個(gè)方法。當(dāng)這個(gè)方法返回true時(shí),這個(gè)bean才會(huì)注入到容器當(dāng)中。

5、自定義Conditional 注解
1) 創(chuàng)建MyCondition類。實(shí)現(xiàn)Condition接口重寫matches方法,符合條件返回true
public class MyCondition implements Condition {
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
String[] properties = (String[]) metadata
.getAnnotationAttributes("com.example.demo.condi.MyConditionAnnotation")
.get("value");
for(String property : properties){
if(StringUtils.isEmpty(context.getEnvironment().getProperty(property))){
return false;
}
}
return true;
}
}
2) 創(chuàng)建注解MyConditionAnnotation ,并且引入Conditional注解,引入MyCondition類
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Conditional({MyCondition.class})
public @interface MyConditionAnnotation {
String[] value() default {};
}
3) 創(chuàng)建類AA使用注解MyConditionAnnotation
@Component
@MyConditionAnnotation({"com.example.condition1","com.example.condition2"})
public class AA {
}
4) 測(cè)試
a) 此時(shí)并沒有com.example.condition1和com.example.condition2這兩個(gè)屬性值,所有測(cè)試失敗

b) 然后增加這兩個(gè)屬性。

測(cè)試成功

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Spring?Boot自動(dòng)配置的原理及@Conditional條件注解
- SpringBoot深入探究@Conditional條件裝配的使用
- 淺析SpringBoot2底層注解@Conditional@ImportResource
- springboot @ConditionalOnMissingBean注解的作用詳解
- Spring Boot中@ConditionalOnProperty的使用方法
- Spring Boot @Conditional注解用法示例介紹
- 淺談SpringBoot中的@Conditional注解的使用
- Spring boot中@Conditional和spring boot的自動(dòng)配置實(shí)例詳解
- Spring?Boot?詳細(xì)分析Conditional自動(dòng)化配置注解
相關(guān)文章
一文教你掌握J(rèn)ava如何實(shí)現(xiàn)判空
實(shí)際項(xiàng)目中我們會(huì)有很多地方需要判空校驗(yàn),如果不做判空校驗(yàn)則可能產(chǎn)生NullPointerException異常。所以本文小編為大家整理了Java中幾個(gè)常見的判空方法,希望對(duì)大家有所幫助2023-04-04
基于Java反射技術(shù)實(shí)現(xiàn)簡(jiǎn)單IOC容器
這篇文章主要介紹了基于Java反射技術(shù)實(shí)現(xiàn)簡(jiǎn)單IOC容器,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07
ehcache開源緩存框架_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
Ehcache是現(xiàn)在最流行的純Java開源緩存框架,這篇文章主要介紹了ehcache框架的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07
解決Spring運(yùn)行時(shí)報(bào)錯(cuò):Consider defining a bean o
該文章主要講述了在使用Spring框架時(shí),如果遇到某個(gè)bean未找到的錯(cuò)誤,應(yīng)該在配置文件中定義該bean,解決方法是在對(duì)應(yīng)的類上添加@Component注解2025-01-01
SpringBoot增強(qiáng)Controller方法@ControllerAdvice注解的使用詳解
這篇文章主要介紹了SpringBoot增強(qiáng)Controller方法@ControllerAdvice注解的使用詳解,@ControllerAdvice,是Spring3.2提供的新注解,它是一個(gè)Controller增強(qiáng)器,可對(duì)controller進(jìn)行增強(qiáng)處理,需要的朋友可以參考下2023-10-10

