HowTo have multiple SPI implementations in one JAR

micfra

Assume I have one interface com.example.Marker and I do have one implementation of this, eg com.example.MarkerImplA. To get one of them registered, I need to place a text file in META-INF/services/com.example.Marker looking like this

com.example.MarkerImplA

This works like a charme. Now, I have another implementation in the same jar file, eg com.example.MarkerImpl2. How do I achieve the registration of the second one?

René Link

Just add multiple lines with the fully-qualified provider's class name

So if your SPI is com.example.Marker

META-INF/services/com.example.Marker

add each implementation in a new line

com.example.MarkerImplA
com.example.MarkerImplB
com.example.MarkerImplC

For details take a look at the JAR File Specification

Provider-Configuration File

A service provider identifies itself by placing a provider-configuration file in the resource directory META-INF/services. The file's name should consist of the fully-qualified name of the abstract service class. The file should contain a newline-separated list of unique concrete provider-class names. Space and tab characters, as well as blank lines, are ignored. The comment character is '#' (0x23); on each line all characters following the first comment character are ignored. The file must be encoded in UTF-8.

Provider lookup

Use the ServiceLoader<T> that was introduced in Java 1.6

ServiceLoader<Marker> markerLoader = ServiceLoader.load(com.example.Marker.class);
for (Marker marker : markerLoader ) {
     // select the marker you want or use all

     // only for demo
     System.out.println(marker);
}

Prior to 1.6 you have to implement the service loader by yourself or use a library. E.g. the apache discovery library's Service.

A note about plug-ins

It is possible to have multiple META-INF/service provider-configuration files in different deployment units, usually jar archives.

archive1.jar
 +- META-INF/services/com.example.Marker

archive2.jar
 +- META-INF/services/com.example.Marker

A lookup via the ServiceLoader<T> will pick up all implementations in all archives. This is done using ClassLoader.getResources().

Thus you only have to add a jar to the classpath and the provided services can be picked up. That gives you the opportunity to build a plug-in architecture.

This is how spring works. You just add a jar to the classpath and spring features are available, because spring scans for it's service definitions. These definitions are named e.g. spring.handlers. Spring handlers are namespace handlers that extend the xml parsing to create BeanDefinitions that a BeanFactory uses to create spring beans for the ApplicationContext. See e.g. spring-tx.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

HowTo have multiple SPI implementations in one JAR

From Dev

To look an EJB , i have to deploy the implementations .jar or interfaces .jar?

From Dev

Android Multiple Implementations of one element of the same Layout

From Dev

Guice: One "Provider<T>" for multiple implementations

From Dev

Excel VBA Grouping - HOWTO Multiple Groups in One Group Level

From Dev

Allow Spring to have multiple WebMvcConfigurer implementations in different jars

From Dev

How to add multiple implementations to one service in vnext startup file?

From Dev

Laravel 4 One interface with multiple implementations at same time

From Dev

create multiple implementations of a class, one to send Qt signals and one to work with hardware directly

From Dev

Scala Multiple Implementations Design

From Dev

Interface with multiple implementations in ninject

From Dev

Multiple IEnumerable implementations paradox

From Dev

Interface with multiple implementations in ninject

From Dev

Multiple implementations of common interface

From Dev

Android: how to use Code to interface design pattern to have multiple implementations of network call libraries(Retrofit or volley)

From Dev

Combine multiple jar into one (using maven)

From Dev

howto build api for multiple clients

From Dev

Howto Install Multiple Snaps Simultaneously?

From Dev

How can I tell the linker which one of multiple implementations of a method to use?

From Dev

Howto have different colors on links in a Android Webview

From Dev

Howto have thunderbird/lightning open ics files

From Dev

Howto have thunderbird/lightning open ics files

From Dev

Howto setup Flexbox to always have 3 Columns?

From Dev

Is it possible to have one activity with multiple xml layouts?

From Dev

In bash, is there a way have multiple pipes to one process?

From Dev

Is it possible to have multiple highcharts with one id?

From Dev

How to have multiple SignalR hub on one page

From Dev

How to have multiple FontLoader objects in one class?

From Dev

Is it possible to have multiple fonts in one div?

Related Related

  1. 1

    HowTo have multiple SPI implementations in one JAR

  2. 2

    To look an EJB , i have to deploy the implementations .jar or interfaces .jar?

  3. 3

    Android Multiple Implementations of one element of the same Layout

  4. 4

    Guice: One "Provider<T>" for multiple implementations

  5. 5

    Excel VBA Grouping - HOWTO Multiple Groups in One Group Level

  6. 6

    Allow Spring to have multiple WebMvcConfigurer implementations in different jars

  7. 7

    How to add multiple implementations to one service in vnext startup file?

  8. 8

    Laravel 4 One interface with multiple implementations at same time

  9. 9

    create multiple implementations of a class, one to send Qt signals and one to work with hardware directly

  10. 10

    Scala Multiple Implementations Design

  11. 11

    Interface with multiple implementations in ninject

  12. 12

    Multiple IEnumerable implementations paradox

  13. 13

    Interface with multiple implementations in ninject

  14. 14

    Multiple implementations of common interface

  15. 15

    Android: how to use Code to interface design pattern to have multiple implementations of network call libraries(Retrofit or volley)

  16. 16

    Combine multiple jar into one (using maven)

  17. 17

    howto build api for multiple clients

  18. 18

    Howto Install Multiple Snaps Simultaneously?

  19. 19

    How can I tell the linker which one of multiple implementations of a method to use?

  20. 20

    Howto have different colors on links in a Android Webview

  21. 21

    Howto have thunderbird/lightning open ics files

  22. 22

    Howto have thunderbird/lightning open ics files

  23. 23

    Howto setup Flexbox to always have 3 Columns?

  24. 24

    Is it possible to have one activity with multiple xml layouts?

  25. 25

    In bash, is there a way have multiple pipes to one process?

  26. 26

    Is it possible to have multiple highcharts with one id?

  27. 27

    How to have multiple SignalR hub on one page

  28. 28

    How to have multiple FontLoader objects in one class?

  29. 29

    Is it possible to have multiple fonts in one div?

HotTag

Archive