Ever thought how that JPA implementation (EclipseLink or Hibernate) is picked up by the Java Persistence APIs. Or How does java.sql.DriverManager loads the available implementation of java.sql.Driver. Or for that matter How JMS implementation is picked up by messaging framework.

Well they all follow JAVA SPI (Service Provider Interface) mechanism.

According to Effective Java, 2nd Edition :

A service provider framework is a system in which multiple service providers implement a service, and the system makes the implementations available to its clients, decoupling them from the implementations.

There are 3 essential components of a service provider framework:

A service interface, which providers implement. A provider registration API, which the system uses to register implementations, giving clients access to them. and A service access API, which client chooses to obtain an Instance of the service.

In this article we will go through a sample code, through which we will try to understand Java Service Provider framework.

Set Up :

All the code is available @ https://github.com/niteshagrawalgmail/basic.git in the folder spi.

https://github.com/niteshagrawalgmail/basic/tree/master/spi