• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2006 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.google.inject;
18 
19 /**
20  * A scope is a level of visibility that instances provided by Guice may have. By default, an
21  * instance created by the {@link Injector} has <i>no scope</i>, meaning it has no state from the
22  * framework's perspective -- the {@code Injector} creates it, injects it once into the class that
23  * required it, and then immediately forgets it. Associating a scope with a particular binding
24  * allows the created instance to be "remembered" and possibly used again for other injections.
25  *
26  * <p>An example of a scope is {@link Scopes#SINGLETON}.
27  *
28  * @author crazybob@google.com (Bob Lee)
29  */
30 public interface Scope {
31 
32   /**
33    * Scopes a provider. The returned provider returns objects from this scope. If an object does not
34    * exist in this scope, the provider can use the given unscoped provider to retrieve one.
35    *
36    * <p>Scope implementations are strongly encouraged to override {@link Object#toString} in the
37    * returned provider and include the backing provider's {@code toString()} output.
38    *
39    * @param key binding key
40    * @param unscoped locates an instance when one doesn't already exist in this scope.
41    * @return a new provider which only delegates to the given unscoped provider when an instance of
42    *     the requested object doesn't already exist in this scope
43    */
scope(Key<T> key, Provider<T> unscoped)44   public <T> Provider<T> scope(Key<T> key, Provider<T> unscoped);
45 
46   /**
47    * A short but useful description of this scope. For comparison, the standard scopes that ship
48    * with guice use the descriptions {@code "Scopes.SINGLETON"}, {@code "ServletScopes.SESSION"} and
49    * {@code "ServletScopes.REQUEST"}.
50    */
51   @Override
toString()52   String toString();
53 }
54