• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2007 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 import com.google.inject.internal.InternalInjectorCreator;
20 import java.util.Arrays;
21 
22 /**
23  * The entry point to the Guice framework. Creates {@link Injector}s from {@link Module}s.
24  *
25  * <p>Guice supports a model of development that draws clear boundaries between APIs,
26  * Implementations of these APIs, Modules which configure these implementations, and finally
27  * Applications which consist of a collection of Modules. It is the Application, which typically
28  * defines your {@code main()} method, that bootstraps the Guice Injector using the {@code Guice}
29  * class, as in this example:
30  *
31  * <pre>
32  *     public class FooApplication {
33  *       public static void main(String[] args) {
34  *         Injector injector = Guice.createInjector(
35  *             new ModuleA(),
36  *             new ModuleB(),
37  *             . . .
38  *             new FooApplicationFlagsModule(args)
39  *         );
40  *
41  *         // Now just bootstrap the application and you're done
42  *         FooStarter starter = injector.getInstance(FooStarter.class);
43  *         starter.runApplication();
44  *       }
45  *     }
46  * </pre>
47  */
48 public final class Guice {
49 
Guice()50   private Guice() {}
51 
52   /**
53    * Creates an injector for the given set of modules. This is equivalent to calling {@link
54    * #createInjector(Stage, Module...)} with Stage.DEVELOPMENT.
55    *
56    * @throws CreationException if one or more errors occur during injector construction
57    */
createInjector(Module... modules)58   public static Injector createInjector(Module... modules) {
59     return createInjector(Arrays.asList(modules));
60   }
61 
62   /**
63    * Creates an injector for the given set of modules. This is equivalent to calling {@link
64    * #createInjector(Stage, Iterable)} with Stage.DEVELOPMENT.
65    *
66    * @throws CreationException if one or more errors occur during injector creation
67    */
createInjector(Iterable<? extends Module> modules)68   public static Injector createInjector(Iterable<? extends Module> modules) {
69     return createInjector(Stage.DEVELOPMENT, modules);
70   }
71 
72   /**
73    * Creates an injector for the given set of modules, in a given development stage.
74    *
75    * @throws CreationException if one or more errors occur during injector creation.
76    */
createInjector(Stage stage, Module... modules)77   public static Injector createInjector(Stage stage, Module... modules) {
78     return createInjector(stage, Arrays.asList(modules));
79   }
80 
81   /**
82    * Creates an injector for the given set of modules, in a given development stage.
83    *
84    * @throws CreationException if one or more errors occur during injector construction
85    */
createInjector(Stage stage, Iterable<? extends Module> modules)86   public static Injector createInjector(Stage stage, Iterable<? extends Module> modules) {
87     return new InternalInjectorCreator().stage(stage).addModules(modules).build();
88   }
89 }
90