欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

條件裝配

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

啥是條件裝配

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

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

首先我們定義一個接口

public interface CalculateService {

  /**
   * 從多個整數(shù) sum 求和
   * @param values 多個整數(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。然后我定義一個啟動類在里面配置裝配哪一個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)。

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

/**
 * 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();
}

定義一個條件判斷的類,當(dāng)這個類中的條件滿足是才會裝載bean,這個實(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則裝載這個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);
  }
}

最后在啟動類中啟動

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ù)雜的裝載類的條件,判斷是否裝載某個bean。

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

相關(guān)文章

  • SpringBoot開發(fā)之整合Mybatis詳解

    SpringBoot開發(fā)之整合Mybatis詳解

    這篇文章主要介紹了SpringBoot開發(fā)之整合Mybatis詳解,MyBatis是一個半自動的ORM框架,它允許我們通過編寫SQL語句來操作數(shù)據(jù)庫,使用MyBatis,我們可以通過定義映射文件(XML文件)或使用注解的方式將Java對象與數(shù)據(jù)庫表進(jìn)行映射,需要的朋友可以參考下
    2023-09-09
  • Java基本語法筆記(菜鳥必看篇)

    Java基本語法筆記(菜鳥必看篇)

    下面小編就為大家?guī)硪黄狫ava基本語法筆記(菜鳥必看篇)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-05-05
  • DoytoQuery 聚合查詢方案示例詳解

    DoytoQuery 聚合查詢方案示例詳解

    這篇文章主要為大家介紹了DoytoQuery 聚合查詢方案示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-12-12
  • SpringBoot SSE服務(wù)端主動推送事件的實(shí)現(xiàn)

    SpringBoot SSE服務(wù)端主動推送事件的實(shí)現(xiàn)

    本文主要介紹了SpringBoot SSE服務(wù)端主動推送事件的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-06-06
  • Java集合系列之LinkedHashMap源碼分析

    Java集合系列之LinkedHashMap源碼分析

    這篇文章主要為大家詳細(xì)介紹了Java集合系列之LinkedHashMap源碼分析,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-02-02
  • Spring發(fā)送郵件如何內(nèi)嵌圖片增加附件

    Spring發(fā)送郵件如何內(nèi)嵌圖片增加附件

    這篇文章主要介紹了Spring發(fā)送郵件如何內(nèi)嵌圖片增加附件,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-10-10
  • Spring中的AutowireCandidateResolver的具體使用詳解

    Spring中的AutowireCandidateResolver的具體使用詳解

    這篇文章主要介紹了Spring中的AutowireCandidateResolver的具體使用詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-04-04
  • Spring實(shí)現(xiàn)文件上傳(示例代碼)

    Spring實(shí)現(xiàn)文件上傳(示例代碼)

    Spring可以繼承commons-fileupload插件來實(shí)現(xiàn)文件上傳的功能。分為前端JSP編寫和后臺Controller的編寫
    2013-10-10
  • 深入理解java中的synchronized關(guān)鍵字

    深入理解java中的synchronized關(guān)鍵字

    這篇文章主要介紹了java中的synchronized關(guān)鍵字,有需要的朋友可以參考一下
    2013-12-12
  • java創(chuàng)建二維碼并賦予url鏈接的功能實(shí)現(xiàn)

    java創(chuàng)建二維碼并賦予url鏈接的功能實(shí)現(xiàn)

    這篇文章給大家分享java創(chuàng)建二維碼并賦予url鏈接的功能實(shí)現(xiàn),需要獲取要賦值給二維碼的鏈接后綴,通過設(shè)置二維碼的訪問路徑等一系列操作,具體實(shí)現(xiàn)代碼跟隨小編一起看看吧
    2021-06-06

最新評論