Service Provider Interface

An Extensible application is one in which you can extend without modifying its original code base.  Using SPI all you have to do is to add a jar (with META-INF/services/<interface> file in applicaitons class path or extended library location) and the services will be found and made available to the application.

Service: A set of programming interfaces and classes that

  • Define a functionality (Service Interface).
  • And a way to retrieve an implementation (Service Access API).
  • And a way to regiser a provider (Registration API).

Service Provider Interface: A Set of public interfaces and abstract classes that providers implement to create an instance of their Service Implementation.

Service Provider: Implements SPI.

Example:

1. A word processor application that allows end users to write their own dictionary and spell checkers.

  • DictionaryServiceDemo: Applicaiton Name
  • DictionaryDemo.java: Client
  • DictionaryService.java: Service Interface; Service Access Interface; Provier Registration through SPI standard
  • Dictionary.java: SPI
  • ExtendedDictionary.java: Service Provider 1
  • GeneralDictionary: Service Provider 2

2. In JDBC Connection plays part of Service Inreface (or the feature that clients use); Driver.getConnection is Servce Access API; DriverManager.registerDriver is the provider registration API; Driver is the SPI that service providers implement.

Java Trail: http://docs.oracle.com/javase/tutorial/ext/basics/spi.html

SPI vs API: http://stackoverflow.com/questions/2954372/difference-between-spi-and-api

ServiceLoader: http://docs.oracle.com/javase/6/docs/api/java/util/ServiceLoader.html

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s