一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - Spring 4.0新功能:@Conditional注解詳細介紹

Spring 4.0新功能:@Conditional注解詳細介紹

2020-12-28 09:54沈子平 Java教程

Spring Boot的強大之處在于使用了Spring 4框架的新特性:@Conditional注釋,此注釋使得只有在特定條件滿足時才啟用一些配置。下面這篇文章主要給大家介紹了關于Spring4.0中新功能:@Conditional注解的相關資料,需要的朋友可以參考下。

前言

最近在學習spring,抽空會將學習的知識總結下面,本文我們會接觸spring 4的新功能:@Conditional注解。在之前的spring版本中,你處理conditions只有以下兩個方法:

  • 在3.1版本之前,你需要使用spring expression language
  • 在3.1版本發布時,profiles被引入來處理conditions。

讓我們分別看看以上兩者,在來理解spring 4帶來的@Conditional注解。

Spring Expression Language(SPeL)

SPeL的三元標識符(IF-THEN-ELSE)可以在spring配置文件中用來表達條件語句。

?
1
2
3
4
5
6
<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就具有了動態的能力。

使用 Profiles

這是在spring 3.1引入的。像下面這樣使用。

?
1
2
3
4
5
6
7
8
9
10
<!-- 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注解

現在介紹@Conditional注解。官方文檔的說明是“只有當所有指定的條件都滿足是,組件才可以注冊”。主要的用處是在創建bean時增加一系列限制條件。

Conditional接口的聲明如下:

?
1
2
3
4
5
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE, ElementType.METHOD)
public @interface Conditional{
 Class <!--?extends Condition-->[] value();
}

所以@Conditional注解使用方法如下

  • 類型級別,可以在@Component 或是 @Configuration類上使用
  • 原型級別,可以用在其他自定義的注解上
  • 方法級別,可以用在@Bean的方法上

如果一個@Configuration類使用了@Conditional,會影響所有@Bean方法和@Import關聯類

?
1
2
3
4
5
6
7
8
9
10
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);
}

下面是一個例子

?
1
2
3
4
5
6
7
8
9
10
11
12
13
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. 這兩個類都實現了Condition接口.覆蓋的方法基于屬性flag返回一個布爾值。

現在我們定義兩個類,一個是positive條件,一個是negative條件:

?
1
2
3
4
5
6
7
8
9
10
11
@Bean
@Conditional(SystemPropertyCondition.class)
public SampleService service1() {
 return new SampleServiceImpl1();
}
 
@Bean
@Conditional(SystemPropertyAbsentCondition.class)
public SampleService service2() {
 return new SampleServiceImpl2();
}

上面提到的profiles已經通過conditional原型注解進行了修改。

總結

本文介紹了spring 4的conditianal注解。注意condition注解是不會繼承的。如果一個父類使用了conditional注解,其子類是不會擁有conditions的。如果你動手嘗試以上的例子,會幫助你獲得更好的理解。

好了,以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對服務器之家的支持。

原文鏈接:https://segmentfault.com/a/1190000011033012

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 精品国产自在在线在线观看 | 日本黄色影院 | 456亚洲人成高清在线 | 国产精品夜夜爽张柏芝 | 好猛好紧好硬使劲好大刺激视频 | 国产精品国语自产拍在线观看 | 日韩毛片在线影视 | 村上里沙40分钟在线观看 | 成人免费片 | 久久99国产亚洲高清观着 | 免费看60分钟大片视频播放 | 亚洲精品久久中文字幕 | 免费观看视频在线 | 精品日产1区2卡三卡麻豆 | 手机亚洲第一页 | 久久青青草视频在线观 | 国产一区二区视频在线播放 | 四虎成人免费 | 亚洲国产综合久久精品 | 日韩精品免费一级视频 | www.麻豆视频 | 狠狠做五月深爱婷婷天天综合 | 亚洲成色www久久网站 | 国产高清路线一路线二2022 | 黄漫免费观看 | 久久久久九九 | 久久精品成人免费看 | 美女逼逼喷水 | 久久中文字幕免费高清 | 亚洲国产精品线在线观看 | 99re在线精品视频免费 | 免费在线观看亚洲 | 免费一级特黄特色大片 | 亚洲视频免 | 日本漫画工囗全彩内番e绅 日本伦理动漫在线观看 | 青草网在线观看 | 91免费在线播放 | 亚洲天堂网在线观看视频 | 四虎国产 | 极品美女写真菠萝蜜视频 | 国产3p绿奴在线视频 |