blob: 8a4c48c88e9cc65f139738dde3b5d008c31ee264 [file] [log] [blame]
kevinb9nb20cc352007-03-01 19:35:41 +00001/*
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
kevinb9ndb85d9c2007-02-20 05:23:46 +000017package com.google.inject;
18
19import java.util.Arrays;
20
21/**
kevinb9na2915a92007-02-28 06:20:30 +000022 * The entry point to the Guice framework. Creates {@link Injector}s from
crazyboblee0bfdbc62007-02-20 21:47:19 +000023 * {@link Module}s.
kevinb9nb950ad92007-03-13 02:16:16 +000024 *
25 * <p>Guice supports a model of development that draws clear boundaries between
26 * APIs, Implementations of these APIs, Modules which configure these
27 * implementations, and finally Applications which consist of a collection of
28 * Modules. It is the Application, which typically defines your {@code main()}
29 * method, that bootstraps the Guice Injector using the {@code Guice} class, as
30 * in this example:
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
limpbizkit9b894702008-12-11 09:57:47 +000042 * FooStarter starter = injector.getInstance(FooStarter.class);
kevinb9nb950ad92007-03-13 02:16:16 +000043 * starter.runApplication();
44 * }
45 * }
46 * </pre>
kevinb9ndb85d9c2007-02-20 05:23:46 +000047 */
kevinb9ne5073a22007-02-20 18:06:47 +000048public final class Guice {
kevinb9ndb85d9c2007-02-20 05:23:46 +000049
crazyboblee0bfdbc62007-02-20 21:47:19 +000050 private Guice() {}
51
52 /**
kevinb9na2915a92007-02-28 06:20:30 +000053 * Creates an injector for the given set of modules.
crazybobleec1d0c642007-03-07 17:20:22 +000054 *
kevinb9nb950ad92007-03-13 02:16:16 +000055 * @throws CreationException if one or more errors occur during Injector
56 * construction
crazyboblee0bfdbc62007-02-20 21:47:19 +000057 */
crazybobleec1d0c642007-03-07 17:20:22 +000058 public static Injector createInjector(Module... modules) {
kevinb9na2915a92007-02-28 06:20:30 +000059 return createInjector(Arrays.asList(modules));
kevinb9ndb85d9c2007-02-20 05:23:46 +000060 }
61
crazyboblee0bfdbc62007-02-20 21:47:19 +000062 /**
kevinb9na2915a92007-02-28 06:20:30 +000063 * Creates an injector for the given set of modules.
crazybobleec1d0c642007-03-07 17:20:22 +000064 *
kevinb9nb950ad92007-03-13 02:16:16 +000065 * @throws CreationException if one or more errors occur during Injector
limpbizkit4994bf62008-12-27 02:54:59 +000066 * creation
crazyboblee0bfdbc62007-02-20 21:47:19 +000067 */
crazybobleed9d16a02007-09-09 21:18:48 +000068 public static Injector createInjector(Iterable<? extends Module> modules) {
kevinb9na2915a92007-02-28 06:20:30 +000069 return createInjector(Stage.DEVELOPMENT, modules);
kevinb9ne5073a22007-02-20 18:06:47 +000070 }
71
crazyboblee0bfdbc62007-02-20 21:47:19 +000072 /**
kevinb9na2915a92007-02-28 06:20:30 +000073 * Creates an injector for the given set of modules, in a given development
74 * stage.
crazybobleec1d0c642007-03-07 17:20:22 +000075 *
kevinb9nb950ad92007-03-13 02:16:16 +000076 * @throws CreationException if one or more errors occur during Injector
limpbizkit4994bf62008-12-27 02:54:59 +000077 * creation
crazyboblee0bfdbc62007-02-20 21:47:19 +000078 */
crazybobleec1d0c642007-03-07 17:20:22 +000079 public static Injector createInjector(Stage stage, Module... modules) {
kevinb9na2915a92007-02-28 06:20:30 +000080 return createInjector(stage, Arrays.asList(modules));
kevinb9ne5073a22007-02-20 18:06:47 +000081 }
82
crazyboblee0bfdbc62007-02-20 21:47:19 +000083 /**
kevinb9na2915a92007-02-28 06:20:30 +000084 * Creates an injector for the given set of modules, in a given development
85 * stage.
crazybobleec1d0c642007-03-07 17:20:22 +000086 *
kevinb9nb950ad92007-03-13 02:16:16 +000087 * @throws CreationException if one or more errors occur during Injector
88 * construction
crazyboblee0bfdbc62007-02-20 21:47:19 +000089 */
crazybobleed9d16a02007-09-09 21:18:48 +000090 public static Injector createInjector(Stage stage,
91 Iterable<? extends Module> modules) {
limpbizkit3d58d6b2008-03-08 16:11:47 +000092 return new InjectorBuilder()
93 .stage(stage)
limpbizkit3d58d6b2008-03-08 16:11:47 +000094 .addModules(modules)
95 .build();
kevinb9ndb85d9c2007-02-20 05:23:46 +000096 }
kevinb9ndb85d9c2007-02-20 05:23:46 +000097}