@Retention(value=RUNTIME) @Target(value={TYPE,FIELD}) @RequestScoped @Documented public @interface Provides
Indicates 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 Provides are discoverable by the Dependency Injection framework.
If necessary the set of services provided by a type can be explicitly specified using the value() property.
@Provides
class Foo {
...
}
Since Foo does not implement any interfaces then it provides itself as a service.
@Provides
class Foo implements Bar {
...
}
Foo provides the service Bar.
@Provides({Bar.class})
class Foo extends AbstractBar {
...
}
Foo is a specialization of the AbstractBar superclass that provides the Bar service.
@Provides
class Foo extends AbstractBar implements Bar{
...
}
An alternate way to express Foo is a specialization of the AbstractBar superclass that provides the Bar service.
@Provides({Foo.class,Bar.class}}
class Foo extends AbstractBar implements Bar{
...
}
Foo provides the Bar service and also provides itself as a service.
Provides is annotated with the RequestScoped scoping, meaning that a single instance of a type annotated with Provides will be created per scope. RequestScoped types 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. ApplicationScoped types cannot depend on any RequestScoped types.
@Provides({GlobalService.class}}
@ApplicationScoped
class Singleton implements GlobalService{
...
}
public abstract Class<?>[] value