Interface ServiceRegistry
- All Known Implementing Classes:
DefaultServiceRegistry
public interface ServiceRegistry
The service registry manages and provides access to service providers. Services are interfaces or abstract classes
which define a set of methods a provider of a service must implement. Modules and/or plugins can query
implementations from the registry (if one is available for the given service). If multiple service providers are
present for a service the plugin/module must either decide which service to use or should use the first one (first
registered one).
- Since:
- 4.0
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> @UnknownNullability TDeprecated, for removal: This API element is subject to removal in a future version.the method is deprecated as it requires abusing the purpose of dependency injection in order to work.default <T> @UnknownNullability TfirstProvider(@NonNull Class<T> service) Get the first registered provider for the given service from this registry.<T> booleanhasProvider(@NonNull Class<T> service, @NonNull String name) Checks if a provider with the given name is registered for the given service.@NonNull Collection<Class<?>>Get all services for which a provider was registered previously.<T> @UnknownNullability TGet the provider for the given service with the given name from this registry.<T> @UnmodifiableView @NonNull Collection<T>Get all registered providers for the given service.<T,E extends T>
@NonNull ServiceRegistryregisterProvider(@NonNull Class<T> service, @NonNull String name, E provider) Registers the given provider for the given service to this service registry.Unregisters all previously registered providers for all services from this registry.unregisterAll(@NonNull ClassLoader classLoader) Unregisters all providers for any service from this registry whose classes were loaded by the given class loader.<T,E extends T>
@NonNull ServiceRegistryunregisterProvider(@NonNull Class<T> service, @NonNull Class<E> provider) Unregisters all providers for the given service which are an instance of the given class.unregisterProvider(@NonNull Class<T> service, @NonNull String name) Unregisters the provider with the given name from the given service if registered previously.<T,E extends T>
@NonNull ServiceRegistryunregisterProvider(@NonNull Class<T> service, E provider) Unregisters the given provider instance from the registered services of the given service class.unregisterProviders(@NonNull Class<T> service) Removes all previously registered providers for the given service.
-
Method Details
-
first
@Deprecated(since="4.0", forRemoval=true) @ScheduledForRemoval(inVersion="4.1") static <T> @UnknownNullability T first(@NonNull @NonNull Class<T> service) Deprecated, for removal: This API element is subject to removal in a future version.the method is deprecated as it requires abusing the purpose of dependency injection in order to work. There are two ways to replace this part of the api- injecting the service registry itself and calling
firstProvider(Class). - injecting the requested service directly using the
Serviceannotation.
Get the first provider for the given service class. This method returns null when no service for the given class is present. If multiple providers are present the first registered one will be returned.- Type Parameters:
T- the type of the service to query.- Parameters:
service- the service to query.- Returns:
- the last registered provider for the given service or null if no provider is registered.
- Throws:
NullPointerException- if the given service class is null.
- injecting the service registry itself and calling
-
registerProvider
@NonNull <T,E extends T> @NonNull ServiceRegistry registerProvider(@NonNull @NonNull Class<T> service, @NonNull @NonNull String name, @NonNull E provider) Registers the given provider for the given service to this service registry. After registration the given provider will be the first provider of the given service and retrievable with the given name.If multiple services with the same name are registered the first one will be returned which was registered to this registry.
- Type Parameters:
T- the service class type to register the service to.E- the provider (or service implementation) class type.- Parameters:
service- the service class to register the implementation to.name- the name of the provider to register.provider- the provider (and service implementation) to register.- Returns:
- the same instance as used to call the method, for chaining.
- Throws:
NullPointerException- if the given service class, name or implementation is null.
-
unregisterProvider
@NonNull <T,E extends T> @NonNull ServiceRegistry unregisterProvider(@NonNull @NonNull Class<T> service, @NonNull @NonNull Class<E> provider) Unregisters all providers for the given service which are an instance of the given class.- Type Parameters:
T- the type of the service.E- the type of the providers.- Parameters:
service- the service to unregister the providers from.provider- the class of the providers to unregister.- Returns:
- the same instance as used to call the method, for chaining.
- Throws:
NullPointerException- if the given service or provider class is null.
-
unregisterProvider
@NonNull <T,E extends T> @NonNull ServiceRegistry unregisterProvider(@NonNull @NonNull Class<T> service, @NonNull E provider) Unregisters the given provider instance from the registered services of the given service class.- Type Parameters:
T- the type of the service.E- the type of the providers.- Parameters:
service- the service to unregister the provider from.provider- the provider instance to unregister.- Returns:
- the same instance as used to call the method, for chaining.
- Throws:
NullPointerException- if the given service class or provider instance is null.
-
hasProvider
Checks if a provider with the given name is registered for the given service.- Type Parameters:
T- the type of the service.- Parameters:
service- the service to check the providers of.name- the name of the service to check for.- Returns:
- true if this registry has a provider with the given name for the service, false otherwise.
- Throws:
NullPointerException- if the given service class or provider name is null.
-
unregisterProvider
@NonNull <T> @NonNull ServiceRegistry unregisterProvider(@NonNull @NonNull Class<T> service, @NonNull @NonNull String name) Unregisters the provider with the given name from the given service if registered previously.- Type Parameters:
T- the type of the service.- Parameters:
service- the service to unregister the provider from.name- the name of the provider to unregister.- Returns:
- the same instance as used to call the method, for chaining.
- Throws:
NullPointerException- if the given service class or provider name is null.
-
unregisterProviders
Removes all previously registered providers for the given service.- Type Parameters:
T- the type of the service.- Parameters:
service- the service to unregisters the providers of.- Returns:
- the same instance as used to call the method, for chaining.
- Throws:
NullPointerException- if the given service is null.
-
unregisterAll
Unregisters all previously registered providers for all services from this registry.- Returns:
- the same instance as used to call the method, for chaining.
-
unregisterAll
Unregisters all providers for any service from this registry whose classes were loaded by the given class loader.- Parameters:
classLoader- the class loader of the providers to unregister.- Returns:
- the same instance as used to call the method, for chaining.
- Throws:
NullPointerException- if the given class loader is null.
-
providedServices
Get all services for which a provider was registered previously.- Returns:
- all services for which a provider was registered previously.
-
provider
<T> @UnknownNullability T provider(@NonNull @NonNull Class<T> service, @NonNull @NonNull String name) Get the provider for the given service with the given name from this registry. This method returns null if no such provider was registered previously.This method will return the first provider registered with the given name if multiple providers with the same name for the given service were registered.
- Type Parameters:
T- the type of the service.- Parameters:
service- the service of the provider.name- the name of the provider to get.- Returns:
- the provider for the given service with the given name, null if no such provider was registered.
- Throws:
NullPointerException- if the given service or provider name is null.
-
firstProvider
Get the first registered provider for the given service from this registry. This method returns null if no provider for the given service is registered.- Type Parameters:
T- the type of the service.- Parameters:
service- the service to get the first provider of.- Returns:
- the first registered provider for the given service, null if no providers for the service are registered.
- Throws:
NullPointerException- if the given service is null.
-
providers
Get all registered providers for the given service. This method returns an empty collection if no providers are registered for the given service but never null.- Type Parameters:
T- the type of the service.- Parameters:
service- the service to get all registered providers of.- Returns:
- all registered providers for the service or an empty collection if no providers were registered for it.
- Throws:
NullPointerException- if the given service is null.
-