Spring Boot conditional注解用法詳解
1、conditional注解介紹
含義: 基于條件的注解
作用: 根據(jù)是否滿足某一個(gè)特定條件來(lái)決定是否創(chuàng)建某個(gè)特定的bean
意義: Springboot實(shí)現(xiàn)自動(dòng)配置的關(guān)鍵基礎(chǔ)能力
2、常見(jiàn)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è)試類
拋出異常,表示沒(méi)有找到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í)并沒(méi)有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)文章
詳解eclipse項(xiàng)目中.classpath文件的使用
這篇文章主要介紹了詳解eclipse項(xiàng)目中.classpath文件的使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10關(guān)于Idea中的.properties文件顯示問(wèn)題
這篇文章主要介紹了關(guān)于Idea中的.properties文件顯示問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07詳解基于Mybatis-plus多租戶實(shí)現(xiàn)方案
這篇文章主要介紹了詳解基于Mybatis-plus多租戶實(shí)現(xiàn)方案,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04JVM內(nèi)存結(jié)構(gòu)相關(guān)知識(shí)解析
這篇文章主要介紹了JVM內(nèi)存結(jié)構(gòu)相關(guān)知識(shí)解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11Java Gradle項(xiàng)目中的資源正確獲取方式
這篇文章主要介紹了Java Gradle項(xiàng)目中的資源正確獲取方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-11-11