Annotation Interface Service.Provider

Enclosing class:
Service

@Documented @Retention(CLASS) @Target(TYPE) public static @interface Service.Provider
A service provider. This is similar to ServiceLoader service providers.

The provider is an implementation of a Service.Contract that is discoverable by the service registry. A service provider annotated with this annotation must provide either of:

  • an accessible no-argument constructor (package private is sufficient), and must itself be at least package private
  • an accessible constructor with arguments that are all services as well
The support for providing constructor arguments is limited to the following types:
  • Contract - obtain an instance of a service providing that contract
  • Optional<Contract> - the other service may not be available
  • List<Contract> - obtain all instances of services providing the contract
  • Supplier<Contract> - and suppliers of all above, to break instantiation chaining, and to support cyclic service references, just make sure you call the get method outside of the constructor
A service provider may implement the contract in two ways:
  • Direct implementation of interface (or extending an abstract class)
  • Implementing a Supplier of the contract; when using supplier, service registry supports the capability to return Optional in case the service cannot provide a value; such a service will be ignored and only other implementations (with lower weight) would be used. Supplier will be called each time the dependency is used, or each time a method on registry is called to request an instance
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final TypeName
    Type name of this annotation.
  • Field Details

    • TYPE

      static final TypeName TYPE
      Type name of this annotation.