您好,登錄后才能下訂單哦!
conditional注解如何在Spring Boot中使用?很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來(lái)學(xué)習(xí)下,希望你能有所收獲。
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è)試類
拋出異常,表示沒(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è)試成功
看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝您對(duì)億速云的支持。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。