public abstract class AbstractServiceLoader extends Object implements ServiceLoader
Subclasses need to provide the implementation class name matching the requested services. The implementation is then
instantiated using Class.forName(String) and Class.newInstance(). The singletons are stored in a map.
| Constructor and Description |
|---|
AbstractServiceLoader()
Creates a new abstract service loader.
|
| Modifier and Type | Method and Description |
|---|---|
protected abstract String |
getImplementationName(String key)
Gets the implementation class name for the given key.
|
<T> T |
getService(Class<T> service)
Gets the instance of the given service.
|
<T> T |
getService(Class<T> service,
boolean singleton)
Gets the registered implementation for the given service.
|
<T> T |
getService(Class<T> service,
Class<? extends T> defaultImplementation)
Gets the registered implementation for the given service.
|
<T> T |
getService(Class<T> service,
Class<? extends T> defaultImplementation,
boolean singleton)
Gets the registered implementation for the given service.
|
public AbstractServiceLoader()
@Nullable protected abstract String getImplementationName(String key)
key - the service name.null if none.@Nullable public <T> T getService(Class<T> service)
ServiceLoader
Equivalent to calling ServiceLoader.getService(Class, boolean) passing @{code true} as @{code singleton}.
getService in interface ServiceLoadergetService in interface ServiceProviderT - the service type.service - the service.@Nullable public <T> T getService(Class<T> service, boolean singleton)
ServiceLoaderIf a singleton is asked and if an instance of this service has already been created, this instance is returned. Otherwise, creates a new instance of the implementation type.
getService in interface ServiceLoaderT - the service type.service - the service.singleton - if true returns an already created instance if any, creates a new one otherwise.null if no implementation is available.public <T> T getService(Class<T> service, Class<? extends T> defaultImplementation)
ServiceLoadergetService in interface ServiceLoaderT - the service type.service - the service.defaultImplementation - the default implementation to use.public <T> T getService(Class<T> service, Class<? extends T> defaultImplementation, boolean singleton)
ServiceLoaderIf a singleton is asked and if an instance of this service has already been created, this instance is returned. Otherwise, creates a new instance of the implementation type.
getService in interface ServiceLoaderT - the service type.service - the service.defaultImplementation - the default implementation to use.singleton - if true returns an already created instance if any, creates a new one otherwise.