public class ServiceFactory extends Object
Gets a service loader that is able to retrieve services implementation.
The service loader implementation is retrieved from system properties (see SystemPropertiesServiceLoader), if
none is found, a SystemPropertiesServiceLoader is used. The implementation may be a
CompositeServiceLoader that searches to resolve the services in a list of service loaders.
Usage:
MyInterface instance = ServiceFactory.getService(MyInterface.class);
The service registry implementation is retrieved from system properties (see SystemPropertiesServiceLoader),
if none is found, a SimpleServiceRegistry is used.
Usage:
ServiceFactory.register(MyInterface.class, myInstance);
| Modifier and Type | Method and Description |
|---|---|
static <T> T |
getRequiredService(Class<T> service)
Gets the instance of the given service.
|
static <T> T |
getService(Class<T> service)
Gets the instance of the given service.
|
static <T> T |
getService(Class<T> service,
Class<? extends T> defaultImplementation)
Gets the instance of the given service in the service loader then in the service registry (see
getService(Class). |
static ServiceLoader |
getServiceLoader()
Gets a service loader unique instance.
|
static ServiceRegistry |
getServiceRegistry()
Gets a service registry unique instance.
|
static <T> void |
register(Class<T> service,
T instance)
Registers a service instance.
|
static <T> void |
unregister(Class<T> service,
T instance)
Unregisters a service instance.
|
public static <T> T getRequiredService(Class<T> service)
If there is already an instance in cache, it is returned otherwise a new one is created.
May throw a MissingServiceException if the service is not defined.
T - the service type.service - the service.ServiceLoadingException - if an error occurs while instantiating the service instance.SecurityException - if a security manager exists and does not allow the caller to retrieve the given service instance.MissingServiceException - if the service has no registered implementation.ServiceLoader.getService(Class)@Nullable public static <T> T getService(Class<T> service)
It first searches in the service loader, then in the service registry.
May return null if the service is not defined.
T - the service type.service - the service.ServiceLoadingException - if an error occurs while instantiating the service instance.SecurityException - if a security manager exists and does not allow the caller to retrieve the given service instance.getServiceLoader(),
getServiceRegistry()public static <T> T getService(Class<T> service, Class<? extends T> defaultImplementation)
getService(Class).
If there is no registered implementation, an instance of the default implementation is created, registered in the service registry and returned.
T - the service type.service - the service.defaultImplementation - the default implementation to use.ServiceLoadingException - if an error occurs while instantiating the service instance.SecurityException - if a security manager exists and does not allow the caller to retrieve the given service instance.getServiceLoader(),
getServiceRegistry()public static ServiceLoader getServiceLoader()
public static ServiceRegistry getServiceRegistry()
public static <T> void register(Class<T> service, T instance)
The given instance is returned each time the service is requested by getService(Class).
If the service already has a registered instance, the previous one is replaced.
T - the service type.service - the implemented service.instance - the service instance.SecurityException - if a security manager exists and does not allow the caller to register the given service instance.public static <T> void unregister(Class<T> service, T instance)
If the given instance is not the one currently registered for the given service, nothing is done.
T - the service type.service - the implemented service.instance - the service instance.SecurityException - if a security manager exists and does not allow the caller to unregister the given service instance.