淺談SpringBoot中的@Conditional注解的使用
概述
Spring boot 中的 @Conditional 注解是一個不太常用到的注解,但確實非常的有用,我們知道 Spring Boot 是根據(jù)配置文件中的內(nèi)容,決定是否創(chuàng)建 bean,以及如何創(chuàng)建 bean 到 Spring 容器中,而 Spring boot 自動化配置的核心控制,就是 @Conditional 注解。
@Conditional 注解是 Spring 4.0 之后出的一個注解,與其搭配的一個接口是 Condition,@Conditional 注解會根據(jù)具體的條件決定是否創(chuàng)建 bean 到容器中,接下來看看 @Conditional 注解的簡單使用。
1. @Conditional 和 Condition 接口搭配使用
這里需要實現(xiàn)的功能是,我們根據(jù)配置文件中的具體內(nèi)容,來決定是否創(chuàng)建 bean,首先我們在 application.yml 中加上一個自定義配置:
這里我們決定,這個配置中包含了 product 這個字符串的時候,才創(chuàng)建 bean。Product 是我自己隨便創(chuàng)建的一個實體類,你可以自行創(chuàng)建。
新建一個類 ProductCondition
,內(nèi)容如下:
public class ProductCondition implements Condition { @Override public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) { //從配置文件中獲取屬性 String property = conditionContext.getEnvironment().getProperty("create.bean"); if (property != null){ return property.contains("product"); } else { return false; } } }
這個類實現(xiàn)了 Condition 接口,這個接口只有一個方法,我們從配置文件中獲取剛才創(chuàng)建的自定義配置,如果配置中包含了 product 這個字符串,就會返回 true。
接下來創(chuàng)建一個配置類 ProductConfig,內(nèi)容如下:
@Configuration public class ProductConfig { @Conditional(ProductCondition.class) @Bean(name = "product") public Product createProd(){ return Product.builder().id(12312).categoryId(12). productName("Mac Book Pro").productImg("prod.png") .productPrice(18000).build(); } }
我們在創(chuàng)建的 bean 方法前面加上了 @Conditional 注解,判斷的標(biāo)準(zhǔn)是剛才的 ProductCondition,如果是 true,則創(chuàng)建 bean,否則不創(chuàng)建。我們寫一個測試類,來測試一下 bean 是否被創(chuàng)建了。測試代碼如下:
@Slf4j @SpringBootTest @RunWith(SpringRunner.class) public class ProductConfigTest { @Test public void createProd() { try { Product product = SpringContextUtil.getBean("product", Product.class); if (product != null){ System.out.println("創(chuàng)建了 bean : " + product.toString()); } } catch (Exception e){ log.info("發(fā)生異常,{}", e.getMessage()); System.out.println("沒有創(chuàng)建 bean"); } } }
運行測試代碼,發(fā)現(xiàn) bean 已經(jīng)創(chuàng)建了:
如果把 application.yml 中的配置改一下,不包含 product 這個字符串,那么返回的是 false,bean 則不會被創(chuàng)建,你可以試一下。
2. @ConditionalOnClass 的使用
這個注解的屬性可以跟上一個類的完整路徑或者是類的 Class 對象,如果類存在,則會創(chuàng)建 bean,例如下面的例子:
@Configuration public class ProductConfig { @ConditionalOnClass(name = "com.roseduan.demo.entity.Product") @Bean(name = "product") public Product createProd(){ return Product.builder().id(12312).categoryId(12). productName("Mac Book Pro").productImg("prod.png") .productPrice(18000).build(); } }
這個路徑下面的實體類 Product 是存在的,所以會創(chuàng)建 bean,如果是一個不存在的類,則不會創(chuàng)建。
3. @ConditionalOnProperty 的使用
這個注解可以直接從配置文件中獲取屬性,然后做為是否創(chuàng)建 bean 的依據(jù)。例如我們在 application.yml 中添加一個自定義配置:
ProductConfig 類的內(nèi)容是這樣的:
@Configuration public class ProductConfig { @ConditionalOnProperty(value = "create.product.bean") @Bean(name = "product") public Product createProd(){ return Product.builder().id(12312).categoryId(12). productName("Mac Book Pro").productImg("prod.png") .productPrice(18000).build(); } }
這里使用了 @ConditionalOnProperty 注解,從文件中讀取配置,因為我們設(shè)置的是 true,所以這個 bean 會被創(chuàng)建,如果設(shè)置成 false,則 bean 不會被創(chuàng)建,你可以自己試一下。根據(jù)這個特性,我們可以給一些特定的配置加上一個開關(guān),非常方便控制。
這里我只是列舉了幾個常用的注解,你可以查看官方文檔,里面有更詳細的說明:
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java中對AtomicInteger和int值在多線程下遞增操作的測試
這篇文章主要介紹了Java中對AtomicInteger和int值在多線程下遞增操作的測試,本文得出AtomicInteger操作 與 int操作的效率大致相差在50-80倍上下的結(jié)論,需要的朋友可以參考下2014-09-09Intellij IDEA命令行執(zhí)行java無法加載主類解決方案
這篇文章主要介紹了Intellij IDEA命令行執(zhí)行java無法加載主類解決方案,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-09-09詳解基于IDEA2020.1的JAVA代碼提示插件開發(fā)例子
這篇文章主要介紹了詳解基于IDEA2020.1的JAVA代碼提示插件開發(fā)例子,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06java面向?qū)ο笾畬W(xué)生信息管理系統(tǒng)
這篇文章主要為大家詳細介紹了java面向?qū)ο笾畬W(xué)生信息管理系統(tǒng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-03-03Mybatis下動態(tài)sql中##和$$的區(qū)別講解
今天小編就為大家分享一篇關(guān)于Mybatis下動態(tài)sql中##和$$的區(qū)別講解,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-03-03