Uses Annotation processing to automatically add META-INF/services
entries for classes and validate module-info.java
files.
<dependency>
<groupId>io.avaje</groupId>
<artifactId>avaje-spi-service</artifactId>
<version>${spi.version}</version>
<optional>true</optional>
<scope>provided</scope>
</dependency>
When working with Java modules you need to add the annotation module as a static dependency.
module my.module {
requires static io.avaje.spi;
}
On classes that you'd like registered, put the @ServiceProvider
annotation. As long as you only have one interface or one superclass, that type is assumed to be the spi interface. So given the example below:
@ServiceProvider
public class MyProvider implements SomeSPI {
...
}
You get the META-INF/services/com.example.SomeSPI
file whose content is org.acme.MyProvider
.
If you have multiple interfaces and/or base type, the library cannot infer the contract type. In such a case, specify the contract type explicitly by giving it to @ServiceProvider
like this:
@ServiceProvider(SomeSPI.class)
public class MyExtendedProvider extends AbstractSet implements Comparable, Serializable, SomeSPI {
...
}
For modular projects, the processor will throw a compile error describing what provides
statements you have missed. So if you define the SPI like the the previous steps, and have a module setup like the following:
module my.module {
requires static io.avaje.spi;
}
You'll get the following compile error:
Compilation failure /src/main/java/module-info.java:[1,1]
Missing `provides SomeSPI with MyProvider, MyExtendedProvider;`