使用maven构建java9 service实例详解

2025-05-29 0 36

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

maven

整个工程跟传统maven多module的工程结构一样,java9的一个module对应maven project的一个module。下面是根目录下的pom文件:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42
<?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

?

1

2

3

4
module service.sort {

exports service.sort;

uses service.sort.SortService;

}

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

?

1

2

3

4

5

6

7

8

9

10

11

12

13
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

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23
<?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

?

1

2

3

4
module service.sort.bubble {

requires service.sort;

provides service.sort.SortService with sort.impl.bubble.BubbleSort;

}

这里声明了BubbleSort提供了SortService的实现

BubbleSort

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20
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

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23
<?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

?

1

2

3

4
module service.sort.merge {

requires service.sort;

provides service.sort.SortService with sort.impl.merge.MergeSort;

}

这里声明了MergeSort为SortService接口的实现

MergeSort

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14
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

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23
<?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

?

1

2

3
module consumer {

requires service.sort;

}

Main

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

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

}

}

编译及运行

编译

?

1
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那种声明方式。

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

原文链接:https://juejin.im/post/5a9269c85188257a84625450

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 使用maven构建java9 service实例详解 https://www.kuaiidc.com/112520.html

相关文章

发表评论
暂无评论