JDK6 added a neat facility named ServiceLoader that enables library authors to hook in implementations using an SPI. As explained at the service loader documentation, services must follow certain rules in order to be considered as such; they also must be registered using an standard location based on a naming convention. The following rules apply to classes that may be considered services
-
The class must implement at least one target interface (the service interface).
-
The class must provide a no-args constructor.
-
The class must be public.
-
The class name should be added to a file named
META-INF/services/<target_interface_name>
This library provides a mechanism for enforcing those rules by simply adding an annotation on each service implementation, for
example say there exists the following Calculator
service interface
package com.acme;
public interface Calculator {
double add(double a, double b);
}
A basic implementation of such service may be as follows
package com.acme;
@org.kordamp.jipsy.annotations.ServiceProviderFor(Calculator.class)
public class BasicCalculator implements Calculator {
public double add(double a, double b) { return a + b; }
}
Compile your code. If you look closely at your project’s output you’ll see a file named
META-INF/services/com.acme.Calculator
whose contents should look similar to
# Generated by org.kordamp.jipsy.processor.service.ServiceProviderProcessor (1.2.0)
com.acme.BasicCalculator
Et voilà! There are no additional sources to be touched nor files to be created; Jipsy will take care of the boiler plate.
Jipsy is packaged as a standalone jars with no additional dependencies. Jipsy requires JDK8 as a minimum as it relies on the Annotation Processor facility to do its work. You must enable annotation processing on your IDE if you want Jipsy to work correctly.
Jipsy can be downloaded directly from Maven Central, configure it via Maven or Gradle.
Maven
<dependency> <groupId>org.kordamp.jipsy</groupId> <artifactId>jipsy-processor</artifactId> <version>1.2.0</version> <scope>provided</scope> </dependency>
Gradle
dependencies { annotationProcessor 'org.kordamp.jipsy:jipsy-processor:1.2.0' compileOnly 'org.kordamp.jipsy:jipsy-annotations:1.2.0' }
ℹ️
|
For Maven, use the provided scope in order to mark Jipsy as a compile-only dependency; this also avoids exposing Jipsy to
consumers of your library.
|
Jipsy is a re-implementation of the org.mangosdk.spi project, original by Roel Spilker and Bart Enkelaar; copyrighted by TOPdesk; source distributed under Apache Software License 2.0