@Target(value=ANNOTATION_TYPE)
@Retention(value=RUNTIME)
@Documented
public @interface Scope
In the following example, the scope annotation @Singleton
ensures
that we only have one Log instance:
@Singleton class Log { void log(String message) { ... } }
The injector generates an error if it encounters more than one scope annotation on the same class or a scope annotation it doesn't support.
A scope annotation:
@Scope
, @Retention(RUNTIME)
,
and typically @Documented
.@Inherited
, so scoping is orthogonal to
implementation inheritance.@Target
. While
this specification covers applying scopes to classes only, some
injector configurations might use scope annotations
in other places (on factory method results for example).For example:
@java.lang.annotation.Documented @java.lang.annotation.Retention(RUNTIME) @jakarta.inject.Scope public @interface RequestScoped {}
Annotating scope annotations with @Scope
helps the injector
detect the case where a programmer used the scope annotation on a class but
forgot to configure the scope in the injector. A conservative injector
would generate an error rather than not apply a scope.
@Singleton