1. <ul id="0c1fb"></ul>

      <noscript id="0c1fb"><video id="0c1fb"></video></noscript>
      <noscript id="0c1fb"><listing id="0c1fb"><thead id="0c1fb"></thead></listing></noscript>

      99热在线精品一区二区三区_国产伦精品一区二区三区女破破_亚洲一区二区三区无码_精品国产欧美日韩另类一区

      RELATEED CONSULTING
      相關咨詢
      選擇下列產(chǎn)品馬上在線溝通
      服務時間:8:30-17:00
      你可能遇到了下面的問題
      關閉右側工具欄

      新聞中心

      這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
      springboot中怎么利用bean實現(xiàn)條件裝配

      本篇文章為大家展示了springboot中怎么利用bean實現(xiàn)條件裝配,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。

      10余年的壽縣網(wǎng)站建設經(jīng)驗,針對設計、前端、開發(fā)、售后、文案、推廣等六對一服務,響應快,48小時及時工作處理。成都營銷網(wǎng)站建設的優(yōu)勢是能夠根據(jù)用戶設備顯示端的尺寸不同,自動調(diào)整壽縣建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設計,從而大程度地提升瀏覽體驗。成都創(chuàng)新互聯(lián)公司從事“壽縣網(wǎng)站設計”,“壽縣網(wǎng)站推廣”以來,每個客戶項目都認真落實執(zhí)行。

      條件裝配

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

      啥是條件裝配

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

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

      首先我們定義一個接口

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

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

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

      Java8的實現(xiàn)

      @Profile("Java8")@Servicepublic class Java8CalculateService implements CalculateService {  @Override  public Integer sum(Integer... values) {    System.out.println("Java 8 Lambda 實現(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")注解來表明對應的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));    // 關閉上下文    context.close();  }}

      使用基于@ConditionalOnSystemProperty注解的方式實現(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();}

      定義一個條件判斷的類,當這個類中的條件滿足是才會裝載bean,這個實現(xiàn)類實現(xiàn)org.springframework.context.annotation.Condition,AnnotatedTypeMetadata可以獲取的到注解中的name和value信息,假設我們現(xiàn)在實現(xiàn)判斷系統(tǒng)屬性和注解中的配置的一樣就加載bean,System.getProperty("user.name")獲取當前系統(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 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);    // 關閉上下文    context.close();  }}

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

      上述內(nèi)容就是springboot中怎么利用bean實現(xiàn)條件裝配,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注創(chuàng)新互聯(lián)行業(yè)資訊頻道。


      當前文章:springboot中怎么利用bean實現(xiàn)條件裝配
      URL地址:http://ef60e0e.cn/article/ppgpeh.html
      99热在线精品一区二区三区_国产伦精品一区二区三区女破破_亚洲一区二区三区无码_精品国产欧美日韩另类一区
      1. <ul id="0c1fb"></ul>

        <noscript id="0c1fb"><video id="0c1fb"></video></noscript>
        <noscript id="0c1fb"><listing id="0c1fb"><thead id="0c1fb"></thead></listing></noscript>

        宕昌县| 霍邱县| 营口市| 项城市| 循化| 巴南区| 长泰县| 诏安县| 武山县| 磐石市| 登封市| 夏津县| 宁津县| 怀来县| 应城市| 玉溪市| 涟源市| 霍林郭勒市| 苍溪县| 娄底市| 广德县| 南京市| 邳州市| 泸州市| 合水县| 清河县| 宁南县| 乾安县| 抚远县| 莎车县| 罗定市| 河北区| 大理市| 特克斯县| 利辛县| 永顺县| 清原| 新闻| 华容县| 仁寿县| 莱西市|