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

使用maven構(gòu)建java9 service實(shí)例詳解

 更新時(shí)間:2018年02月26日 11:02:39   作者:go4it  
本篇文章主要介紹了使用maven構(gòu)建java9 service實(shí)例詳解,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

本文主要研究下如何在maven里頭構(gòu)建java9 multi module及service實(shí)例

maven

整個(gè)工程跟傳統(tǒng)maven多module的工程結(jié)構(gòu)一樣,java9的一個(gè)module對(duì)應(yīng)maven project的一個(gè)module。下面是根目錄下的pom文件:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

  <modelVersion>4.0.0</modelVersion>

  <groupId>com.example</groupId>
  <artifactId>java9-service-demo</artifactId>
  <version>0.0.1-SNAPSHOT</version>

  <modules>
    <module>consumer-demo</module>
    <module>service-sort</module>
    <module>service-sort-bubble</module>
    <module>service-sort-merge</module>
  </modules>
  <packaging>pom</packaging>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <!--讓intellij能夠正確編譯java9,不然老是變回使用1.5-->
    <maven.compiler.source>9</maven.compiler.source>
    <maven.compiler.target>9</maven.compiler.target>
  </properties>

  <build>
    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.6.1</version>
          <configuration>
            <release>9</release>
          </configuration>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>

</project>

這里管理了一個(gè)maven-compiler-plugin,配置release為9,因?yàn)閖ava9支持multi release,可以同時(shí)支持多個(gè)java版本,這里編譯為java9版本。

service-sort

這個(gè)是service接口module

module service.sort {
  exports service.sort;
  uses service.sort.SortService;
}

這里同時(shí)聲明uses SortService表示是它需要在這個(gè)module里頭使用ServiceLoader去加載service實(shí)例

public interface SortService {
  public <T extends Comparable> List<T> sortList(List<T> list);

  public static SortService getProviderInstanceLazy() {
    Stream<Provider<SortService>> providers = ServiceLoader.load(SortService.class)
        .stream();
    //provider方法等到get的時(shí)候才會(huì)實(shí)例化
    SortService service = providers.map(Provider::get)
        .findAny()
        .orElse(null);
    return service;
  }
}

這里在聲明接口的同時(shí),也增加了靜態(tài)方法,用于加載service實(shí)例。

service-sort-bubble

maven

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <parent>
    <artifactId>java9-service-demo</artifactId>
    <groupId>com.example</groupId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.example</groupId>
  <artifactId>service-sort-bubble</artifactId>
  <packaging>jar</packaging>

  <dependencies>
    <dependency>
      <groupId>com.example</groupId>
      <artifactId>service-sort</artifactId>
      <version>0.0.1-SNAPSHOT</version>
    </dependency>
  </dependencies>
</project>

這里添加對(duì)api包的依賴

module-info.java

module service.sort.bubble {
  requires service.sort;
  provides service.sort.SortService with sort.impl.bubble.BubbleSort;
}

這里聲明了BubbleSort提供了SortService的實(shí)現(xiàn)

BubbleSort

public class BubbleSort implements SortService {

  public <T extends Comparable> List<T> sortList(List<T> list) {
    System.out.println("use BubbleSort");
    for (int outer = 0; outer < list.size() - 1; outer++) {
      for (int inner = 0; inner < list.size()-outer-1; inner++) {
        if (list.get(inner).compareTo(list.get(inner + 1)) > 0) {
          swap(list, inner);
        }
      }
    }
    return list;
  }

  private <T> void swap(List<T>list, int inner) {
    T temp = list.get(inner);
    list.set(inner, list.get(inner + 1));
    list.set(inner + 1, temp);
  }
}

service-sort-merge

maven

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <parent>
    <artifactId>java9-service-demo</artifactId>
    <groupId>com.example</groupId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.example</groupId>
  <artifactId>service-sort-merge</artifactId>
  <packaging>jar</packaging>

  <dependencies>
    <dependency>
      <groupId>com.example</groupId>
      <artifactId>service-sort</artifactId>
      <version>0.0.1-SNAPSHOT</version>
    </dependency>
  </dependencies>
</project>

module-info.java

module service.sort.merge {
  requires service.sort;
  provides service.sort.SortService with sort.impl.merge.MergeSort;
}

這里聲明了MergeSort為SortService接口的實(shí)現(xiàn)

MergeSort

import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Arrays;
import service.sort.SortService;

public class MergeSort implements SortService {

  public <T extends Comparable> List<T> sortList(List<T> list) {
    System.out.println("using MergeSort");
    Collections.sort(list);
    return list;
  }
}

consumer

maven

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <parent>
    <artifactId>java9-service-demo</artifactId>
    <groupId>com.example</groupId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.example</groupId>
  <artifactId>consumer-demo</artifactId>
  <packaging>jar</packaging>

  <dependencies>
    <dependency>
      <groupId>com.example</groupId>
      <artifactId>service-sort</artifactId>
      <version>0.0.1-SNAPSHOT</version>
    </dependency>
  </dependencies>
</project>

注意這里沒有添加實(shí)現(xiàn)類的依賴

module-info.java

module consumer {
  requires service.sort;
}

Main

public class Main {

  public static void main(String[] args) {

    System.out.println("sort service consumer started.");
    List<Integer> data = new ArrayList<Integer>();
    data.add(5);
    data.add(3);
    data.add(10);
    data.add(2);
    data.add(8);

    SortService sortService = SortService.getProviderInstanceLazy();
    if (sortService != null) {
      sortService.sortList(data);
    }
    System.out.println(data);
    System.out.println("finish");
  }
}

編譯及運(yùn)行

編譯

mvn clean install

這里是在根目錄下執(zhí)行

使用bubble

復(fù)制代碼 代碼如下:

java --module-path ./consumer-demo/target/consumer-demo-0.0.1-SNAPSHOT.jar:./service-sort/target/service-sort-0.0.1-SNAPSHOT.jar:./service-sort-bubble/target/service-sort-bubble-0.0.1-SNAPSHOT.jar --module consumer/consumer.Main

注意這里添加了bubble的jar到module-path

輸出

sort service consumer started.
use BubbleSort
[2, 3, 5, 8, 10]
finish

使用merge

復(fù)制代碼 代碼如下:

java --module-path ./consumer-demo/target/consumer-demo-0.0.1-SNAPSHOT.jar:./service-sort/target/service-sort-0.0.1-SNAPSHOT.jar:./service-sort-merge/target/service-sort-merge-0.0.1-SNAPSHOT.jar --module consumer/consumer.Main

注意這里添加了merge的jar到module-path

輸出

sort service consumer started.
using MergeSort
[2, 3, 5, 8, 10]
finish

兩個(gè)service實(shí)現(xiàn)都添加

復(fù)制代碼 代碼如下:

java --module-path ./consumer-demo/target/consumer-demo-0.0.1-SNAPSHOT.jar:./service-sort/target/service-sort-0.0.1-SNAPSHOT.jar:./service-sort-bubble/target/service-sort-bubble-0.0.1-SNAPSHOT.jar:./service-sort-merge/target/service-sort-merge-0.0.1-SNAPSHOT.jar --module consumer/consumer.Main

或者

復(fù)制代碼 代碼如下:

java --module-path ./consumer-demo/target/consumer-demo-0.0.1-SNAPSHOT.jar:./service-sort/target/service-sort-0.0.1-SNAPSHOT.jar:./service-sort-merge/target/service-sort-merge-0.0.1-SNAPSHOT.jar:./service-sort-bubble/target/service-sort-bubble-0.0.1-SNAPSHOT.jar --module consumer/consumer.Main

輸出

sort service consumer started.
use BubbleSort
[2, 3, 5, 8, 10]
finish

發(fā)現(xiàn)貌似跟添加到path的順序沒有關(guān)系,即使把merge的jar包放在前面,也是使用bubble

小結(jié)

在java6的時(shí)候就已經(jīng)有ServiceLoader了,不過那個(gè)時(shí)候是依賴在jar包的META-INF/services目錄下創(chuàng)建一個(gè)service接口全路徑名稱的文件,里頭寫上實(shí)現(xiàn)類的全路徑名稱。java9對(duì)在引入模塊化后也支持在module-info.java里頭聲明service的提供方和消費(fèi)者信息,這樣模塊系統(tǒng)可以支持ServiceLoader,不需要使用原來的META-INF那種聲明方式。

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

相關(guān)文章

  • Java解密微信小程序手機(jī)號(hào)的方法

    Java解密微信小程序手機(jī)號(hào)的方法

    這篇文章主要為大家詳細(xì)介紹了Java解密微信小程序手機(jī)號(hào)的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-12-12
  • JAVA堆排序算法的講解

    JAVA堆排序算法的講解

    這篇文章主要介紹了JAVA堆排序算法的知識(shí)點(diǎn),文中代碼非常詳細(xì),配合上圖片講解,幫助大家更好的參考和學(xué)習(xí),感興趣的朋友可以了解下
    2020-06-06
  • Spring boot2X Consul如何使用Feign實(shí)現(xiàn)服務(wù)調(diào)用

    Spring boot2X Consul如何使用Feign實(shí)現(xiàn)服務(wù)調(diào)用

    這篇文章主要介紹了spring boot2X Consul如何使用Feign實(shí)現(xiàn)服務(wù)調(diào)用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-12-12
  • Java中漢字轉(zhuǎn)拼音pinyin4j用法實(shí)例分析

    Java中漢字轉(zhuǎn)拼音pinyin4j用法實(shí)例分析

    這篇文章主要介紹了Java中漢字轉(zhuǎn)拼音pinyin4j用法,結(jié)合實(shí)例形式較為詳細(xì)的分析了pinyin4j庫的具體使用技巧,需要的朋友可以參考下
    2015-12-12
  • Java設(shè)計(jì)模式之代理模式原理及實(shí)現(xiàn)代碼分享

    Java設(shè)計(jì)模式之代理模式原理及實(shí)現(xiàn)代碼分享

    這篇文章主要介紹了Java設(shè)計(jì)模式之代理模式原理及實(shí)現(xiàn)代碼分享,設(shè)計(jì)代理模式的定義,靜態(tài)代理,動(dòng)態(tài)代理,jdk動(dòng)態(tài)代理實(shí)現(xiàn)步驟,原理及源碼等相關(guān)內(nèi)容,具有一定參考價(jià)值,需要的朋友可以了解下。
    2017-11-11
  • Spring Boot打jar包后配置文件的外部?jī)?yōu)化配置方法

    Spring Boot打jar包后配置文件的外部?jī)?yōu)化配置方法

    這篇文章主要介紹了Spring Boot打jar包后配置文件的外部?jī)?yōu)化配置方法,需要的朋友可以參考下
    2018-02-02
  • 超全MyBatis動(dòng)態(tài)代理詳解(絕對(duì)干貨)

    超全MyBatis動(dòng)態(tài)代理詳解(絕對(duì)干貨)

    這篇文章主要介紹了超全MyBatis動(dòng)態(tài)代理詳解(絕對(duì)干貨),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-02-02
  • Spring實(shí)戰(zhàn)之使用注解實(shí)現(xiàn)聲明式事務(wù)操作示例

    Spring實(shí)戰(zhàn)之使用注解實(shí)現(xiàn)聲明式事務(wù)操作示例

    這篇文章主要介紹了Spring實(shí)戰(zhàn)之使用注解實(shí)現(xiàn)聲明式事務(wù)操作,結(jié)合實(shí)例形式詳細(xì)分析了spring使用注解實(shí)現(xiàn)聲明式事務(wù)相關(guān)配置、接口實(shí)現(xiàn)與使用技巧,需要的朋友可以參考下
    2020-01-01
  • Java 淺談 高并發(fā) 處理方案詳解

    Java 淺談 高并發(fā) 處理方案詳解

    這篇文章主要介紹了淺談Java高并發(fā)解決方案以及高負(fù)載優(yōu)化方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • 聊聊Lombok中的@Builder注解使用教程

    聊聊Lombok中的@Builder注解使用教程

    @Builder注解的作用主要是用來生成對(duì)象,并且可以為對(duì)象鏈?zhǔn)劫x值。接下來通過本文給大家介紹Lombok中的@Builder注解使用教程,感興趣的朋友一起看看吧
    2021-11-11

最新評(píng)論