01: /**
02: * Copyright (C) 2006 Google Inc.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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: */package com.bm.ejb3guice.inject;
16:
17: /**
18: * A scope is a level of visibility that instances provided by Guice may have.
19: * By default, an instance created by the {@link Injector} has <i>no scope</i>,
20: * meaning it has no state from the framework's perspective -- the
21: * {@code Injector} creates it, injects it once into the class that required it,
22: * and then immediately forgets it. Associating a scope with a particular
23: * binding allows the created instance to be "remembered" and possibly used
24: * 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.
34: * If an object does not exist in this scope, the provider can use the given
35: * unscoped provider to retrieve one.
36: *
37: * <p>Scope implementations are strongly encouraged to override
38: * {@link Object#toString} in the returned provider and include the backing
39: * provider's {@code toString()} output.
40: *
41: * @param key binding key
42: * @param unscoped locates an instance when one doesn't already exist in this
43: * scope.
44: * @return a new provider which only delegates to the given unscoped provider
45: * when an instance of the requested object doesn't already exist in this
46: * scope
47: */
48: public <T> Provider<T> scope(Key<T> key, Provider<T> unscoped);
49:
50: /**
51: * A short but useful description of this scope. For comparison, the standard
52: * scopes that ship with guice use the descriptions
53: * {@code "Scopes.SINGLETON"}, {@code "ServletScopes.SESSION"} and
54: * {@code "ServletScopes.REQUEST"}.
55: */
56: String toString();
57: }
|