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 * Simply, any object capable of providing instances of type {@code T}. 21 * Providers are used in numerous ways by the Guice framework: 22 * 23 * <ul> 24 * <li>When the default means for obtaining instances (an injectable or 25 * parameterless constructor) is insufficient for a particular binding, the 26 * module can specify a custom {@code Provider} instead, to control exactly how 27 * Guice creates or obtains instances for the binding. 28 * 29 * <li>An implementation class may always choose to have a {@code Provider<T>} 30 * instance injected, rather than having a {@code T} injected directly. This 31 * may give you access to multiple instances, instances you wish to safely 32 * mutate and discard, instances which are out of scope (e.g. using a 33 * {@code @RequestScoped} object from within a {@code @SessionScoped} object), 34 * or instances you don't want to initialize until they are absolutely needed. 35 * 36 * <li>A custom {@link Scope} is implemented as a decorator of 37 * {@code Provider<T>}, which decides when to delegate to the backing provider 38 * and when to provide the instance some other way. 39 * 40 * <li>The {@link Injector} offers access to the {@code Provider<T>} it uses 41 * to fulfill requests for a given key, via the {@link Injector#getProvider} 42 * methods. 43 * </ul> 44 * 45 * @param <T> the type of object this provider provides 46 * 47 * @author crazybob@google.com (Bob Lee) 48 */ 49 public interface Provider<T> { 50 51 /** 52 * Provides an instance of {@code T}. Must never return {@code null}. 53 */ 54 T get(); 55 } 56