欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

使用maven构建java9 service实例详解

程序员文章站 2022-06-30 23:43:20
序 本文主要研究下如何在maven里头构建java9 multi module及service实例 maven 整个工程跟传统maven多module的工程结构一样,...

本文主要研究下如何在maven里头构建java9 multi module及service实例

maven

整个工程跟传统maven多module的工程结构一样,java9的一个module对应maven project的一个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>

这里管理了一个maven-compiler-plugin,配置release为9,因为java9支持multi release,可以同时支持多个java版本,这里编译为java9版本。

service-sort

这个是service接口module

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

这里同时声明uses sortservice表示是它需要在这个module里头使用serviceloader去加载service实例

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的时候才会实例化
    sortservice service = providers.map(provider::get)
        .findany()
        .orelse(null);
    return service;
  }
}

这里在声明接口的同时,也增加了静态方法,用于加载service实例。

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>

这里添加对api包的依赖

module-info.java

module service.sort.bubble {
  requires service.sort;
  provides service.sort.sortservice with sort.impl.bubble.bubblesort;
}

这里声明了bubblesort提供了sortservice的实现

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接口的实现

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>

注意这里没有添加实现类的依赖

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");
  }
}

编译及运行

编译

mvn clean install

这里是在根目录下执行

使用bubble

复制代码 代码如下:

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

复制代码 代码如下:

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

两个service实现都添加

复制代码 代码如下:

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

或者

复制代码 代码如下:

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

发现貌似跟添加到path的顺序没有关系,即使把merge的jar包放在前面,也是使用bubble

小结

在java6的时候就已经有serviceloader了,不过那个时候是依赖在jar包的meta-inf/services目录下创建一个service接口全路径名称的文件,里头写上实现类的全路径名称。java9对在引入模块化后也支持在module-info.java里头声明service的提供方和消费者信息,这样模块系统可以支持serviceloader,不需要使用原来的meta-inf那种声明方式。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。