@groovy.transform.CompileStatic class ServiceRegistry extends java.lang.Object
A service registry that loads and caches different service types using the Java java.util.ServiceLoader, while allowing overriding which instance to return.
This class provides thread-safe service loading and caching, supporting parallel test execution. It provides both automatic service discovery through java.util.ServiceLoader and explicit replacement of the returned service instance for customization.
Usage example:
MyService service = ServiceRegistry.getInstance(MyService, DefaultMyService)
Constructor and description |
---|
ServiceRegistry() |
Type Params | Return Type | Name and description |
---|---|---|
<T> |
static T |
getInstance(java.lang.Class<T> serviceType, java.lang.Class<? extends T> defaultType) Returns the service instance of the given service type, loading it using java.util.ServiceLoader if not already loaded or an instance of the default implementation type if no service implementation is found by the java.util.ServiceLoader. |
<T> |
static void |
setInstance(java.lang.Class<T> serviceType, T instance) Sets the instance to be returned for the given service type, bypassing instance loading from java.util.ServiceLoader. |
<T> |
static void |
setInstance(java.lang.Class<T> serviceType, java.lang.Class<? extends T> instanceType) Sets the implementation type to return for the given service type, bypassing instance loading from java.util.ServiceLoader. |
Methods inherited from class | Name |
---|---|
class java.lang.Object |
java.lang.Object#wait(long, int), java.lang.Object#wait(), java.lang.Object#wait(long), java.lang.Object#equals(java.lang.Object), java.lang.Object#toString(), java.lang.Object#hashCode(), java.lang.Object#getClass(), java.lang.Object#notify(), java.lang.Object#notifyAll() |
Returns the service instance of the given service type, loading it using java.util.ServiceLoader if not already loaded or an instance of the default implementation type if no service implementation is found by the java.util.ServiceLoader. If an instance has been set using setInstance(Class, Object) or setInstance(Class, Class), that instance will be returned instead.
serviceType
- The service typedefaultType
- The service implementation type to use if no service
implementation is found (Must have a zero-argument constructor)Sets the instance to be returned for the given service type, bypassing instance loading from java.util.ServiceLoader.
Setting the instance to null
will revert to loading the the service instance
via the java.util.ServiceLoader.
serviceType
- The service type for which the instance should be setinstance
- The instance to return for the given service type, or
null
for default service loadingSets the implementation type to return for the given service type, bypassing instance loading from java.util.ServiceLoader.
serviceType
- The service type for which the instance should be setinstanceType
- The type of the instance to return for the given service type.
(Must have a zero-argument constructor).Groovy Documentation