O'Reilly logo

Mastering Eclipse Plug-in Development by Dr. Alex Blewitt

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Java ServiceLoader

The ServiceLoader class in the java.util package (added in Java 1.6) provides a means of acquiring an instance of an interface or abstract class. It is used by a variety of different parts in the JDK, where a single implementation is required that cannot be known in advance, such as JDBC drivers.

The ServiceLoader class provides a static load method that can be used to return a ServiceLoader, which in turn provides an Iterator over all services available:

ServiceLoader<Driver> sl = ServiceLoader.load(Driver.class);
Iterator<Driver> it = sl.iterator();
while (it.hasNext()) {
  Driver driver = (Driver) it.next();
  // do something with driver
}

The implementation class for the driver is found by consulting a text file, located under ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required