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