@Override public final synchronized void configure(Binder builder) { checkState(this.binder == null, "Re-entry is not allowed."); this.binder = checkNotNull(builder, "builder"); try { configure(); } finally { this.binder = null; } }
// Here we use an anonymous class as the "configuring" module. In real life, you would // probably use a standalone module. Injector injector = Guice.createInjector(new AbstractModule() { @Override protected void configure() { // No cheese please! bindConstant().annotatedWith(Names.named("addCheese")).to(false); bind(PantryModule.class); } }); Module configuredConditionalModule = injector.getInstance(PantryModule.class);
Injector instrumentInjector = injector.createChildInjector(new AbstractModule() { @Override protected void configure() { bind(InstrumentConfig.class).toInstance(instrumentConfig); Class<? extends Instrument> clazz = Util.lenientClassForName(className).asSubclass(Instrument.class); builder.add(instrumentInjector.getInstance(clazz)); } catch (ClassNotFoundException e) { throw new InvalidCommandException("Cannot find instrument class '%s'", className);
Injector i = Guice.createInjector(new AbstractModule() { protected void configure() { bind(Boolean.class).toInstance(Boolean.TRUE); Foo foo = i.getInstance(Foo.class); assertThat(foo.x, is(true)); public void memberInjection() { Foo foo = new Foo(); i.injectMembers(foo); assertThat(foo.x, is(true));
public class TestBase { protected Injector injector = Guice.createInjector(new AbstractModule() { @Override protected void configure() { bind(HelloService.class); } }); @Before public void setup () { injector.injectMembers(this); } }
public class PipelineRunner { @Inject Injector injector; // rarely a good idea, but necessary here public D createD(final String inputForA) { Module module = new AbstractModule() { @Override public void configure() { bindConstant(inputForA).annotatedWith(PipelineInput.class); } }; return injector.createChildInjector(new PipelineModule(), module) .getInstance(D.class); } }
/** @see Binder#bindConstant() */ protected AnnotatedConstantBindingBuilder bindConstant() { return binder().bindConstant(); }
Injector injector = Guice.createInjector(new AbstractModule() theGuiceCreatedObject = injector.getProvider(X.class);
/** * @see Binder#getProvider(Key) * @since 2.0 */ protected <T> Provider<T> getProvider(Key<T> key) { return binder().getProvider(key); }
public class FizzFuzz { @Inject @Named("red") private String service; public static void main(String[] args) { FizzFuzz fizzFuzz = Guice.createInjector(new AbstractModule() { @Override protected void configure() { bindConstant().annotatedWith(Names.named("red")).to("red-service"); } }).getInstance(FizzFuzz.class); System.out.println(fizzFuzz.service); } }
/** * @see Binder#bindListener(com.google.inject.matcher.Matcher, com.google.inject.spi.TypeListener) * @since 2.0 */ protected void bindListener(Matcher<? super TypeLiteral<?>> typeMatcher, TypeListener listener) { binder().bindListener(typeMatcher, listener); }
/** * @see Binder#requestInjection(Object) * @since 2.0 */ protected void requestInjection(Object instance) { binder().requestInjection(instance); }
/** @see Binder#requestStaticInjection(Class[]) */ protected void requestStaticInjection(Class<?>... types) { binder().requestStaticInjection(types); }
/** * @see Binder#bindInterceptor(com.google.inject.matcher.Matcher, * com.google.inject.matcher.Matcher, org.aopalliance.intercept.MethodInterceptor[]) */ protected void bindInterceptor( Matcher<? super Class<?>> classMatcher, Matcher<? super Method> methodMatcher, org.aopalliance.intercept.MethodInterceptor... interceptors) { binder().bindInterceptor(classMatcher, methodMatcher, interceptors); } /*end[AOP]*/
/** * @see Binder#convertToTypes * @since 2.0 */ protected void convertToTypes( Matcher<? super TypeLiteral<?>> typeMatcher, TypeConverter converter) { binder().convertToTypes(typeMatcher, converter); }
/** * @see Binder#currentStage() * @since 2.0 */ protected Stage currentStage() { return binder().currentStage(); }