kevinb9n | b20cc35 | 2007-03-01 19:35:41 +0000 | [diff] [blame] | 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 | |
kevinb9n | db85d9c | 2007-02-20 05:23:46 +0000 | [diff] [blame] | 17 | package com.google.inject; |
| 18 | |
| 19 | import java.util.Arrays; |
| 20 | |
| 21 | /** |
kevinb9n | a2915a9 | 2007-02-28 06:20:30 +0000 | [diff] [blame] | 22 | * The entry point to the Guice framework. Creates {@link Injector}s from |
crazyboblee | 0bfdbc6 | 2007-02-20 21:47:19 +0000 | [diff] [blame] | 23 | * {@link Module}s. |
kevinb9n | b950ad9 | 2007-03-13 02:16:16 +0000 | [diff] [blame] | 24 | * |
| 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 |
limpbizkit | 9b89470 | 2008-12-11 09:57:47 +0000 | [diff] [blame] | 42 | * FooStarter starter = injector.getInstance(FooStarter.class); |
kevinb9n | b950ad9 | 2007-03-13 02:16:16 +0000 | [diff] [blame] | 43 | * starter.runApplication(); |
| 44 | * } |
| 45 | * } |
| 46 | * </pre> |
kevinb9n | db85d9c | 2007-02-20 05:23:46 +0000 | [diff] [blame] | 47 | */ |
kevinb9n | e5073a2 | 2007-02-20 18:06:47 +0000 | [diff] [blame] | 48 | public final class Guice { |
kevinb9n | db85d9c | 2007-02-20 05:23:46 +0000 | [diff] [blame] | 49 | |
crazyboblee | 0bfdbc6 | 2007-02-20 21:47:19 +0000 | [diff] [blame] | 50 | private Guice() {} |
| 51 | |
| 52 | /** |
kevinb9n | a2915a9 | 2007-02-28 06:20:30 +0000 | [diff] [blame] | 53 | * Creates an injector for the given set of modules. |
crazyboblee | c1d0c64 | 2007-03-07 17:20:22 +0000 | [diff] [blame] | 54 | * |
kevinb9n | b950ad9 | 2007-03-13 02:16:16 +0000 | [diff] [blame] | 55 | * @throws CreationException if one or more errors occur during Injector |
| 56 | * construction |
crazyboblee | 0bfdbc6 | 2007-02-20 21:47:19 +0000 | [diff] [blame] | 57 | */ |
crazyboblee | c1d0c64 | 2007-03-07 17:20:22 +0000 | [diff] [blame] | 58 | public static Injector createInjector(Module... modules) { |
kevinb9n | a2915a9 | 2007-02-28 06:20:30 +0000 | [diff] [blame] | 59 | return createInjector(Arrays.asList(modules)); |
kevinb9n | db85d9c | 2007-02-20 05:23:46 +0000 | [diff] [blame] | 60 | } |
| 61 | |
crazyboblee | 0bfdbc6 | 2007-02-20 21:47:19 +0000 | [diff] [blame] | 62 | /** |
kevinb9n | a2915a9 | 2007-02-28 06:20:30 +0000 | [diff] [blame] | 63 | * Creates an injector for the given set of modules. |
crazyboblee | c1d0c64 | 2007-03-07 17:20:22 +0000 | [diff] [blame] | 64 | * |
kevinb9n | b950ad9 | 2007-03-13 02:16:16 +0000 | [diff] [blame] | 65 | * @throws CreationException if one or more errors occur during Injector |
limpbizkit | 4994bf6 | 2008-12-27 02:54:59 +0000 | [diff] [blame] | 66 | * creation |
crazyboblee | 0bfdbc6 | 2007-02-20 21:47:19 +0000 | [diff] [blame] | 67 | */ |
crazyboblee | d9d16a0 | 2007-09-09 21:18:48 +0000 | [diff] [blame] | 68 | public static Injector createInjector(Iterable<? extends Module> modules) { |
kevinb9n | a2915a9 | 2007-02-28 06:20:30 +0000 | [diff] [blame] | 69 | return createInjector(Stage.DEVELOPMENT, modules); |
kevinb9n | e5073a2 | 2007-02-20 18:06:47 +0000 | [diff] [blame] | 70 | } |
| 71 | |
crazyboblee | 0bfdbc6 | 2007-02-20 21:47:19 +0000 | [diff] [blame] | 72 | /** |
kevinb9n | a2915a9 | 2007-02-28 06:20:30 +0000 | [diff] [blame] | 73 | * Creates an injector for the given set of modules, in a given development |
| 74 | * stage. |
crazyboblee | c1d0c64 | 2007-03-07 17:20:22 +0000 | [diff] [blame] | 75 | * |
kevinb9n | b950ad9 | 2007-03-13 02:16:16 +0000 | [diff] [blame] | 76 | * @throws CreationException if one or more errors occur during Injector |
limpbizkit | 4994bf6 | 2008-12-27 02:54:59 +0000 | [diff] [blame] | 77 | * creation |
crazyboblee | 0bfdbc6 | 2007-02-20 21:47:19 +0000 | [diff] [blame] | 78 | */ |
crazyboblee | c1d0c64 | 2007-03-07 17:20:22 +0000 | [diff] [blame] | 79 | public static Injector createInjector(Stage stage, Module... modules) { |
kevinb9n | a2915a9 | 2007-02-28 06:20:30 +0000 | [diff] [blame] | 80 | return createInjector(stage, Arrays.asList(modules)); |
kevinb9n | e5073a2 | 2007-02-20 18:06:47 +0000 | [diff] [blame] | 81 | } |
| 82 | |
crazyboblee | 0bfdbc6 | 2007-02-20 21:47:19 +0000 | [diff] [blame] | 83 | /** |
kevinb9n | a2915a9 | 2007-02-28 06:20:30 +0000 | [diff] [blame] | 84 | * Creates an injector for the given set of modules, in a given development |
| 85 | * stage. |
crazyboblee | c1d0c64 | 2007-03-07 17:20:22 +0000 | [diff] [blame] | 86 | * |
kevinb9n | b950ad9 | 2007-03-13 02:16:16 +0000 | [diff] [blame] | 87 | * @throws CreationException if one or more errors occur during Injector |
| 88 | * construction |
crazyboblee | 0bfdbc6 | 2007-02-20 21:47:19 +0000 | [diff] [blame] | 89 | */ |
crazyboblee | d9d16a0 | 2007-09-09 21:18:48 +0000 | [diff] [blame] | 90 | public static Injector createInjector(Stage stage, |
| 91 | Iterable<? extends Module> modules) { |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 92 | return new InjectorBuilder() |
| 93 | .stage(stage) |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 94 | .addModules(modules) |
| 95 | .build(); |
kevinb9n | db85d9c | 2007-02-20 05:23:46 +0000 | [diff] [blame] | 96 | } |
kevinb9n | db85d9c | 2007-02-20 05:23:46 +0000 | [diff] [blame] | 97 | } |