xref: /aosp_15_r20/external/jsr330/src/javax/inject/Scope.java (revision 3ff81872dd771505ae446579ad46cd1fa54f7475)
1*3ff81872SXin Li /*
2*3ff81872SXin Li  * Copyright (C) 2009 The JSR-330 Expert Group
3*3ff81872SXin Li  *
4*3ff81872SXin Li  * Licensed under the Apache License, Version 2.0 (the "License");
5*3ff81872SXin Li  * you may not use this file except in compliance with the License.
6*3ff81872SXin Li  * You may obtain a copy of the License at
7*3ff81872SXin Li  *
8*3ff81872SXin Li  *      http://www.apache.org/licenses/LICENSE-2.0
9*3ff81872SXin Li  *
10*3ff81872SXin Li  * Unless required by applicable law or agreed to in writing, software
11*3ff81872SXin Li  * distributed under the License is distributed on an "AS IS" BASIS,
12*3ff81872SXin Li  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*3ff81872SXin Li  * See the License for the specific language governing permissions and
14*3ff81872SXin Li  * limitations under the License.
15*3ff81872SXin Li  */
16*3ff81872SXin Li 
17*3ff81872SXin Li package javax.inject;
18*3ff81872SXin Li 
19*3ff81872SXin Li import java.lang.annotation.Target;
20*3ff81872SXin Li import java.lang.annotation.Retention;
21*3ff81872SXin Li import java.lang.annotation.Documented;
22*3ff81872SXin Li import static java.lang.annotation.RetentionPolicy.RUNTIME;
23*3ff81872SXin Li import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
24*3ff81872SXin Li 
25*3ff81872SXin Li /**
26*3ff81872SXin Li  * Identifies scope annotations. A scope annotation applies to a class
27*3ff81872SXin Li  * containing an injectable constructor and governs how the injector reuses
28*3ff81872SXin Li  * instances of the type. By default, if no scope annotation is present, the
29*3ff81872SXin Li  * injector creates an instance (by injecting the type's constructor), uses
30*3ff81872SXin Li  * the instance for one injection, and then forgets it. If a scope annotation
31*3ff81872SXin Li  * is present, the injector may retain the instance for possible reuse in a
32*3ff81872SXin Li  * later injection. If multiple threads can access a scoped instance, its
33*3ff81872SXin Li  * implementation should be thread safe. The implementation of the scope
34*3ff81872SXin Li  * itself is left up to the injector.
35*3ff81872SXin Li  *
36*3ff81872SXin Li  * <p>In the following example, the scope annotation {@code @Singleton} ensures
37*3ff81872SXin Li  * that we only have one Log instance:
38*3ff81872SXin Li  *
39*3ff81872SXin Li  * <pre>
40*3ff81872SXin Li  *   &#064;Singleton
41*3ff81872SXin Li  *   class Log {
42*3ff81872SXin Li  *     void log(String message) { ... }
43*3ff81872SXin Li  *   }</pre>
44*3ff81872SXin Li  *
45*3ff81872SXin Li  * <p>The injector generates an error if it encounters more than one scope
46*3ff81872SXin Li  * annotation on the same class or a scope annotation it doesn't support.
47*3ff81872SXin Li  *
48*3ff81872SXin Li  * <p>A scope annotation:
49*3ff81872SXin Li  * <ul>
50*3ff81872SXin Li  *   <li>is annotated with {@code @Scope}, {@code @Retention(RUNTIME)},
51*3ff81872SXin Li  *      and typically {@code @Documented}.</li>
52*3ff81872SXin Li  *   <li>should not have attributes.</li>
53*3ff81872SXin Li  *   <li>is typically not {@code @Inherited}, so scoping is orthogonal to
54*3ff81872SXin Li  *      implementation inheritance.</li>
55*3ff81872SXin Li  *   <li>may have restricted usage if annotated with {@code @Target}. While
56*3ff81872SXin Li  *      this specification covers applying scopes to classes only, some
57*3ff81872SXin Li  *      injector configurations might use scope annotations
58*3ff81872SXin Li  *      in other places (on factory method results for example).</li>
59*3ff81872SXin Li  * </ul>
60*3ff81872SXin Li  *
61*3ff81872SXin Li  * <p>For example:
62*3ff81872SXin Li  *
63*3ff81872SXin Li  * <pre>
64*3ff81872SXin Li  *   &#064;java.lang.annotation.Documented
65*3ff81872SXin Li  *   &#064;java.lang.annotation.Retention(RUNTIME)
66*3ff81872SXin Li  *   &#064;javax.inject.Scope
67*3ff81872SXin Li  *   public @interface RequestScoped {}</pre>
68*3ff81872SXin Li  *
69*3ff81872SXin Li  * <p>Annotating scope annotations with {@code @Scope} helps the injector
70*3ff81872SXin Li  * detect the case where a programmer used the scope annotation on a class but
71*3ff81872SXin Li  * forgot to configure the scope in the injector. A conservative injector
72*3ff81872SXin Li  * would generate an error rather than not apply a scope.
73*3ff81872SXin Li  *
74*3ff81872SXin Li  * @see javax.inject.Singleton @Singleton
75*3ff81872SXin Li  */
76*3ff81872SXin Li @Target(ANNOTATION_TYPE)
77*3ff81872SXin Li @Retention(RUNTIME)
78*3ff81872SXin Li @Documented
79*3ff81872SXin Li public @interface Scope {}
80