Spring 4.0新功能:@Conditional注解詳細(xì)介紹
前言
最近在學(xué)習(xí)spring,抽空會(huì)將學(xué)習(xí)的知識(shí)總結(jié)下面,本文我們會(huì)接觸spring 4的新功能:@Conditional注解。在之前的spring版本中,你處理conditions只有以下兩個(gè)方法:
- 在3.1版本之前,你需要使用spring expression language
- 在3.1版本發(fā)布時(shí),profiles被引入來(lái)處理conditions。
讓我們分別看看以上兩者,在來(lái)理解spring 4帶來(lái)的@Conditional注解。
Spring Expression Language(SPeL)
SPeL的三元標(biāo)識(shí)符(IF-THEN-ELSE)可以在spring配置文件中用來(lái)表達(dá)條件語(yǔ)句。
<bean id="flag">
<constructor-arg value="#{systemProperties['system.propery.flag'] ?: false }" />
</bean>
<bean id="bean">
<property name="property" value="#{ flag ? 'yes' : 'no' }"/>
</bean>
這個(gè)bean的屬性依賴于flag的值,該值是使用外部屬性注入的,這樣bean就具有了動(dòng)態(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注解。官方文檔的說(shuō)明是“只有當(dāng)所有指定的條件都滿足是,組件才可以注冊(cè)”。主要的用處是在創(chuàng)建bean時(shí)增加一系列限制條件。
Conditional接口的聲明如下:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE, ElementType.METHOD)
public @interface Conditional{
Class <!--?extends Condition-->[] value();
}
所以@Conditional注解使用方法如下
- 類型級(jí)別,可以在@Component 或是 @Configuration類上使用
- 原型級(jí)別,可以用在其他自定義的注解上
- 方法級(jí)別,可以用在@Bean的方法上
如果一個(gè)@Configuration類使用了@Conditional,會(huì)影響所有@Bean方法和@Import關(guān)聯(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);
}
下面是一個(gè)例子
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);
}
}
這里我們有兩個(gè)類:SystemPropertyCondition和SystemPropertyAbsentCondtion. 這兩個(gè)類都實(shí)現(xiàn)了Condition接口.覆蓋的方法基于屬性flag返回一個(gè)布爾值。
現(xiàn)在我們定義兩個(gè)類,一個(gè)是positive條件,一個(gè)是negative條件:
@Bean
@Conditional(SystemPropertyCondition.class)
public SampleService service1() {
return new SampleServiceImpl1();
}
@Bean
@Conditional(SystemPropertyAbsentCondition.class)
public SampleService service2() {
return new SampleServiceImpl2();
}
上面提到的profiles已經(jīng)通過(guò)conditional原型注解進(jìn)行了修改。
總結(jié)
本文介紹了spring 4的conditianal注解。注意condition注解是不會(huì)繼承的。如果一個(gè)父類使用了conditional注解,其子類是不會(huì)擁有conditions的。如果你動(dòng)手嘗試以上的例子,會(huì)幫助你獲得更好的理解。
好了,以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
解決引入spring-cloud-starter-openfeign后部分類找不到的問(wèn)題
這篇文章主要介紹了解決引入spring-cloud-starter-openfeign后部分類找不到的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
Mybatis-Plus?動(dòng)態(tài)表名的實(shí)踐
本文主要介紹了Mybatis-Plus?動(dòng)態(tài)表名的實(shí)踐,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-08-08
java7鉆石語(yǔ)法知識(shí)點(diǎn)總結(jié)
在本篇文章里小編給大家整理的是關(guān)于java7鉆石語(yǔ)法的相關(guān)知識(shí)點(diǎn)內(nèi)容,有需要的朋友們參考下。2019-11-11
修改idea運(yùn)行內(nèi)存大小的方法總結(jié)
在開(kāi)發(fā)過(guò)程中,總會(huì)遇到idea運(yùn)行內(nèi)存不足,所以本文小編給大家介紹了修改idea運(yùn)行內(nèi)存大小的兩種方法,文中通過(guò)圖文給大家講解的非常詳細(xì),需要的朋友可以參考下2023-12-12
Java 中DateUtils日期工具類的實(shí)例詳解
這篇文章主要介紹了Java 中DateUtils日期工具類的實(shí)例詳解的相關(guān)資料,有時(shí)候開(kāi)發(fā)java項(xiàng)目使用日期類型,這里介紹下日期工具類,需要的朋友可以參考下2017-08-08
java實(shí)現(xiàn)仿windows 字體設(shè)置選項(xiàng)卡實(shí)例
本篇文章介紹了java仿windows 字體設(shè)置選項(xiàng)卡,可實(shí)現(xiàn)類似windows字體設(shè)置效果,需要的朋友可以參考下。2016-10-10
springsecurity 企業(yè)微信登入的實(shí)現(xiàn)示例
本文主要介紹了springsecurity 企業(yè)微信登入的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04

