最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring條件注解@Conditional示例詳解

 更新時間:2019年08月06日 08:37:16   作者:java_lover  
這篇文章主要給大家介紹了關(guān)于Spring條件注解@Conditional的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Spring具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧

前言

@Conditional是Spring4新提供的注解,它的作用是根據(jù)某個條件創(chuàng)建特定的Bean,通過實現(xiàn)Condition接口,并重寫matches接口來構(gòu)造判斷條件。總的來說,就是根據(jù)特定條件來控制Bean的創(chuàng)建行為,這樣我們可以利用這個特性進(jìn)行一些自動的配置。

本文將分為三大部分,@Conditional源碼的介紹、Condition的使用示例和@Conditional的擴展注解的介紹。

一、@Conditional的源碼

@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Conditional {

  /**
   * All {@link Condition Conditions} that must {@linkplain Condition#matches match}
   * in order for the component to be registered.
   */
  Class<? extends Condition>[] value();

}

從源碼中可以看到,@Conditional注解可以用在類和方法上,需要傳入一個實現(xiàn)了Condition接口class數(shù)組。

二、代碼示例

下面將以不同的操作系統(tǒng)為條件,通過實現(xiàn)Condition接口,并重寫其matches方法來構(gòu)造判斷條件。若在Windows系統(tǒng)下運行程序,則輸出列表命令為dir;若在Linux系統(tǒng)下運行程序,則輸出列表命令為ls。

1.判斷條件類的定義

(1).判斷Windows的條件

package com.study.day01;

import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;

/**
 * @Auther: lifq
 * @Description:
 */
public class WindowsCondition implements Condition {
  public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
    return context.getEnvironment().getProperty("os.name").contains("Windows");
  }
}

(2).判斷Linux的條件

package com.study.day01;

import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;

/**
 * @Auther: lifq
 * @Description:
 */
public class LinuxCondition implements Condition {
  public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
    return context.getEnvironment().getProperty("os.name").contains("Linux");
  }
}

2.不同系統(tǒng)下的Bean的類

(1).接口代碼

package com.study.day01;

import org.springframework.stereotype.Service;

/**
 * @Auther: lifq
 * @Description:
 */
public interface ListService {

  public String showListCmd();

}

(2).Windows實現(xiàn)類代碼

package com.study.day01;

/**
 * @Auther: lifq
 * @Description:
 */
public class WindowsService implements ListService {
  public String showListCmd() {
    return "dir";
  }
}

(3).Linux實現(xiàn)類代碼

package com.study.day01;

/**
 * @Auther: lifq
 * @Description:
 */
public class LinuxService implements ListService {
  public String showListCmd() {
    return "ls";
  }
}

3.配置類

package com.study.day01;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;

/**
 * @Auther: lifq
 * @Description:
 */
@Configuration
public class Config {

  @Bean
  @Conditional(WindowsCondition.class)
  public ListService window() {
    return new WindowsService();
  }

  @Bean
  @Conditional(LinuxCondition.class)
  public ListService linux() {
    return new LinuxService();
  }
}

4.運行類

package com.study.day01;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * @Auther: lifq
 * @Description:
 */
public class Main01 {

  public static void main (String []args) {

    AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(Config.class);

    ListService listService = applicationContext.getBean(ListService.class);

    System.out.println(applicationContext.getEnvironment().getProperty("os.name") + "系統(tǒng)下的列表命令為:" + listService.showListCmd());
  }
}

我的是Windows操作系統(tǒng),輸出結(jié)果為dir,運行截圖如下:

運行截圖

@Conditional注解例子演示完成,有問題歡迎留言溝通哦!

完整源碼地址:https://github.com/suisui2019/springboot-study

三、@Conditional的擴展注解

  • @ConditionalOnBean:僅僅在當(dāng)前上下文中存在某個對象時,才會實例化一個Bean。
  • @ConditionalOnClass:某個class位于類路徑上,才會實例化一個Bean。
  • @ConditionalOnExpression:當(dāng)表達(dá)式為true的時候,才會實例化一個Bean。
  • @ConditionalOnMissingBean:僅僅在當(dāng)前上下文中不存在某個對象時,才會實例化一個Bean。
  • @ConditionalOnMissingClass:某個class類路徑上不存在的時候,才會實例化一個Bean。
  • @ConditionalOnNotWebApplication:不是web應(yīng)用,才會實例化一個Bean。
  • @ConditionalOnBean:當(dāng)容器中有指定Bean的條件下進(jìn)行實例化。
  • @ConditionalOnMissingBean:當(dāng)容器里沒有指定Bean的條件下進(jìn)行實例化。
  • @ConditionalOnClass:當(dāng)classpath類路徑下有指定類的條件下進(jìn)行實例化。
  • @ConditionalOnMissingClass:當(dāng)類路徑下沒有指定類的條件下進(jìn)行實例化。
  • @ConditionalOnWebApplication:當(dāng)項目是一個Web項目時進(jìn)行實例化。
  • @ConditionalOnNotWebApplication:當(dāng)項目不是一個Web項目時進(jìn)行實例化。
  • @ConditionalOnProperty:當(dāng)指定的屬性有指定的值時進(jìn)行實例化。
  • @ConditionalOnExpression:基于SpEL表達(dá)式的條件判斷。
  • @ConditionalOnJava:當(dāng)JVM版本為指定的版本范圍時觸發(fā)實例化。
  • @ConditionalOnResource:當(dāng)類路徑下有指定的資源時觸發(fā)實例化。
  • @ConditionalOnJndi:在JNDI存在的條件下觸發(fā)實例化。
  • @ConditionalOnSingleCandidate:當(dāng)指定的Bean在容器中只有一個,或者有多個但是指定了首選的Bean時觸發(fā)實例化。

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對腳本之家的支持。

相關(guān)文章

  • 解讀靜態(tài)資源訪問static-locations和static-path-pattern

    解讀靜態(tài)資源訪問static-locations和static-path-pattern

    本文主要介紹了Spring Boot中靜態(tài)資源的配置和訪問方式,包括靜態(tài)資源的默認(rèn)前綴、默認(rèn)地址、目錄結(jié)構(gòu)、訪問路徑以及靜態(tài)資源處理器的工作原理,通過配置文件和實現(xiàn)`WebMvcConfigurer`接口,可以自定義靜態(tài)資源目錄和訪問前綴
    2025-01-01
  • 編碼實現(xiàn)從無序鏈表中移除重復(fù)項(C和JAVA實例)

    編碼實現(xiàn)從無序鏈表中移除重復(fù)項(C和JAVA實例)

    如果不能使用臨時緩存,你怎么實現(xiàn)無序鏈表中移除重復(fù)項(?C和JAVA實例無序鏈表中移除重復(fù)項。
    2013-10-10
  • Java中string和int的互相轉(zhuǎn)換問題

    Java中string和int的互相轉(zhuǎn)換問題

    本文通過實例代碼給大家詳細(xì)介紹了Java中string和int的互相轉(zhuǎn)換問題,感興趣的朋友一起看看吧
    2017-10-10
  • 使用Apache Ignite實現(xiàn)Java數(shù)據(jù)網(wǎng)格

    使用Apache Ignite實現(xiàn)Java數(shù)據(jù)網(wǎng)格

    今天我們來探討如何使用Apache Ignite來實現(xiàn)Java數(shù)據(jù)網(wǎng)格,Apache Ignite是一個高性能的內(nèi)存計算平臺,它提供了分布式緩存、數(shù)據(jù)網(wǎng)格和計算功能,可以顯著提高大規(guī)模應(yīng)用的數(shù)據(jù)處理性能,感興趣的小伙伴跟著小編一起來看看吧
    2024-08-08
  • Spring Security如何在Servlet中執(zhí)行

    Spring Security如何在Servlet中執(zhí)行

    這篇文章主要介紹了Spring Security如何在Servlet中執(zhí)行,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-04-04
  • SpringBoot?Cache?二級緩存的使用

    SpringBoot?Cache?二級緩存的使用

    本文主要介紹了SpringBoot?Cache?二級緩存的使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-06-06
  • Java語言中Swing組件編程詳解

    Java語言中Swing組件編程詳解

    這篇文章主要為大家介紹了Java語言中Swing組件編程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-10-10
  • Java實時監(jiān)控日志文件并輸出的方法詳解

    Java實時監(jiān)控日志文件并輸出的方法詳解

    這篇文章主要給大家介紹了關(guān)于Java實時監(jiān)控日志文件并輸出的方法,文中通過示例代碼介紹的非常詳細(xì),對大家具有一定的參考學(xué)習(xí)價值,需要的朋友們下面跟著小編一起來學(xué)習(xí)學(xué)習(xí)吧。
    2017-06-06
  • SpringMVC框架實現(xiàn)上傳圖片的示例代碼

    SpringMVC框架實現(xiàn)上傳圖片的示例代碼

    本篇文章主要介紹了SpringMVC框架實現(xiàn)上傳圖片的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-09-09
  • Spring如何基于aop實現(xiàn)操作日志功能

    Spring如何基于aop實現(xiàn)操作日志功能

    這篇文章主要介紹了Spring如何基于aop實現(xiàn)操作日志功能,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-11-11

最新評論

枣庄市| 安丘市| 平原县| 津南区| 盈江县| 普兰店市| 巫溪县| 高邑县| 延吉市| 兴仁县| 格尔木市| 永善县| 天峻县| 佛坪县| 年辖:市辖区| 天祝| 宝坻区| 延安市| 温宿县| 九寨沟县| 青河县| 潜江市| 醴陵市| 南平市| 孙吴县| 常州市| 内黄县| 汝阳县| 沧州市| 九台市| 溆浦县| 溧阳市| 安顺市| 吴川市| 囊谦县| 灌南县| 汉源县| 云霄县| 惠州市| 丰台区| 资阳市|