blob: df75116727602416ce228aeb0656753e7e5e91b8 [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;
limpbizkit5ae41eb2009-06-06 17:51:27 +000020import com.google.inject.internal.InjectorBuilder;
kevinb9ndb85d9c2007-02-20 05:23:46 +000021
22/**
kevinb9na2915a92007-02-28 06:20:30 +000023 * The entry point to the Guice framework. Creates {@link Injector}s from
crazyboblee0bfdbc62007-02-20 21:47:19 +000024 * {@link Module}s.
kevinb9nb950ad92007-03-13 02:16:16 +000025 *
26 * <p>Guice supports a model of development that draws clear boundaries between
27 * APIs, Implementations of these APIs, Modules which configure these
28 * implementations, and finally Applications which consist of a collection of
29 * Modules. It is the Application, which typically defines your {@code main()}
30 * method, that bootstraps the Guice Injector using the {@code Guice} class, as
31 * in this example:
32 * <pre>
33 * public class FooApplication {
34 * public static void main(String[] args) {
35 * Injector injector = Guice.createInjector(
36 * new ModuleA(),
37 * new ModuleB(),
38 * . . .
39 * new FooApplicationFlagsModule(args)
40 * );
41 *
42 * // Now just bootstrap the application and you're done
limpbizkit9b894702008-12-11 09:57:47 +000043 * FooStarter starter = injector.getInstance(FooStarter.class);
kevinb9nb950ad92007-03-13 02:16:16 +000044 * starter.runApplication();
45 * }
46 * }
47 * </pre>
kevinb9ndb85d9c2007-02-20 05:23:46 +000048 */
kevinb9ne5073a22007-02-20 18:06:47 +000049public final class Guice {
kevinb9ndb85d9c2007-02-20 05:23:46 +000050
crazyboblee0bfdbc62007-02-20 21:47:19 +000051 private Guice() {}
52
53 /**
kevinb9na2915a92007-02-28 06:20:30 +000054 * Creates an injector for the given set of modules.
crazybobleec1d0c642007-03-07 17:20:22 +000055 *
kevinb9nb950ad92007-03-13 02:16:16 +000056 * @throws CreationException if one or more errors occur during Injector
57 * construction
crazyboblee0bfdbc62007-02-20 21:47:19 +000058 */
crazybobleec1d0c642007-03-07 17:20:22 +000059 public static Injector createInjector(Module... modules) {
kevinb9na2915a92007-02-28 06:20:30 +000060 return createInjector(Arrays.asList(modules));
kevinb9ndb85d9c2007-02-20 05:23:46 +000061 }
62
crazyboblee0bfdbc62007-02-20 21:47:19 +000063 /**
kevinb9na2915a92007-02-28 06:20:30 +000064 * Creates an injector for the given set of modules.
crazybobleec1d0c642007-03-07 17:20:22 +000065 *
kevinb9nb950ad92007-03-13 02:16:16 +000066 * @throws CreationException if one or more errors occur during Injector
limpbizkit4994bf62008-12-27 02:54:59 +000067 * creation
crazyboblee0bfdbc62007-02-20 21:47:19 +000068 */
crazybobleed9d16a02007-09-09 21:18:48 +000069 public static Injector createInjector(Iterable<? extends Module> modules) {
kevinb9na2915a92007-02-28 06:20:30 +000070 return createInjector(Stage.DEVELOPMENT, modules);
kevinb9ne5073a22007-02-20 18:06:47 +000071 }
72
crazyboblee0bfdbc62007-02-20 21:47:19 +000073 /**
kevinb9na2915a92007-02-28 06:20:30 +000074 * Creates an injector for the given set of modules, in a given development
75 * stage.
crazybobleec1d0c642007-03-07 17:20:22 +000076 *
kevinb9nb950ad92007-03-13 02:16:16 +000077 * @throws CreationException if one or more errors occur during Injector
limpbizkit4994bf62008-12-27 02:54:59 +000078 * creation
crazyboblee0bfdbc62007-02-20 21:47:19 +000079 */
crazybobleec1d0c642007-03-07 17:20:22 +000080 public static Injector createInjector(Stage stage, Module... modules) {
kevinb9na2915a92007-02-28 06:20:30 +000081 return createInjector(stage, Arrays.asList(modules));
kevinb9ne5073a22007-02-20 18:06:47 +000082 }
83
crazyboblee0bfdbc62007-02-20 21:47:19 +000084 /**
kevinb9na2915a92007-02-28 06:20:30 +000085 * Creates an injector for the given set of modules, in a given development
86 * stage.
crazybobleec1d0c642007-03-07 17:20:22 +000087 *
kevinb9nb950ad92007-03-13 02:16:16 +000088 * @throws CreationException if one or more errors occur during Injector
89 * construction
crazyboblee0bfdbc62007-02-20 21:47:19 +000090 */
crazybobleed9d16a02007-09-09 21:18:48 +000091 public static Injector createInjector(Stage stage,
92 Iterable<? extends Module> modules) {
limpbizkit3d58d6b2008-03-08 16:11:47 +000093 return new InjectorBuilder()
94 .stage(stage)
limpbizkit3d58d6b2008-03-08 16:11:47 +000095 .addModules(modules)
96 .build();
kevinb9ndb85d9c2007-02-20 05:23:46 +000097 }
kevinb9ndb85d9c2007-02-20 05:23:46 +000098}