org.glassfish.hk2
Interface ServiceLocator<T>
- All Superinterfaces:
- Providers<T>
- All Known Subinterfaces:
- ContractLocator<T>
public interface ServiceLocator<T>
- extends Providers<T>
A ServiceLocator provides runtime access to a particular
service in HK2.
At any point in time, one can get the Providers that qualify
to the locator query by calling the super class methods.
There are two variations of ServiceLocators. The first type
is when a ServiceLocator is produced by methods like
Locator.byType(Class) and Locator.byType(String).
In HK2, services "byType" represent a concrete class type lacking
any contract level abstraction of that concrete class as shown in
this example:
@Service
public class Example {
...
}
Services "byType" are either present or they are not. This is because
a service "byType" is a concrete class declaration as shown in the
example above. Example.class will either be present in the
HK2 service registry or it will not. In another words, there
is no further way to abstractly locate Example.class when it
is being sought by its type.
The methods #getProvider() and {@link #get()}
represent access to the singleton if the service "byType" exists in
the HK2 service registry. If the service component does not exist then
these methods will return null. In this situation, {@link #all()} will
return an empty collection. Otherwise {@link #all} will return a
singleton collection.
The second variation of ServiceLocator is that it forms the
super interface for {@link ContractLocator} and is produced by methods
like {@link Services#forContract(Class)} and
{@link Services#forContract(String)}.
Unlike "byType" where there can either be zero or one manifestation of
a qualifying service, a {@link ContractLocator} can represent many
service instances in the HK2 service registry. In this case, the
methods {@link #getProvider()} and {@link #get()} represent
the "best qualifying" service matching the locator criteria.
See {@link ContractLocator} for more detail.
- Author:
- Jerome Dochez, Jeff Trent, Mason Taube
- See Also:
ContractLocator
Copyright © 2011 Oracle Corporation. All Rights Reserved.