xref: /aosp_15_r20/external/jsr330/src/javax/inject/Provider.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 /**
20*3ff81872SXin Li  * Provides instances of {@code T}. Typically implemented by an injector. For
21*3ff81872SXin Li  * any type {@code T} that can be injected, you can also inject
22*3ff81872SXin Li  * {@code Provider<T>}. Compared to injecting {@code T} directly, injecting
23*3ff81872SXin Li  * {@code Provider<T>} enables:
24*3ff81872SXin Li  *
25*3ff81872SXin Li  * <ul>
26*3ff81872SXin Li  *   <li>retrieving multiple instances.</li>
27*3ff81872SXin Li  *   <li>lazy or optional retrieval of an instance.</li>
28*3ff81872SXin Li  *   <li>breaking circular dependencies.</li>
29*3ff81872SXin Li  *   <li>abstracting scope so you can look up an instance in a smaller scope
30*3ff81872SXin Li  *      from an instance in a containing scope.</li>
31*3ff81872SXin Li  * </ul>
32*3ff81872SXin Li  *
33*3ff81872SXin Li  * <p>For example:
34*3ff81872SXin Li  *
35*3ff81872SXin Li  * <pre>
36*3ff81872SXin Li  *   class Car {
37*3ff81872SXin Li  *     &#064;Inject Car(Provider&lt;Seat> seatProvider) {
38*3ff81872SXin Li  *       Seat driver = seatProvider.get();
39*3ff81872SXin Li  *       Seat passenger = seatProvider.get();
40*3ff81872SXin Li  *       ...
41*3ff81872SXin Li  *     }
42*3ff81872SXin Li  *   }</pre>
43*3ff81872SXin Li  */
44*3ff81872SXin Li public interface Provider<T> {
45*3ff81872SXin Li 
46*3ff81872SXin Li     /**
47*3ff81872SXin Li      * Provides a fully-constructed and injected instance of {@code T}.
48*3ff81872SXin Li      *
49*3ff81872SXin Li      * @throws RuntimeException if the injector encounters an error while
50*3ff81872SXin Li      *  providing an instance. For example, if an injectable member on
51*3ff81872SXin Li      *  {@code T} throws an exception, the injector may wrap the exception
52*3ff81872SXin Li      *  and throw it to the caller of {@code get()}. Callers should not try
53*3ff81872SXin Li      *  to handle such exceptions as the behavior may vary across injector
54*3ff81872SXin Li      *  implementations and even different configurations of the same injector.
55*3ff81872SXin Li      */
get()56*3ff81872SXin Li     T get();
57*3ff81872SXin Li }
58