Your SlideShare is downloading. ×
Guice2.0
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Guice2.0

2,547

Published on

Guice2.0 New Feature

Guice2.0 New Feature

Published in: Technology, News & Politics
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
2,547
On Slideshare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
84
Comments
0
Likes
3
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Guice2.0 ‐New Features and 3rd Party Modules‐ Jul 15th, 2009
  • 2. introduction Name Masaaki Yonebayashi ID id:yone098 Blog http://d.hatena.ne.jp/yone098/ Company Abby Co.,Ltd.  President
  • 3. Agenda Guice2.0 New Features Provider Methods Binding Override MultiBindings, MapBindings Private Modules Tips Third Party Modules Testing Other Language
  • 4. Guice2.0 New Features
  • 5. Guice2.0 Guice 2.0 Released May 19, 2009 URL http://code.google.com/p/google‐guice/ guice‐2.0‐no_aop.jar For Android(not support AOP) Wiki http://code.google.com/p/google‐guice/wiki/Guice20
  • 6. New Features Provider Methods
  • 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
  • 15. Provider Methods solves it
  • 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();
  • 19. Question void Provider Methods Injector injector = Guice.createInjector(new AbstractModule() { @Override public void configure() { } @Provides void sample() { } });
  • 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
  • 22. New Features Provider Methods Summary
  • 23. Provider Methods summary The definition is simple. There is no generation cost of the  Provider class. It is a short cut.
  • 24. New Features Binding Overrides
  • 25. Binding Overrides Sample Module private static Module module1() { return new AbstractModule() { protected void configure() { bind(Pet.class).to(Dog.class); } }; }
  • 26. Binding Overrides Module override Injector injector = createInjector( Modules.override(module1()) .with(new AbstractModule() { protected void configure() { bind(Pet.class).to(Cat.class); } } )); Pet pet = injector.getInstance(Pet.class); pet.run();
  • 27. Binding Overrides Module override Injector injector = createInjector( Modules.override(module1()) .with(new AbstractModule() { protected void configure() { Cat is run bind(Pet.class).to(Cat.class); } } )); Pet pet = injector.getInstance(Pet.class); pet.run();
  • 28. New Features Binding Overrides Override multiple
  • 29. Binding Overrides override multiple private static <T> Module newModule(final T bound) { return new AbstractModule() { protected void configure() { @SuppressWarnings("unchecked") Class<T> type = (Class<T>) bound.getClass(); bind(type).toInstance(bound); } }; }
  • 30. Binding Overrides override multiple Module module = Modules.override( newModule("A"),  newModule(1), newModule(0.5f) ).with( newModule("B"),  newModule(2), newModule(1.5d) ); Injector injector = createInjector(module);
  • 31. Binding Overrides override multiple assertEquals("B",  injector.getInstance(String.class)); assertEquals(0.5f,  injector.getInstance(Float.class)); assertEquals(1.5d,  injector.getInstance(Double.class));
  • 32. New Features Binding Overrides Override constant
  • 33. Binding Overrides override constant Module original = new AbstractModule() { protected void configure() { bindConstant().annotatedWith(named(“c9")).to(“ak"); } }; Module replacements = new AbstractModule() { protected void configure() { bindConstant().annotatedWith(named(“c9")).to(“yaman"); } }; Injector injector =  createInjector(Modules.override(original) .with(replacements));
  • 34. Binding Overrides override constant Injector injector =  createInjector(Modules.override(original) .with(replacements)); assertEquals("yaman", injector.getInstance( Key.get(String.class, named("c9"))));
  • 35. Binding Override summary An existing module can be recycled. The offered module can be  customized.
  • 36. New Features MultiBindings, MapBindings
  • 37. Guice2.0 dependency guice‐multibindings‐2.0.jar artifactId multibindings groupId com.google.inject.extension
  • 38. New Features MultiBindings
  • 39. MultiBindings MultiBindings Module abc = new AbstractModule() { protected void configure() { Multibinder<String> multibinder = Multibinder.newSetBinder( binder(), String.class); multibinder.addBinding().toInstance("A"); multibinder.addBinding().toInstance("B"); multibinder.addBinding().toInstance("C"); } };
  • 40. MultiBindings MultiBindings Injector injector = Guice.createInjector(abc); Set<String> abcStr =  injector.getInstance(Key.get( new TypeLiteral<Set<String>>() {} )); assertEquals(setOf("A", "B", "C"), abcStr);
  • 41. MultiBindings MultiBindings private static class Test { @Inject private Set<String> multiString; } Test test = injector.getInstance(Test.class); assertEquals(setOf("A", "B", "C"),  test.multiString);
  • 42. New Features MapBindings
  • 43. MapBindings MapBindings Module abc = new AbstractModule() { protected void configure() { MapBinder<String, String> multibinder =      MapBinder.newMapBinder( binder(), String.class, String.class); multibinder.addBinding("a").toInstance("A"); multibinder.addBinding("b").toInstance("B"); multibinder.addBinding("c").toInstance("C"); } };
  • 44. MapBindings MapBindings Injector injector = Guice.createInjector(abc); Map<String, String> abcMap =  injector.getInstance(Key.get( new TypeLiteral<Map<String, String>>(){} )); assertEquals( mapOf("a", "A", "b", "B", "c", "C"), abcMap );
  • 45. MapBindings MapBindings private static class Test { @Inject private Map<String, String> map; } Test test = injector.getInstance(Test.class); assertEquals( mapOf("a", "A", "b", "B", "c", "C"),  test.map );
  • 46. New Features MapBindings •with AnnotationType
  • 47. MapBindings MapBindings with AnnotationType /** * Ikemen annotation */ @Retention(RUNTIME) @BindingAnnotation @interface C9 { }
  • 48. MapBindings MapBindings with AnnotationType Injector injector = Guice.createInjector( new AbstractModule() { protected void configure() { MapBinder<String, Integer> mapBinder = MapBinder .newMapBinder(binder(),  String.class, Integer.class, C9.class); mapBinder.addBinding("yaman").toInstance(35); mapBinder.addBinding("shot6").toInstance(31); mapBinder.addBinding("yone098").toInstance(31); mapBinder.addBinding("skirnir").toInstance(52); } });
  • 49. MapBindings MapBindings with AnnotationType Map<String, Integer> abc =  injector.getInstance(Key.get( new TypeLiteral<Map<String, Integer>>(){}, C9.class)); assertEquals( mapOf("yaman", 35, "shot6", 31,  "yone098", 31, "skirnir", 52),  abc );
  • 50. MapBindings MapBindings with AnnotationType private static class Ikemen { @Inject @C9 Map<String, Integer> map; } Ikemen ikemen = injector.getInstance(Ikemen.class); assertEquals( mapOf("yaman", 35, "shot6", 31,  "yone098", 31, "skirnir", 52),  ikemen.map);
  • 51. MultiBindings summary The advantage of MultiBindings is  not found. :( It depends on guice‐multibindings‐ 2.0.jar.
  • 52. New Features PrivateModule
  • 53. PrivateModule PrivateModule Injector injector = Guice.createInjector( new PrivateModule() { public void configure() { bind(String.class).annotatedWith(named("c9")) .toInstance("katayama"); expose(String.class).annotatedWith(named("c9")); } }); assertEquals("katayama", injector.getInstance(Key.get(String.class, named("c9"))));
  • 54. PrivateModule PrivateModule private static class Test { @Inject @Named("c9") private String c9katayama; } Test test = injector.getInstance(Test.class); assertEquals("katayama", test.c9katayama);
  • 55. New Features PrivateModule expose
  • 56. PrivateModule PrivateModule Injector injector = Guice.createInjector( new PrivateModule() { public void configure() { bind(String.class).annotatedWith(named("c9")) .toInstance("katayama"); expose(String.class).annotatedWith(named("c9")); } }); assertEquals("katayama", injector.getInstance(Key.get(String.class, named("c9"))));
  • 57. PrivateModule PrivateModule Injector injector = Guice.createInjector( new PrivateModule() { public void configure() { bind(String.class).annotatedWith(named("c9")) .toInstance("katayama"); expose(String.class).annotatedWith(named("c9")); } }); assertEquals("katayama", injector.getInstance(Key.get(String.class, named("c9")))); demo
  • 58. New Features PrivateModule @Exposed
  • 59. PrivateModule PrivateModule Injector injector = Guice.createInjector( new PrivateModule(){ protected void configure() { } @Provides @Named("c9") @Exposed  String provideKata() { return "KATA"; } }, new AbstractModule() { protected void configure() { } @Provides @Named("yaman") String provideYama(@Named("c9") String c9) { return c9 + "YAMA"; } });
  • 60. PrivateModule PrivateModule assertEquals("KATAYAMA",  injector.getInstance(Key.get( String.class, named("yaman"))) ); demo
  • 61. PrivateModule Use it positively if you make the  framework. It will become easy to design the  module. The module can be safely used by  limiting scope.
  • 62. Guice2.0
  • 63. New Features InjectionListener demo
  • 64. New Features Names.bindProperties demo
  • 65. Guice2.0 Third Party Modules
  • 66. Third Party Modules Testing
  • 67. Third Party Modules GuiceBerry JUnit and integration testing http://code.google.com/p/guiceberry/ AtUnit JUnit and mocking frameworks (JMock,  EasyMock) http://code.google.com/p/atunit/
  • 68. Third Party Modules Other Language
  • 69. Third Party Modules Groovy‐Guice(Groovy) http://code.google.com/p/groovy‐guice/ Ninject(.NET) http://ninject.org/ Snake Guice(Python) http://code.google.com/p/snake‐guice/ Smartypants‐IOC(Flex) http://code.google.com/p/smartypants‐ ioc/
  • 70. Guice2.0 Summary
  • 71. summary Enjoy! Guice2.0
  • 72. Haiku Today very hot No Haiku❤
  • 73. Thank you! :)

×