Created
September 20, 2013 15:31
-
-
Save cheddar/6639345 to your computer and use it in GitHub Desktop.
Example of getting tripped up on bindings that are never used
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.google.inject.Binder; | |
import com.google.inject.Guice; | |
import com.google.inject.Inject; | |
import com.google.inject.Injector; | |
import com.google.inject.Key; | |
import com.google.inject.Module; | |
import com.google.inject.TypeLiteral; | |
import com.google.inject.multibindings.MapBinder; | |
import com.google.inject.name.Names; | |
import java.util.Map; | |
/** | |
*/ | |
public class ExampleMain | |
{ | |
public static void main(String[] args) | |
{ | |
final Injector histInj = Guice.createInjector(new HistoricalModule(), new ExtensionModule()); | |
System.out.println("==========="); | |
outputMap(histInj.getInstance(Key.get(new TypeLiteral<Map<String, Extension>>(){}, Names.named("Historical")))); | |
outputMap(histInj.getInstance(Key.get(new TypeLiteral<Map<String, Extension>>(){}, Names.named("Broker")))); | |
System.out.println("||||||||||||"); | |
System.out.println(); | |
final Injector brokInj = Guice.createInjector(new BrokerModule(), new ExtensionModule()); | |
System.out.println("============"); | |
outputMap(brokInj.getInstance(Key.get(new TypeLiteral<Map<String, Extension>>(){}, Names.named("Broker")))); | |
System.out.println("||||||||||||"); | |
System.out.println(); | |
} | |
private static void outputMap(Map<String, Extension> extensions) | |
{ | |
for (Map.Entry<String, Extension> entry : extensions.entrySet()) { | |
System.out.printf("%8s => %s%n", entry.getKey(), entry.getValue().go()); | |
} | |
} | |
public static class HistoricalModule implements Module | |
{ | |
@Override | |
public void configure(Binder binder) | |
{ | |
binder.bind(CharSequence.class).toInstance("WHOZI"); | |
binder.bind(String.class).toInstance("HOWDY"); | |
} | |
} | |
public static class BrokerModule implements Module | |
{ | |
@Override | |
public void configure(Binder binder) | |
{ | |
binder.bind(String.class).toInstance("HOWDY"); | |
} | |
} | |
public static class ExtensionModule implements Module | |
{ | |
@Override | |
public void configure(Binder binder) | |
{ | |
MapBinder.newMapBinder(binder, String.class, Extension.class, Names.named("Broker")) | |
.addBinding("howdy") | |
.to(BrokerExtension.class); | |
MapBinder.newMapBinder(binder, String.class, Extension.class, Names.named("Historical")) | |
.addBinding("3") | |
.to(HistoricalExtension.class); | |
} | |
} | |
public static interface Extension | |
{ | |
public String go(); | |
} | |
public static class BrokerExtension implements Extension | |
{ | |
private final String msg; | |
@Inject | |
public BrokerExtension( | |
String msg | |
) | |
{ | |
this.msg = msg; | |
} | |
@Override | |
public String go() | |
{ | |
return msg; | |
} | |
} | |
public static class HistoricalExtension implements Extension | |
{ | |
private final CharSequence val; | |
@Inject | |
public HistoricalExtension( | |
CharSequence val | |
) | |
{ | |
this.val = val; | |
} | |
@Override | |
public String go() | |
{ | |
return val.toString(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
=========== | |
3 => WHOZI | |
howdy => HOWDY | |
|||||||||||| | |
Exception in thread "main" com.google.inject.CreationException: Guice creation errors: | |
1) No implementation for java.lang.CharSequence was bound. | |
while locating java.lang.CharSequence | |
for parameter 0 at io.druid.curator.discovery.ExampleMain$HistoricalExtension.<init>(ExampleMain.java:110) | |
at io.druid.curator.discovery.ExampleMain$ExtensionModule.configure(ExampleMain.java:72) | |
1 error | |
at com.google.inject.internal.Errors.throwCreationExceptionIfErrorsExist(Errors.java:448) | |
at com.google.inject.internal.InternalInjectorCreator.initializeStatically(InternalInjectorCreator.java:155) | |
at com.google.inject.internal.InternalInjectorCreator.build(InternalInjectorCreator.java:107) | |
at com.google.inject.Guice.createInjector(Guice.java:96) | |
at com.google.inject.Guice.createInjector(Guice.java:73) | |
at com.google.inject.Guice.createInjector(Guice.java:62) | |
at io.druid.curator.discovery.ExampleMain.main(ExampleMain.java:29) | |
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) | |
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) | |
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) | |
at java.lang.reflect.Method.invoke(Method.java:601) | |
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120) | |
Process finished with exit code 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment