Skip to content

avaje/avaje-spi-service

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

268 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build License Maven Central Discord

Uses Annotation processing to automatically add META-INF/services entries for classes and validate module-info.java files.

Usage

1. Add dependency:

Maven:

<dependency>
  <groupId>io.avaje</groupId>
  <artifactId>avaje-spi-service</artifactId>
  <version>${spi.version}</version>
  <optional>true</optional>
  <scope>provided</scope>
</dependency>

Gradle:

compileOnly("io.avaje:avaje-spi-service:${spi.version}")
annotationProcessor("io.avaje:avaje-spi-service:${spi.version}")

When working with Java modules you need to add the annotation module as a static dependency.

module my.module {
  requires static io.avaje.spi;
}

2. Add @ServiceProvider

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 {
  ...
}

3. module-info validation

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;`

Related Works

About

META-INF/services generator

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages