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 service instances matching the contract with the expectation that there may not be a match available.static <T> List
<T> Get all instances of the contract.static <T> List
<T> Get all service instances matching the contract with the expectation that there may not be a match available.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 the first service instance matching the contract with the expectation that there may not be a match available.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 the first service instance matching the contract with the expectation that there may not be a match available.static <T> Optional
<T> firstNamed
(TypeName contract, String name) Get the first named service instance matching the contract with the expectation that there may not be a match available.static <T> Optional
<T> firstNamed
(Class<T> contract, String name) Get the first named service instance matching the contract with the expectation that there may not be a match available.static <T> T
Get the first instance of the contract, expecting the contract is available.static <T> T
Get the first service instance matching the contract and qualifiers with the expectation that there is a match available.static <T> T
Get the first instance of the contract, expecting the contract is available.static <T> T
Get the first service instance matching the contract and qualifiers with the expectation that there is a match available.static <T> T
Get the first named service instance matching the contract with the expectation that there is a match available.static <T> T
Get the first named service instance matching the contract with the expectation that there is a match 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.static <T> void
Set a named instance.static <T> void
setQualified
(Class<T> contract, T instance, Qualifier... qualifiers) Set a qualified instance.
-
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
-
setQualified
Set a qualified instance.Rules are the same as for
set(Class, Object[])
.- Type Parameters:
T
- type of the service- Parameters:
contract
- contract to setinstance
- instance to usequalifiers
- qualifier(s) to qualify the instance
-
setNamed
Set a named instance.Rules are the same as for
set(Class, Object[])
.- Type Parameters:
T
- type of the service- Parameters:
contract
- contract to setinstance
- instance to usename
- name qualifier to qualify the instance
-
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:
-
getNamed
Get the first named service instance matching the contract with the expectation that there is a match available.- Type Parameters:
T
- type of the contract- Parameters:
contract
- contract to look-upname
- name qualifier of the instance to get- Returns:
- the best service instance matching the contract
- Throws:
ServiceRegistryException
- if there is no service that could satisfy the lookup, or the resolution to instance failed
-
get
Get the first service instance matching the contract and qualifiers with the expectation that there is a match available.- Type Parameters:
T
- type of the contract- Parameters:
contract
- contract to look-upqualifiers
- qualifiers to find- Returns:
- the best service instance matching the contract
- Throws:
ServiceRegistryException
- if there is no service that could satisfy the lookup, or the resolution to instance failed
-
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:
-
getNamed
Get the first named service instance matching the contract with the expectation that there is a match available.- Type Parameters:
T
- type of the contract- Parameters:
contract
- contract to look-upname
- name qualifier of the instance to get- Returns:
- the best service instance matching the contract
- Throws:
ServiceRegistryException
- if there is no service that could satisfy the lookup, or the resolution to instance failed
-
get
Get the first service instance matching the contract and qualifiers with the expectation that there is a match available.- Type Parameters:
T
- type of the contract (we will "blindly" cast the result to the expected type, make sure you use the right one)- Parameters:
contract
- contract to look-upqualifiers
- qualifiers to find- Returns:
- the best service instance matching the contract
- Throws:
ServiceRegistryException
- if there is no service that could satisfy the lookup, or the resolution to instance failed
-
all
Get all service instances matching the contract with the expectation that there may not be a match available.- Type Parameters:
T
- type of the contract- Parameters:
contract
- contract to look-upqualifiers
- qualifiers to find- Returns:
- list of services matching the criteria, may be empty if none matched, or no instances were provided
-
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 service instances matching the contract with the expectation that there may not be a match available.- Type Parameters:
T
- type of the contract- Parameters:
contract
- contract to look-upqualifiers
- qualifiers to find- Returns:
- list of services matching the criteria, may be empty if none matched, or no instances were provided
-
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
-
firstNamed
Get the first named service instance matching the contract with the expectation that there may not be a match available.- Type Parameters:
T
- type of the contract- Parameters:
contract
- contract to look-upname
- name qualifier of the instance to get- Returns:
- the best service instance matching the contract
- Throws:
ServiceRegistryException
- if there is no service that could satisfy the lookup, or the resolution to instance failed
-
first
Get the first service instance matching the contract with the expectation that there may not be a match available.- Type Parameters:
T
- type of the contract- Parameters:
contract
- contract to look-upqualifiers
- qualifiers to find- Returns:
- the best service instance matching the contract, or an empty
Optional
if none match - Throws:
ServiceRegistryException
- if there is no service that could satisfy the lookup, or the resolution to instance failed
-
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
-
firstNamed
Get the first named service instance matching the contract with the expectation that there may not be a match available.- Type Parameters:
T
- type of the contract- Parameters:
contract
- contract to look-upname
- name qualifier of the instance to get- Returns:
- the best service instance matching the contract
- Throws:
ServiceRegistryException
- if there is no service that could satisfy the lookup, or the resolution to instance failed
-
first
Get the first service instance matching the contract with the expectation that there may not be a match available.- Type Parameters:
T
- type of the contract- Parameters:
contract
- contract to look-upqualifiers
- qualifiers to find- Returns:
- the best service instance matching the contract, or an empty
Optional
if none match - Throws:
ServiceRegistryException
- if there is no service that could satisfy the lookup, or the resolution to instance failed
-