Coder Social home page Coder Social logo

avaje-spi-service's Introduction

Build License Maven Central : avaje-spi-service Discord

avaje-spi-service

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

Usage

1. Add dependency:

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

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

avaje-spi-service's People

Contributors

sentryman avatar rbygrave avatar dependabot[bot] avatar github-actions[bot] avatar rob-bygrave avatar

Stargazers

Vinicius de Sousa Camargo avatar  avatar Adam Gent avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.