Spring中@Conditional注解的用法
@Conditional
@Conditional是Spring4新提供的注解,它的作用是按照一定的條件進行判斷,滿足條件給容器注冊bean。
@Target({ElementType.TYPE, ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface Conditional { Class<? extends Condition>[] value(); }
從代碼中可以看到,需要傳入一個Class數(shù)組,并且需要繼承Condition接口:
@FunctionalInterface public interface Condition { boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata); }
Condition是個接口,需要實現(xiàn)matches方法,返回true則注入bean,false則不注入。
用法如下
下面舉例測試Condition的注入Bean
舉例測試
首先創(chuàng)建Dog類
@Data @NoArgsConstructor @AllArgsConstructor public class Dog { private String name; private String gender; }
然后創(chuàng)建MyCondition類
重寫matches方法, 返回true則注入bean,false則不注入 如果容器中注入了 dog 就返回true
public class MyCondition implements Condition { /** * 重寫matches方法, 返回true則注入bean,false則不注入。 * */ @Override public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { ConfigurableListableBeanFactory beanFactory = context.getBeanFactory(); // 如果容器中注入了 dog 就返回true if(beanFactory.containsBean("dog")){ return true; } return false; } }
定義兩個Bean
@Configuration public class MyConfig { @Bean("dog") //@ConditionalOnBean(name = {"dog2"}) public Dog dog1(){ return new Dog("金毛","公"); } @Bean("dog1") @Conditional(value = MyCondition.class) public Dog dog2(){ return new Dog("拉布拉多","母"); } }
測試方法AnnotationConfigApplicationContext
AnnotationConfigApplicationContext是一個獨立的應(yīng)用上下文,它接受帶注釋的類作為輸入。 例如@Configuration或@Component。 可以使用scan()查找 Bean,也可以使用register()注冊 Bean。
public class tets { public static void main(String[] args) { AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MyConfig.class); Map<String, Dog> beansOfType = applicationContext.getBeansOfType(Dog.class); System.out.println(beansOfType); } }
測試結(jié)果如下
如果將MyCondition類中邏輯改為,如果如果容器中之前注入了 dog 就返回false,不注入其他的, 即:
public class MyCondition implements Condition { /** * 重寫matches方法, 返回true則注入bean,false則不注入。 * */ @Override public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { ConfigurableListableBeanFactory beanFactory = context.getBeanFactory(); // 如果容器中之前注入了 dog 就返回false if(beanFactory.containsBean("dog")){ return false; } return true; } }
那么結(jié)果如下:
@Conditional本身還是一個父注解,派生出大量的子注解,如下:
- @ConditionalOnBean:當(dāng)容器中有指定Bean的條件下進行實例化。
- @ConditionalOnMissingBean:當(dāng)容器里沒有指定Bean的條件下進行實例化。
- @ConditionalOnClass:當(dāng)classpath類路徑下有指定類的條件下進行實例化。
- @ConditionalOnMissingClass:當(dāng)類路徑下沒有指定類的條件下進行實例化。
- @ConditionalOnWebApplication:當(dāng)項目是一個Web項目時進行實例化。
- @ConditionalOnNotWebApplication:當(dāng)項目不是一個Web項目時進行實例化。
- @ConditionalOnProperty:當(dāng)指定的屬性有指定的值時進行實例化。
- @ConditionalOnExpression:基于SpEL表達(dá)式的條件判斷。
- @ConditionalOnJava:當(dāng)JVM版本為指定的版本范圍時觸發(fā)實例化。
- @ConditionalOnResource:當(dāng)類路徑下有指定的資源時觸發(fā)實例化。
- @ConditionalOnJndi:在JNDI存在的條件下觸發(fā)實例化。
- @ConditionalOnSingleCandidate:當(dāng)指定的Bean在容器中只有一個,或者有多個但是指定了首選的Bean時觸發(fā)實例化。
測試@ConditionalOnBean和@ConditionalOnMissingBean注解
修改之前的配置類如下
@Configuration public class MyConfig { @Bean("dog") public Dog dog1(){ return new Dog("金毛","公"); } @Bean("dog1") @ConditionalOnBean(name = {"dog"})// spring容器中有dog這個Bean 才會創(chuàng)建dog1 // @ConditionalOnMissingBean(name = {"dog"})// spring容器中不存在 dog這個Bean 才會創(chuàng)建dog1 public Dog dog2(){ return new Dog("拉布拉多","母"); } }
結(jié)果如下:
然后再將第二個注釋放開,第一個注釋注釋掉 結(jié)果如下:
到此這篇關(guān)于Spring中@Conditional注解的用法的文章就介紹到這了,更多相關(guān)@Conditional注解內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java線程安全鎖ReentrantReadWriteLock原理分析readLock
這篇文章主要為大家介紹了java線程安全鎖ReentrantReadWriteLock原理分析readLock,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-10-10SpringCloud Eureka Provider及Consumer的實現(xiàn)
這篇文章主要介紹了SpringCloud Eureka 提供者及調(diào)用者的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-10-10SpringBoot整合EasyExcel進行大數(shù)據(jù)處理的方法詳解
EasyExcel是一個基于Java的簡單、省內(nèi)存的讀寫Excel的開源項目。在盡可能節(jié)約內(nèi)存的情況下支持讀寫百M的Excel。本文將在SpringBoot中整合EasyExcel進行大數(shù)據(jù)處理,感興趣的可以了解一下2022-05-05springboot實現(xiàn)https雙向傳輸協(xié)議的示例代碼
本文主要介紹了springboot實現(xiàn)https雙向傳輸協(xié)議的示例代碼,包含配置證書和私鑰路徑、調(diào)用請求方法等步驟,具有一定的參考價值,感興趣的可以了解一下2025-03-03idea2020.1版本git提交項目到github上的方法
這篇文章主要介紹了idea2020.1版本git提交項目到github上的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2020-06-06