序
本文主要研究下如何在maven里头构建java9 multi module及service实例
maven
整个工程跟传统maven多module的工程结构一样,java9的一个module对应maven project的一个module。下面是根目录下的pom文件:
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">
4.0.0
com.example
java9-service-demo
0.0.1-SNAPSHOT
consumer-demo
service-sort
service-sort-bubble
service-sort-merge
pom
UTF-8
9
9
org.apache.maven.plugins
maven-compiler-plugin
3.6.1
9
这里管理了一个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 List sortList(List list);
public static SortService getProviderInstanceLazy() {
Stream> providers = ServiceLoader.load(SortService.class)
.stream();
//provider方法等到get的时候才会实例化
SortService service = providers.map(Provider::get)
.findAny()
.orElse(null);
return service;
}
}
这里在声明接口的同时,也增加了静态方法,用于加载service实例。
service-sort-bubble
maven
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">
java9-service-demo
com.example
0.0.1-SNAPSHOT
4.0.0
com.example
service-sort-bubble
jar
com.example
service-sort
0.0.1-SNAPSHOT
这里添加对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 List sortList(List 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 void swap(Listlist, int inner) {
T temp = list.get(inner);
list.set(inner, list.get(inner + 1));
list.set(inner + 1, temp);
}
}
service-sort-merge
maven
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">
java9-service-demo
com.example
0.0.1-SNAPSHOT
4.0.0
com.example
service-sort-merge
jar
com.example
service-sort
0.0.1-SNAPSHOT
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 List sortList(List list) {
System.out.println("using MergeSort");
Collections.sort(list);
return list;
}
}
consumer
maven
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">
java9-service-demo
com.example
0.0.1-SNAPSHOT
4.0.0
com.example
consumer-demo
jar
com.example
service-sort
0.0.1-SNAPSHOT
注意这里没有添加实现类的依赖
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 data = new ArrayList();
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那种声明方式。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。