public final class ServiceLoader<S> extends Object implements Iterable<S>
This is NOT part of any supported API. If you write code that depends on this, you do so at your own risk. This code and its internal interfaces are subject to change or deletion without notice.
Modifier and Type | Method and Description |
---|---|
Iterator<S> |
iterator()
Lazily loads the available providers of this loader's service.
|
static <S> ServiceLoader<S> |
load(Class<S> service)
Creates a new service loader for the given service type, using the
current thread's context class loader.
|
static <S> ServiceLoader<S> |
load(Class<S> service,
ClassLoader loader)
Creates a new service loader for the given service type and class
loader.
|
static <S> ServiceLoader<S> |
loadInstalled(Class<S> service)
Creates a new service loader for the given service type, using the
extension class loader.
|
void |
reload()
Clear this loader's provider cache so that all providers will be
reloaded.
|
String |
toString()
Returns a string describing this service.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
forEach, spliterator
public void reload()
After invoking this method, subsequent invocations of the iterator
method will lazily look up and instantiate
providers from scratch, just as is done by a newly-created loader.
This method is intended for use in situations in which new providers can be installed into a running Java virtual machine.
public Iterator<S> iterator()
The iterator returned by this method first yields all of the elements of the provider cache, in instantiation order. It then lazily loads and instantiates any remaining providers, adding each one to the cache in turn.
To achieve laziness the actual work of parsing the available
provider-configuration files and instantiating providers must be done by
the iterator itself. Its hasNext
and
next
methods can therefore throw a
ServiceConfigurationError
if a provider-configuration file
violates the specified format, or if it names a provider class that
cannot be found and instantiated, or if the result of instantiating the
class is not assignable to the service type, or if any other kind of
exception or error is thrown as the next provider is located and
instantiated. To write robust code it is only necessary to catch ServiceConfigurationError
when using a service iterator.
If such an error is thrown then subsequent invocations of the iterator will make a best effort to locate and instantiate the next available provider, but in general such recovery cannot be guaranteed.
Design Note Throwing an error in these cases may seem extreme. The rationale for this behavior is that a malformed provider-configuration file, like a malformed class file, indicates a serious problem with the way the Java virtual machine is configured or is being used. As such it is preferable to throw an error rather than try to recover or, even worse, fail silently.
The iterator returned by this method does not support removal.
Invoking its remove
method will
cause an UnsupportedOperationException
to be thrown.
public static <S> ServiceLoader<S> load(Class<S> service, ClassLoader loader)
service
- The interface or abstract class representing the serviceloader
- The class loader to be used to load provider-configuration files
and provider classes, or null if the system class
loader (or, failing that, the bootstrap class loader) is to be
usedpublic static <S> ServiceLoader<S> load(Class<S> service)
An invocation of this convenience method of the form
is equivalent toServiceLoader.load(service)
ServiceLoader.load(service, Thread.currentThread().getContextClassLoader())
service
- The interface or abstract class representing the servicepublic static <S> ServiceLoader<S> loadInstalled(Class<S> service)
This convenience method simply locates the extension class loader, call it extClassLoader, and then returns
ServiceLoader.load(service, extClassLoader)
If the extension class loader cannot be found then the system class loader is used; if there is no system class loader then the bootstrap class loader is used.
This method is intended for use when only installed providers are desired. The resulting service will only find and load providers that have been installed into the current Java virtual machine; providers on the application's class path will be ignored.
service
- The interface or abstract class representing the serviceCopyright © 2016. All rights reserved.