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

spring boot中的條件裝配bean的實(shí)現(xiàn)

 更新時(shí)間:2019年12月17日 08:28:37   作者:一號(hào)線  
這篇文章主要介紹了spring boot中的條件裝配bean的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

條件裝配

從Spring Framework 3.1開始,允許在Bean裝配時(shí)增加前置條件判斷。

啥是條件裝配

在bean裝配前的條件判斷。比如@Profile(是在spring3.1中引入),@Contditional(spring4.0中引入)
實(shí)現(xiàn)方式:注解方式,編程方式。

假設(shè)我們現(xiàn)在有一個(gè)多數(shù)據(jù)求和計(jì)算的小需求,定義兩種方式Java7和Java8,然后使用條件裝配的方式分別裝配不同的bean。

首先我們定義一個(gè)接口

public interface CalculateService {

  /**
   * 從多個(gè)整數(shù) sum 求和
   * @param values 多個(gè)整數(shù)
   * @return sum 累加值
   */
  Integer sum(Integer... values);
}

其次是兩種不同的實(shí)現(xiàn)方式,Java7的方式

@Profile("Java7")
@Service
public class Java7CalculateService implements CalculateService {

  @Override
  public Integer sum(Integer... values) {
    System.out.println("Java 7 for 循環(huán)實(shí)現(xiàn) ");
    int sum = 0;
    for (int i = 0; i < values.length; i++) {
      sum += values[i];
    }
    return sum;
  }

}

Java8的實(shí)現(xiàn)

@Profile("Java8")
@Service
public class Java8CalculateService implements CalculateService {

  @Override
  public Integer sum(Integer... values) {
    System.out.println("Java 8 Lambda 實(shí)現(xiàn)");
    int sum = Stream.of(values).reduce(0, Integer::sum);
    return sum;
  }

  public static void main(String[] args) {
    CalculateService calculateService = new Java8CalculateService();
    System.out.println(calculateService.sum(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
  }

}

我們在這里使用了@Profile("Java8")注解來表明對應(yīng)的profile。然后我定義一個(gè)啟動(dòng)類在里面配置裝配哪一個(gè)bean

@SpringBootApplication(scanBasePackages = "com.service")
public class CalculateServiceBootstrap {

  public static void main(String[] args) {
    ConfigurableApplicationContext context = new SpringApplicationBuilder(CalculateServiceBootstrap.class)
        .web(WebApplicationType.NONE)
        .profiles("Java8")
        .run(args);

    // CalculateService Bean 是否存在
    CalculateService calculateService = context.getBean(CalculateService.class);

    System.out.println("calculateService.sum(1...10) : " +
        calculateService.sum(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));

    // 關(guān)閉上下文
    context.close();
  }
}

使用基于@ConditionalOnSystemProperty注解的方式實(shí)現(xiàn)。

首先我們定義一個(gè)注解,這里定義了一個(gè)屬性和一個(gè)值。

/**
 * Java 系統(tǒng)屬性 條件判斷
 *
 */
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE, ElementType.METHOD })
@Documented
@Conditional(OnSystemPropertyCondition.class)
public @interface ConditionalOnSystemProperty {

  /**
   * Java 系統(tǒng)屬性名稱
   * @return
   */
  String name();

  /**
   * Java 系統(tǒng)屬性值
   * @return
   */
  String value();
}

定義一個(gè)條件判斷的類,當(dāng)這個(gè)類中的條件滿足是才會(huì)裝載bean,這個(gè)實(shí)現(xiàn)類實(shí)現(xiàn)org.springframework.context.annotation.Condition,AnnotatedTypeMetadata可以獲取的到注解中的name和value信息,假設(shè)我們現(xiàn)在實(shí)現(xiàn)判斷系統(tǒng)屬性和注解中的配置的一樣就加載bean,System.getProperty("user.name")獲取當(dāng)前系統(tǒng)下的用戶名,我的mac創(chuàng)建的用戶名叫yanghongxing,如果我們在注解中配置的value是yanghongxing則裝載這個(gè)bean。

/**
 * 系統(tǒng)屬性條件判斷
 *
 */
public class OnSystemPropertyCondition implements Condition {

  @Override
  public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {

    Map<String, Object> attributes = metadata.getAnnotationAttributes(ConditionalOnSystemProperty.class.getName());

    String propertyName = String.valueOf(attributes.get("name"));

    String propertyValue = String.valueOf(attributes.get("value"));

    String javaPropertyValue = System.getProperty(propertyName);

    return propertyValue.equals(javaPropertyValue);
  }
}

最后在啟動(dòng)類中啟動(dòng)

public class ConditionalOnSystemPropertyBootstrap {

  @Bean
  @ConditionalOnSystemProperty(name = "user.name", value = "yanghongxing")
  public String helloWorld() {
    return "Hello,World Honson";
  }

  public static void main(String[] args) {
    ConfigurableApplicationContext context = new SpringApplicationBuilder(ConditionalOnSystemPropertyBootstrap.class)
        .web(WebApplicationType.NONE)
        .run(args);
    // 通過名稱和類型獲取 helloWorld Bean
    String helloWorld = context.getBean("helloWorld", String.class);

    System.out.println("helloWorld Bean : " + helloWorld);

    // 關(guān)閉上下文
    context.close();
  }
}

我們可以在OnSystemPropertyCondition實(shí)現(xiàn)復(fù)雜的裝載類的條件,判斷是否裝載某個(gè)bean。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • java8中的lambda表達(dá)式簡介

    java8中的lambda表達(dá)式簡介

    Lambda表達(dá)式類似匿名函數(shù),簡單地說,它是沒有聲明的方法,也即沒有訪問修飾符、返回值聲明和方法名,這篇文章主要介紹了java8?中的lambda表達(dá)式簡介,需要的朋友可以參考下
    2022-06-06
  • Java設(shè)計(jì)模式之java組合模式詳解

    Java設(shè)計(jì)模式之java組合模式詳解

    這篇文章主要介紹了JAVA設(shè)計(jì)模式之組合模式,簡單說明了組合模式的原理,并結(jié)合實(shí)例分析了java組合模式的具體用法,需要的朋友可以參考下
    2021-09-09
  • 詳解SpringCloud微服務(wù)之Rest

    詳解SpringCloud微服務(wù)之Rest

    今天帶大家學(xué)習(xí)SpringCloud微服務(wù)之Rest的有關(guān)知識(shí),文中有非常詳細(xì)的介紹及代碼示例,對正在學(xué)習(xí)java的小伙伴們有很好的幫助,需要的朋友可以參考下
    2021-05-05
  • kill命令在Java應(yīng)用中使用的注意事項(xiàng)小結(jié)

    kill命令在Java應(yīng)用中使用的注意事項(xiàng)小結(jié)

    這篇文章主要給大家介紹了關(guān)于kill命令在Java應(yīng)用中使用的注意事項(xiàng),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-06-06
  • 淺談Java并發(fā)中ReentrantLock鎖應(yīng)該怎么用

    淺談Java并發(fā)中ReentrantLock鎖應(yīng)該怎么用

    本文主要介紹了ava并發(fā)中ReentrantLock鎖的具體使用,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-11-11
  • SpringBoot實(shí)現(xiàn)釘釘機(jī)器人消息推送的示例代碼

    SpringBoot實(shí)現(xiàn)釘釘機(jī)器人消息推送的示例代碼

    這篇文章主要介紹了SpringBoot實(shí)現(xiàn)釘釘機(jī)器人消息推送的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-03-03
  • Spring Security Remember me使用及原理詳解

    Spring Security Remember me使用及原理詳解

    這篇文章主要介紹了Spring Security Remember me使用及原理詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-09-09
  • Spring的@PropertySource注解源碼解析

    Spring的@PropertySource注解源碼解析

    這篇文章主要介紹了Spring的@PropertySource注解源碼解析,就以源碼時(shí)序圖的方式,直觀的感受下@PropertySource注解在Spring源碼層面的執(zhí)行流程,需要的朋友可以參考下
    2023-11-11
  • 詳解如何實(shí)現(xiàn)SpringBoot的底層注解

    詳解如何實(shí)現(xiàn)SpringBoot的底層注解

    今天給大家?guī)淼奈恼率侨绾螌?shí)現(xiàn)SpringBoot的底層注解,文中有非常詳細(xì)的介紹及代碼示例,對正在學(xué)習(xí)java的小伙伴很有幫助,需要的朋友可以參考下
    2021-06-06
  • Java中 this和super的用法與區(qū)別小結(jié)

    Java中 this和super的用法與區(qū)別小結(jié)

    在Java的學(xué)習(xí)與開發(fā)者我們經(jīng)常遇到this和super關(guān)鍵字,本文主要介紹了Java中 this和super的用法與區(qū)別小結(jié),具有一定的參考價(jià)值,感興趣的可以了解一下
    2023-12-12

最新評(píng)論

连南| 本溪市| 姜堰市| 蒲城县| 清水河县| 永泰县| 上栗县| 峡江县| 临武县| 三门峡市| 万年县| 沛县| 花莲市| 米林县| 毕节市| 吴江市| 肥乡县| 辉县市| 山西省| 永城市| 临潭县| 易门县| 辽阳市| 云龙县| 嘉定区| 揭西县| 庆城县| 同江市| 铜梁县| 麻栗坡县| 阿鲁科尔沁旗| 莒南县| 晋宁县| 富锦市| 利辛县| 绥芬河市| 建阳市| 苗栗市| 博野县| 靖州| 皮山县|