Annotation Type Provides
-
@Retention(RUNTIME) @Target({TYPE,FIELD}) @RequestScoped @Documented public @interface ProvidesIndicates that a class provides one or more services. By default, all the declared interfaces of the class are considered to be services provided by the class. If the class does not implement any interfaces then the service being offered is the class itself.
Classes annotated with
Providesare discoverable by the Dependency Injection framework.If necessary the set of services provided by a type can be explicitly specified using the
value()property.Examples
Type Provides self
@Provides class Foo { ... }Since
Foodoes not implement any interfaces then it provides itself as a service.Type implicitly provides implementation of an interface
@Provides class Foo implements Bar { ... }Fooprovides the serviceBar.Type explicitly provides implementation of an interface
@Provides({Bar.class}) class Foo extends AbstractBar { ... }Foois a specialization of theAbstractBarsuperclass that provides theBarservice.@Provides class Foo extends AbstractBar implements Bar{ ... }An alternate way to express
Foois a specialization of theAbstractBarsuperclass that provides theBarservice.Type explicitly provides implementation of an interface, and self
@Provides({Foo.class,Bar.class}} class Foo extends AbstractBar implements Bar{ ... }Fooprovides theBarservice and also provides itself as a service.Providing Static Fields
Static Constant fields in a type may be annotated with
Providesto provide singleton instances of the field value. Alternatively if the type is declared as abstract it may be annotated withProvidesand each constant field annotated with aQualifierwill bound into the scope. The provided service is the type of the constant field.Examples
Type provides a configuration setting
@Provides public abstract class SomePluginSettings { public static final String SOME_TIMEOUT_SETTING = "some.plugin.someTimeoutSetting"; ... @Named(SomePluginSettings.SOME_TIMEOUT_SETTING); private static final ConfigurationSetting _SOME_SETTING = ConfigurationSetting.setting(TimeDuration.value("15m")); }- The type is annotated with
Providesbut is marked abstract. This indicates that it is not the type itself that should be injected but rather any constant fields annotated with aQualifier(in this caseNamed) - Read more about injecting
ConfigurationSettings in theoracle.dbtools.plugin.api.confpackage summary
Type provides singleton instance of self
public class SomeSingleton { private SomeSingleton() {} ... public static final SomeSingleton instance() { return INSTANCE; } @Provides private static final SomeSingleton INSTANCE = new SomeSingleton; }- The type does not have
Providesannotation - The constant field containing the
SomeSingletoninstance is annotated withProvides. This single instance will be injected at every site declaring a dependency onSomeSingleton
Scoping
Providesis annotated with theRequestScopedscoping, meaning that a single instance of a type annotated withProvideswill be created per scope.RequestScopedtypes do not need to be thread-safe, as they will only be referenced in a single thread.If necessary a type can indicate that only a single instance should be created across all scopes by annotating the type with
ApplicationScoped. Any such type must be thread-safe as the instance may be referenced across multiple threads.ApplicationScopedtypes cannot depend on anyRequestScopedtypes.@Provides({GlobalService.class}} @ApplicationScoped class Singleton implements GlobalService{ ... }- Author:
- cdivilly
- The type is annotated with
-
-
Optional Element Summary
Optional Elements Modifier and Type Optional Element Description java.lang.Class<?>[]valueThe list of services this class provides.
-
-
-
Element Detail
-
value
java.lang.Class<?>[] value
The list of services this class provides.- Returns:
- the set of services this class provides. If empty then the annotation processor should infer the set of services is the set of interfaces implemented by the service, or if the class implements no services, then the public API of the class becomes the service provided.
- Default:
- {}
-
-