1.没有读取到META-INF/services/下面配置的实现,需要在pom.xml中声明
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>META-INF/services/*.*</include>
</includes>
</resource>
</resources>
</build>
2.没有写入到jar包中的mateInfo.service下的JPI设置
已经用了
@AutoService(Factory.class)
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<annotationProcessorPaths>
<path>
<groupId>com.google.auto.service</groupId>
<artifactId>auto-service</artifactId>
<version>1.0.1</version>
</path>
</annotationProcessorPaths>
<proc>only</proc>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.example.MainClass</mainClass>
</transformer>
</transformers>
<!-- 确保 .class 文件被包含 -->
<createDependencyReducedPom>false</createDependencyReducedPom>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
都不管用,最后还是笨方法,直接在source下写入:

本文解决Java中ServiceLoader.load无法正确加载实现类的问题。通过调整Maven配置,确保资源文件正确打包,并使用注解处理器来生成必要的服务文件。
1218

被折叠的 条评论
为什么被折叠?



