Note: Using any methods on this class makes the service registry throw away optimization created via the Helidon Service Registry Maven Plugin (code generated binding) for services that use the contracts configured.
There is always a single instance of a registry available within an application that can be access through methods on this class.
This instance can be explicitly configured using registry(ServiceRegistry)
in case you want to setup a
registry instance yourself.
We support "late binding" of services using this class, such as by using set(Class, Object[])
.
These methods allow setting an explicit instance for a service normally built by the registry.
The set methods have the following limitations:
- The
contract
parameter must be of one of the supported contracts by the service registry - The method MUST be called before the contract is used by any other service, to achieve consistency (i.e. calling
ServiceRegistry.get(Class)
must yield the same instance if the service is a singleton
-
Method Summary
Modifier and TypeMethodDescriptionstatic void
add
(ServiceDescriptor<?> descriptor) Add a custom service descriptor.static <T> void
Add an explicit instance for the specified service contract.static <T> List
<T> Get all instances of the contract.static <T> List
<T> Get all instances of the contract.static <T> Optional
<T> Get first instance of the contract from the registry, all an empty optional if none exist.static <T> Optional
<T> Get first instance of the contract from the registry, all an empty optional if none exist.static <T> T
Get the first instance of the contract, expecting the contract is available.static <T> T
Get the first instance of the contract, expecting the contract is available.static void
registry
(ServiceRegistry registry) Configure the application wide registry to be used by components that require static lookup of required services.static <T> void
Configure an explicit instance (explicit instances) for the specified service contract.
-
Method Details
-
registry
Configure the application wide registry to be used by components that require static lookup of required services.Note that this method MUST be called as one of the first things in your application, as all Helidon components that use other services require it (except for Helidon Logging and Common Config).
- Parameters:
registry
- registry instance to use- Throws:
NullPointerException
- in case the registry is null
-
set
Configure an explicit instance (explicit instances) for the specified service contract. This method replaces all existing service providers and the registry will only use the provided instances.This method must be called before the contract is requested from the registry for the first time.
This method only accepts contracts that are provided by one of the services in this registry.
This method will only work if the underlying implementation of the service registry is provided by Helidon (i.e. it will not work on mocked types, and custom implementations).
- Type Parameters:
T
- type of the contract- Parameters:
contract
- contract to bind the instance underinstances
- instances to use- Throws:
ServiceRegistryException
- in case the service contract was already used and cannot be re-boundNullPointerException
- if either of the parameters is null
-
add
Add an explicit instance for the specified service contract.This method has similar contract to
set(Class, Object[])
except it adds the implementation, where theset
method replaces all implementations.- Type Parameters:
T
- type of the contract- Parameters:
contract
- contract to bind the instance underweight
- weight of the instance (useWeighted.DEFAULT_WEIGHT
for default)instance
- instance to add- Throws:
ServiceRegistryException
- in case the service contract was already used and cannot be re-boundNullPointerException
- if either of the parameters is null
-
add
Add a custom service descriptor.Note: it is recommended to use
ServiceRegistryConfig.BuilderBase.addServiceDescriptor(ServiceDescriptor)
as that service descriptor will be available when constructing the service registry. This method is only to be used when the service registry must be created prior to configuring the binding.- Parameters:
descriptor
- descriptor to "late bind" to the service registry- Throws:
ServiceRegistryException
- in case the service contract was already used and cannot be re-boundNullPointerException
- if the parameter is null
-
get
Get the first instance of the contract, expecting the contract is available.- Type Parameters:
T
- type of the contract- Parameters:
contract
- contract to find- Returns:
- an instance of the contract, never
null
- Throws:
ServiceRegistryException
- in case the contract is not available in the registryNullPointerException
- if either of the parameters is null- See Also:
-
get
Get the first instance of the contract, expecting the contract is available.- Type Parameters:
T
- type of the contract- Parameters:
contract
- contract to find- Returns:
- an instance of the contract, never
null
- Throws:
ServiceRegistryException
- in case the contract is not available in the registryNullPointerException
- if either of the parameters is null- See Also:
-
all
Get all instances of the contract.- Type Parameters:
T
- type of the contract- Parameters:
contract
- contract to find- Returns:
- all instances in the registry, may be empty
- Throws:
NullPointerException
- if either of the parameters is null
-
all
Get all instances of the contract.- Type Parameters:
T
- type of the contract- Parameters:
contract
- contract to find- Returns:
- all instances in the registry, may be empty
- Throws:
NullPointerException
- if either of the parameters is null
-
first
Get first instance of the contract from the registry, all an empty optional if none exist.- Type Parameters:
T
- type of the contract- Parameters:
contract
- contract to find- Returns:
- first instance, or an empty optional
- Throws:
NullPointerException
- if either of the parameters is null
-
first
Get first instance of the contract from the registry, all an empty optional if none exist.- Type Parameters:
T
- type of the contract- Parameters:
contract
- contract to find- Returns:
- first instance, or an empty optional
- Throws:
NullPointerException
- if either of the parameters is null
-