您好,登录后才能下订单哦!
这篇文章主要介绍了如何使用maven构建java9 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
感谢你能够认真阅读完这篇文章,希望小编分享的“如何使用maven构建java9 service”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。