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