为什么ServiceLoader不会从另一个模块加载任何实现?

问题描述 投票:0回答:1

我有一个使用JPMS功能的多模块Maven项目。使用者模块未加载提供程序模块中存在的实现。

这是maven项目结构:

ServiceLoaderExample
├── consumer
├── distribution
├── provider
├── service

接口TestService在“服务”模块中定义。实现是在“提供程序”模块中定义的TestServiceImpl。 “消费者”模块中的main()方法使用ServiceLoader API来加载TestService的实现。在“分发”模块中,我使用maven-assesmbly-plugin创建了具有所有依赖项的JAR。

因此,这是相应的模块信息文件:

1-“服务”模块(定义org.example.service.TestService):

module org.example.service {
    exports org.example.service;
}

2-“提供程序”模块(定义org.example.provider.TestServiceImpl):

module org.example.provider {
    requires org.example.service;
    provides org.example.service.TestService with org.example.provider.TestServiceImpl;
}

3-“消费者”模块(使用ServiceLoader API获取TestService的实现):

module org.example.consumer {
    requires org.example.service;
    uses org.example.service.TestService;
}

我正在使用maven-assesmbly插件,因此在构建JAR时,事情可能会搞砸。供参考,这是插件定义:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>3.2.0</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                    <configuration>
                        <archive>
                            <manifest>
                                <mainClass>org.example.consumer.Main</mainClass>
                            </manifest>
                        </archive>
                        <descriptorRefs>
                            <descriptorRef>jar-with-dependencies</descriptorRef>
                        </descriptorRefs>
                    </configuration>
                </execution>
            </executions>
        </plugin>

我正在使用Java 13和Maven 3.6.3,并且正在使用命令java -jar distribution-1.0-SNAPSHOT-jar-with-dependencies.jar运行该程序。我在做什么错?

java java-module serviceloader
1个回答
0
投票

我无法在uber-jar中使用它,但是,使用JLink时一切正常。因此决定只使用JLink。

© www.soinside.com 2019 - 2024. All rights reserved.