sequence
This article mainly studies how to build java9 multi module and service examples in maven
maven
The entire project is the same as the traditional maven multi-module engineering structure. A module in java9 corresponds to a module in the maven project. Here is the pom file in 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>service-sort</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> <!--Let Intellij compile java9 correctly, otherwise it will always be changed back to use 1.5--> <maven.compiler.source>9</maven.compiler.source> <maven.compiler.target>9</maven.compiler.target> </properties> <build> <pluginManagement> <plugins> <plugins> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.6.1</version> <configuration> <release>9</release> </configuration> </plugins> </pluginManagement> </build></project>
Here is a maven-compiler-plugin managed, and the release is configured to be 9. Because java9 supports multi release, it can support multiple Java versions at the same time. It is compiled as java9 version here.
service-sort
This is the service interface module
module service.sort { exports service.sort; uses service.sort.SortService;} Here, the use SortService is declared that it needs to use ServiceLoader to load the service instance in this module.
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 method will not instantiate until get SortService service = providers.map(Provider::get) .findAny() .orElse(null); return service; }} While declaring the interface, static methods are also added to load service instances.
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 dependencies on the API package here
module-info.java
module service.sort.bubble { require service.sort; provide service.sort.SortService with sort.impl.bubble.BubbleSort;} Here it is stated that BubbleSort provides the implementation of 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 { require service.sort; provide service.sort.SortService with sort.impl.merge.MergeSort;} Here is 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 there are no dependencies for implementation classes added here
module-info.java
module consumer { require 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
Compilation
mvn clean install
This is executed in the root directory
Using bubble
The code copy 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 jar of bubble to module-path is added here
Output
sort service consumer started.
use BubbleSort
[2, 3, 5, 8, 10]
finish
Use merge
The code copy 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 merge jar to module-path is added here
Output
sort service consumer started.
using MergeSort
[2, 3, 5, 8, 10]
finish
Both service implementations are added
The code copy 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
The code copy 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 the merge jar package is placed in front of it, it is still a bubble.
summary
In java6, there was already a ServiceLoader, but at that time, it was relied on creating a full path name file for the service interface in the META-INF/services directory of the jar package, and wrote the full path name of the implementation class. After the introduction of modularization, Java9 also supports the declaration of service provider and consumer information in module-info.java. In this way, the module system can support ServiceLoader without using the original META-INF declaration method.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.