Spring 4.0新功能:@Conditional注解詳細介紹
前言
最近在學習spring,抽空會將學習的知識總結(jié)下面,本文我們會接觸spring 4的新功能:@Conditional注解。在之前的spring版本中,你處理conditions只有以下兩個方法:
- 在3.1版本之前,你需要使用spring expression language
- 在3.1版本發(fā)布時,profiles被引入來處理conditions。
讓我們分別看看以上兩者,在來理解spring 4帶來的@Conditional注解。
Spring Expression Language(SPeL)
SPeL的三元標識符(IF-THEN-ELSE)可以在spring配置文件中用來表達條件語句。
<bean id="flag"> <constructor-arg value="#{systemProperties['system.propery.flag'] ?: false }" /> </bean> <bean id="bean"> <property name="property" value="#{ flag ? 'yes' : 'no' }"/> </bean>
這個bean的屬性依賴于flag的值,該值是使用外部屬性注入的,這樣bean就具有了動態(tài)的能力。
使用 Profiles
這是在spring 3.1引入的。像下面這樣使用。
<!-- default configuration - will be loaded if no profile is specified --> <!-- This will only work if it's put at the end of the configuration file --> <!-- so no bean definitions after that --> <beans profile="default"> <import resource="classpath:default.xml" /> </beans> <!-- some other profile --> <beans profile="otherProfile"> <import resource="classpath:other-profile.xml" /> </beans>
使用spring 4的@Conditional注解
現(xiàn)在介紹@Conditional注解。官方文檔的說明是“只有當所有指定的條件都滿足是,組件才可以注冊”。主要的用處是在創(chuàng)建bean時增加一系列限制條件。
Conditional接口的聲明如下:
@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE, ElementType.METHOD) public @interface Conditional{ Class <!--?extends Condition-->[] value(); }
所以@Conditional注解使用方法如下
- 類型級別,可以在@Component 或是 @Configuration類上使用
- 原型級別,可以用在其他自定義的注解上
- 方法級別,可以用在@Bean的方法上
如果一個@Configuration類使用了@Conditional,會影響所有@Bean方法和@Import關聯(lián)類
public interface Condition{ /** Determine if the condition matches. * @param context the condition context * @param metadata meta-data of the {@link AnnotationMetadata class} or * {@link Method method} being checked. * @return {@code true} if the condition matches and the component can be registered * or {@code false} to veto registration. */ boolean matches(ConditionContext context, AnnotatedTypeMedata metadata); }
下面是一個例子
public class SystemPropertyCondition implements Condition { @Override public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { return (System.getProperty("flag") != null); } } class SystemPropertyAbsentCondition implements Condition { @Override public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { return (System.getProperty("flag") == null); } }
這里我們有兩個類:SystemPropertyCondition和SystemPropertyAbsentCondtion. 這兩個類都實現(xiàn)了Condition接口.覆蓋的方法基于屬性flag返回一個布爾值。
現(xiàn)在我們定義兩個類,一個是positive條件,一個是negative條件:
@Bean @Conditional(SystemPropertyCondition.class) public SampleService service1() { return new SampleServiceImpl1(); } @Bean @Conditional(SystemPropertyAbsentCondition.class) public SampleService service2() { return new SampleServiceImpl2(); }
上面提到的profiles已經(jīng)通過conditional原型注解進行了修改。
總結(jié)
本文介紹了spring 4的conditianal注解。注意condition注解是不會繼承的。如果一個父類使用了conditional注解,其子類是不會擁有conditions的。如果你動手嘗試以上的例子,會幫助你獲得更好的理解。
好了,以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
相關文章
Spring?boot2.0?實現(xiàn)日志集成的方法(2)
這篇文章主要介紹了Spring?boot2.0?實現(xiàn)日志集成的方法,上一章講解了spring?boot日志簡單集成,這篇我們將日志進行分類,常規(guī)日志、異常日志、監(jiān)控日志等,需要將日志輸出到不同的文件,具體內(nèi)容需要的小伙伴可以參考一下2022-04-04詳細聊聊SpringBoot中動態(tài)切換數(shù)據(jù)源的方法
在大型分布式項目中,經(jīng)常會出現(xiàn)多數(shù)據(jù)源的情況,下面這篇文章主要給大家介紹了關于SpringBoot中動態(tài)切換數(shù)據(jù)源的相關資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下2021-09-09Springboot實現(xiàn)Excel批量導入數(shù)據(jù)并保存到本地
這篇文章主要為大家詳細介紹了Springboot實現(xiàn)Excel批量導入數(shù)據(jù)并將文件保存到本地效果的方法,文中的示例代講解詳細,需要的可以參考一下2022-09-09關于kafka消費不到遠程bootstrap-server?數(shù)據(jù)的問題
很多朋友遇到kafka消費不到遠程bootstrap-server?數(shù)據(jù)的問題,怎么解決這個問題,很多朋友不知所措,下面小編給大家?guī)砹岁P于kafka消費不到遠程bootstrap-server?數(shù)據(jù)的問題及解決方法,感興趣的朋友跟隨小編一起看看吧2021-11-11idea 普通文件夾 轉(zhuǎn)換成 module操作
這篇文章主要介紹了idea 普通文件夾 轉(zhuǎn)換成 module操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08