Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Mastering Eclipse Plug-in Development

You're reading from   Mastering Eclipse Plug-in Development Build modular applications on Eclipse by defining custom extension points and using OSGi services

Arrow left icon
Product type Paperback
Published in Aug 2014
Publisher
ISBN-13 9781783287796
Length 362 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Authors (2):
Arrow left icon
Bandlem Limited Bandlem Limited
Author Profile Icon Bandlem Limited
Bandlem Limited
Alex Blewitt Alex Blewitt
Author Profile Icon Alex Blewitt
Alex Blewitt
Arrow right icon
View More author details
Toc

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 the META-INF/services/ directory. When looking for implementations for the java.sql.Driver class, the service loader will attempt to find files called META-INF/services/java.sql.Driver. The contents of these files are fully qualified...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image