7. Provider Methods
example public interface Pet {
void run();
}
public class Cat implements Pet { public class Dog implements Pet {
private String name; private String name;
public Cat(String name) { public Dog(String name) {
this.name = name; this.name = name;
} }
public void run() { public void run() {
System.out.println( System.out.println(
name + “ Cat is run"); name + “ Dog is run");
} }
} }
8. Provider Methods
Guice1.0
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
public void configure() {
bind(Pet.class).toProvider(new Provider<Pet>() {
public Pet get() {
return new Dog("new Provider<Pet>");
}
}).in(Singleton.class);
}
});
Pet pet = injector.getInstance(Pet.class);
pet.run();
9. Provider Methods
Guice1.0
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
public void configure() {
bind(Pet.class).toProvider(new Provider<Pet>() {
public Pet get() {
new Provider<Pet> Dog is run
return new Dog("new Provider<Pet>");
}
}).in(Singleton.class);
}
});
Pet pet = injector.getInstance(Pet.class);
pet.run();
10. Provider Methods
Guice2.0
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
public void configure() {
}
@Provides @Singleton
Pet providePet() {
return new Cat("@Provides");
}
});
Pet pet = injector.getInstance(Pet.class);
pet.run();
11. Provider Methods
Guice2.0
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
public void configure() {
}
@Provides @Singleton
@Provides Cat is run
Pet providePet() {
return new Cat("@Provides");
}
});
Pet pet = injector.getInstance(Pet.class);
pet.run();
12. Question
Guice2.0
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
public void configure() {
}
@Provides @Singleton
Pet providePet() {
return new Cat("@Provides");
}
@Provides @Singleton
Pet providePet2() {
return new Cat("@Provides2");
}
});
13. Provider Methods
CreationException :(
Exception in thread "main"
com.google.inject.CreationException: Guice creation
errors:
1) A binding to samples.providermethod.Pet was already
configured at
samples.providermethod.ProviderMethods$1.providePet().
at
samples.providermethod.ProviderMethods$1.providePet2(P
roviderMethods.java:33)
14. Provider Methods
CreationException :(
Exception in thread "main"
com.google.inject.CreationException: Guice creation
errors:
1) A binding to samples.providermethod.Pet was already
configured at
samples.providermethod.ProviderMethods$1.providePet().
at
samples.providermethod.ProviderMethods$1.providePet2(P
roviderMethods.java:33)
demo
16. solved it
Provides with Named
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
public void configure() {
}
@Provides @Singleton
Pet providePet() {
return new Cat("@Provides");
}
@Provides @Singleton @Named(“NYAAA”)
Pet providePet2() {
return new Cat("@Provides2");
}
});
17. solved it
named
import static
com.google.inject.name.Names.named;
Pet pet = injector.getInstance(Pet.class);
pet.run();
Pet nyaaa = injector.getInstance(
Key.get(Pet.class, named("NYAAA")));
nyaaa.run();
18. solved it
named
import static
com.google.inject.name.Names.named;
Pet pet = injector.getInstance(Pet.class);
pet.run();
@Provides Cat is run
@Provedes2 Cat is run
Pet nyaaa = injector.getInstance(
Key.get(Pet.class, named("NYAAA")));
nyaaa.run();
20. Provider Methods
CreationException :(
Exception in thread "main"
com.google.inject.CreationException: Guice
creation errors:
1) Provider methods must return a value. Do not
return void.
at
samples.providermethod.ProviderMethods$1.sample(P
roviderMethods.java:40)
21. Provider Methods
CreationException :(
Exception in thread "main"
com.google.inject.CreationException: Guice
creation errors:
1) Provider methods must return a value. Do not
return void.
at
samples.providermethod.ProviderMethods$1.sample(P
roviderMethods.java:40)
demo
54. PrivateModule
PrivateModule
private static class Test {
@Inject
@Named("c9")
private String c9katayama;
}
Test test = injector.getInstance(Test.class);
assertEquals("katayama", test.c9katayama);