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.getServiceLoader().getService(MyInterface.class);
The service registry implementation is retrieved from system properties (see SystemPropertiesServiceLoader
),
if none is found, a SimpleServiceRegistry
is used.
Usage:
ServiceFactory.getServiceRegistry().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.
|
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()