钱柜娱乐手机客户端:module相关选项解析,service实例详解

钱柜娱乐手机客户端:module相关选项解析,service实例详解

选择maven营造java9 service实例详解,mavenjava9

本文首要研商下怎么在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里头使用瑟维斯Loader去加载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

ca88手机版登录网页 ,八个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这种注脚情势。

上述正是本文的全体内容,希望对大家的学习抱有助于,也冀望咱们多多照管帮客之家。

service实例详解,mavenjava9 序
本文首要钻探下怎么在maven里头构建java9 multi module及service实例 maven
整个工程跟古板maven多mod…

编写翻译及运维

  • 编译

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

–add-exports(编译时+运行时)

相称javac使用的时候尽管允许编写翻译时能够访谈

javac -d outputDir --add-exports java.base/sun.net=com.example --module-source-path src $(find . -name "*.java")

合作java使用的时候便是允许运转时能够访问

java --module-path outputDir --add-exports java.base/sun.net=com.example -m com.example/com.example.Main

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实例。

–limit-modules

用于降低模块范围,比方

java --limit-modules java.se.ee --list-modules
java.activation@9
java.base@9
java.compiler@9
java.corba@9
java.datatransfer@9
java.desktop@9
java.instrument@9
java.logging@9
java.management@9
java.management.rmi@9
java.naming@9
java.prefs@9
java.rmi@9
java.scripting@9
java.se@9
java.se.ee@9
java.security.jgss@9
java.security.sasl@9
java.sql@9
java.sql.rowset@9
java.transaction@9
java.xml@9
java.xml.bind@9
java.xml.crypto@9
java.xml.ws@9
java.xml.ws.annotation@9
jdk.httpserver@9
jdk.unsupported@9

用来界定编写翻译及运营时得以使用的模块,限定的限量是main
module,–add-modules增添的modules,以及该参数钦命的modules及其transitive依赖的modules
当包含main方法的module是unnamed
modules的时候,常常用这几个参数来收缩要求被剖析的模块数量。

小结

在java6的时候就已经有ServiceLoader了,不过那个时候是信赖在jar包的META-INF/services目录下成立一个service接口全路径名称的公文,里头写上贯彻类的全路线名称。java9对在引进模块化后也支撑在module-info.java里头注解service的提供方和客商信息,那样模块系统能够帮助ServiceLoader,无需运用原本的META-INF这种注解格局。

jmod

用来成立、查看jmod文件

cd $JAVA_HOME
➜  Home ./bin/jmod list ./jmods/java.logging.jmod
classes/module-info.class
classes/java/util/logging/ConsoleHandler.class
classes/java/util/logging/ErrorManager.class
classes/java/util/logging/FileHandler$1.class
classes/java/util/logging/FileHandler$InitializationErrorManager.class
classes/java/util/logging/FileHandler$MeteredStream.class
classes/java/util/logging/FileHandler.class
classes/java/util/logging/Filter.class
classes/java/util/logging/Formatter.class
classes/java/util/logging/Handler$1.class
classes/java/util/logging/Handler.class
classes/java/util/logging/Level$1.class
classes/java/util/logging/Level$KnownLevel.class
classes/java/util/logging/Level$RbAccess.class
classes/java/util/logging/Level.class
classes/java/util/logging/Logger$1.class
classes/java/util/logging/Logger$ConfigurationData.class
classes/java/util/logging/Logger$LoggerBundle.class
classes/java/util/logging/Logger$RbAccess.class
classes/java/util/logging/Logger$SystemLoggerHelper$1.class
classes/java/util/logging/Logger$SystemLoggerHelper.class
classes/java/util/logging/Logger.class
classes/java/util/logging/Logging.class
classes/java/util/logging/LoggingMXBean.class
classes/java/util/logging/LoggingPermission.class
classes/java/util/logging/LogManager$1.class
classes/java/util/logging/LogManager$2.class
classes/java/util/logging/LogManager$3.class
classes/java/util/logging/LogManager$4.class
classes/java/util/logging/LogManager$5.class
classes/java/util/logging/LogManager$6.class
classes/java/util/logging/LogManager$7.class
classes/java/util/logging/LogManager$Cleaner.class
classes/java/util/logging/LogManager$CloseOnReset.class
classes/java/util/logging/LogManager$ConfigProperty.class
classes/java/util/logging/LogManager$LoggerContext$1.class
classes/java/util/logging/LogManager$LoggerContext.class
classes/java/util/logging/LogManager$LoggerWeakRef.class
classes/java/util/logging/LogManager$LoggingProviderAccess.class
classes/java/util/logging/LogManager$LogNode.class
classes/java/util/logging/LogManager$ModType.class
classes/java/util/logging/LogManager$RootLogger.class
classes/java/util/logging/LogManager$SystemLoggerContext.class
classes/java/util/logging/LogManager$VisitedLoggers.class
classes/java/util/logging/LogManager.class
classes/java/util/logging/LogRecord$CallerFinder.class
classes/java/util/logging/LogRecord.class
classes/java/util/logging/MemoryHandler.class
classes/java/util/logging/SimpleFormatter.class
classes/java/util/logging/SocketHandler.class
classes/java/util/logging/StreamHandler$1.class
classes/java/util/logging/StreamHandler.class
classes/java/util/logging/XMLFormatter.class
classes/sun/net/www/protocol/http/logging/HttpLogFormatter.class
classes/sun/util/logging/internal/LoggingProviderImpl$JULWrapper.class
classes/sun/util/logging/internal/LoggingProviderImpl$LogManagerAccess.class
classes/sun/util/logging/internal/LoggingProviderImpl.class
classes/sun/util/logging/resources/logging.class
classes/sun/util/logging/resources/logging_de.class
classes/sun/util/logging/resources/logging_es.class
classes/sun/util/logging/resources/logging_fr.class
classes/sun/util/logging/resources/logging_it.class
classes/sun/util/logging/resources/logging_ja.class
classes/sun/util/logging/resources/logging_ko.class
classes/sun/util/logging/resources/logging_pt_BR.class
classes/sun/util/logging/resources/logging_sv.class
classes/sun/util/logging/resources/logging_zh_CN.class
classes/sun/util/logging/resources/logging_zh_HK.class
classes/sun/util/logging/resources/logging_zh_TW.class
conf/logging.properties
legal/COPYRIGHT
legal/LICENSE
➜  Home ./bin/jmod describe ./jmods/java.logging.jmod
java.logging@9
exports java.util.logging
requires java.base mandated
provides jdk.internal.logger.DefaultLoggerFinder with sun.util.logging.internal.loggingproviderimpl
contains sun.net.www.protocol.http.logging
contains sun.util.logging.internal
contains sun.util.logging.resources
platform macos-amd64

本文首要切磋下哪些在maven里头构建java9 multi module及service实例

doc

  • What is the significance of java.se module in Java
    9?
  • Package
    java.lang.module
  • Java 9 Modules – The Root
    Modules
  • Five Command Line Options To Hack The Java 9 Module
    System
  • Java 9 揭秘(2.
    模块化系统)

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

–add-modules

–add-modules增加module到root set中,方便解析模块依赖

– ALL-DEFAULT,添加unnamed modules的默认root module添加到root set

ALL-SYSTEM,增加系统模块(java,jdk,javafx等起始的模块,即jdk本身带的模块)到root
set

  • ALL-MODULE,加多module-path上全体可读的module到root set

doc

  • Slim modular Java 9 runtime Docker image with Alpine
    Linux
  • 怎么着在Maven项目中装置Java
    9

小结

弄明白modules的分类及其相关选项,能够推动大家越来越好地利用java9的模块系统。

admin

网站地图xml地图