Annotation Interface Provides


@Documented @Retention(RUNTIME) @Target({METHOD,FIELD}) public @interface Provides
An annotation indicating that a method or field is the provider of a service. Example usage:
   public class MyService {
     @Inject
     public MyService( ... ) { ... }

     @Provides
     public OtherService other() {
       return new OtherService( ... );
     }
   }

   ServiceLocator locator =
       ServiceLocatorUtilities.createAndPopulateServiceLocator();

   ServiceLocatorUtilities.addClasses(
       locator,
       ProvidesListener.class,
       MyService.class);

   OtherService other = locator.getService(OtherService.class);
 

Contracts

The contracts of the provided service are, by default, defined by the method return type of the annotated method or the field type of the annotated field. These default contracts can be overridden using contracts().

Lifecycle

The providing class is responsible for initializing instances of the provided service. The system will not automatically invoke a PostConstruct.postConstruct() method declared by the provided service.

If the annotated member is a method, then disposal of the provided service may be customized. See disposeMethod(). If the annotated member is a field, then the provided service will not undergo automatic disposal.

Scope

The scope of the provided service is:

  • The Scope annotation on the method or field, if present.
  • Otherwise, the Scope annotation on the method return type or the field type, if present.
  • Otherwise, if the method or field is non-static, the scope of the containing service.
  • Otherwise, PerLookup.
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static enum 
    Specifies who is responsible for the disposal of instances of a service.
  • Optional Element Summary

    Optional Elements
    Modifier and Type
    Optional Element
    Description
    Class<?>[]
    If non-empty, specifies a list of contracts provided that overrides the default contracts.
    Specifies who is responsible for the disposal of instances of the provided service, assuming that disposeMethod() is non-empty.
    If non-empty, specifies the name of the method to be invoked when disposing of an instance of the provided service.
  • Element Details

    • contracts

      Class<?>[] contracts
      If non-empty, specifies a list of contracts provided that overrides the default contracts. Similar to ContractsProvided.

      If empty, then the default contracts for the provided type will be used.

      Default:
      {}
    • disposeMethod

      String disposeMethod
      If non-empty, specifies the name of the method to be invoked when disposing of an instance of the provided service. The class whose method is to be invoked is specified by disposalHandledBy().

      If empty, then the default pre-destroy behavior for the provided type will be used. If the provided type implements PreDestroy for example, then its PreDestroy.preDestroy() method will be invoked.

      This value is ignored when this annotation is applied to a field.

      Default:
      ""
    • disposalHandledBy

      Provides.DisposalHandledBy disposalHandledBy
      Specifies who is responsible for the disposal of instances of the provided service, assuming that disposeMethod() is non-empty. If disposeMethod() is empty or this annotation is applied to a field, then this value is ignored.

      See Provides.DisposalHandledBy for definitions of the possible values.

      Default:
      PROVIDED_INSTANCE