Use maven to build a Java 9 service instance.
Collation
This article mainly studies how to build Java 9 multi module and service instances in maven
Maven
The entire project has the same structure as the traditional maven multi-module project. A module of Java 9 corresponds to a module of the maven project. Below is the pom file under the root directory:
<? 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> ser Vice-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> <! -- Enable intellij idea to correctly compile java9, otherwise it will always change back to 1.5 --> <maven. compiler. source> 9 </maven. compiler. source> <maven.compiler.tar get> 9 </maven.compiler.tar get> </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>
A maven-compiler-plugin is managed here, and the release configuration is 9. Because java 9 supports multi release and multiple java versions are supported at the same time, it is compiled as java 9.
Service-sort
This is the service interface module.
module service.sort { exports service.sort; uses service.sort.SortService;}
The declaration of uses SortService indicates that it needs to use ServiceLoader in this module to load the service instance.
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 (); // The provider method will not instantiate SortService service = providers until get. map (Provider: get ). findAny (). orElse (null); return service ;}}
A static method is added for loading the service instance while declaring the interface.
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>
Add the dependency on the api package
Module-info.java
module service.sort.bubble { requires service.sort; provides service.sort.SortService with sort.impl.bubble.BubbleSort;}
Here we declare that BubbleSort provides SortService implementation.
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;}
Here we declare the implementation of MergeSort as the SortService Interface
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>
Note that no dependencies for implementation classes are added here.
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"); }}
Compile and run
Compile
mvn clean install
Run the following command in the root directory:
Use bubble
Copy codeThe Code is as follows:
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
Note that the bubble jar is added to module-path.
Output
Sort service consumer started.
Use BubbleSort
[2, 3, 5, 8, 10]
Finish
Use merge
Copy codeThe Code is as follows:
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
Note that the merge jar is added to module-path.
Output
Sort service consumer started.
Using MergeSort
[2, 3, 5, 8, 10]
Finish
Add both service implementations
Copy codeThe Code is as follows:
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
Or
Copy codeThe Code is as follows:
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
Output
Sort service consumer started.
Use BubbleSort
[2, 3, 5, 8, 10]
Finish
It seems that it has nothing to do with the order of adding to the path. Even if we put the merge jar package in front, we also use the bubble
Summary
ServiceLoader already exists in Java 6, but at that time it is dependent on the jar package's META-INF/services Directory to create a service interface full path name file, enter the full path name of the implementation class. Java9 after the introduction of modularization also supports declarations in the module-info.java service provider and consumer information, so that the module system can support ServiceLoader, do not need to use the original META-INF that way of declaration.
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.