Guice2.0

3,013 views

Published on

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
3,013
On SlideShare
0
From Embeds
0
Number of Embeds
491
Actions
Shares
0
Downloads
90
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Guice2.0

  1. 1. Guice2.0 ‐New Features and 3rd Party Modules‐ Jul 15th, 2009
  2. 2. introduction Name Masaaki Yonebayashi ID id:yone098 Blog http://d.hatena.ne.jp/yone098/ Company Abby Co.,Ltd.  President
  3. 3. Agenda Guice2.0 New Features Provider Methods Binding Override MultiBindings, MapBindings Private Modules Tips Third Party Modules Testing Other Language
  4. 4. Guice2.0 New Features
  5. 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. 6. New Features Provider Methods
  7. 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. 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. 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. 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. 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. 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. 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. 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. 15. Provider Methods solves it
  16. 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. 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. 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. 19. Question void Provider Methods Injector injector = Guice.createInjector(new AbstractModule() { @Override public void configure() { } @Provides void sample() { } });
  20. 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. 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. 22. New Features Provider Methods Summary
  23. 23. Provider Methods summary The definition is simple. There is no generation cost of the  Provider class. It is a short cut.
  24. 24. New Features Binding Overrides
  25. 25. Binding Overrides Sample Module private static Module module1() { return new AbstractModule() { protected void configure() { bind(Pet.class).to(Dog.class); } }; }
  26. 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. 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. 28. New Features Binding Overrides Override multiple
  29. 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. 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. 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. 32. New Features Binding Overrides Override constant
  33. 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. 34. Binding Overrides override constant Injector injector =  createInjector(Modules.override(original) .with(replacements)); assertEquals("yaman", injector.getInstance( Key.get(String.class, named("c9"))));
  35. 35. Binding Override summary An existing module can be recycled. The offered module can be  customized.
  36. 36. New Features MultiBindings, MapBindings
  37. 37. Guice2.0 dependency guice‐multibindings‐2.0.jar artifactId multibindings groupId com.google.inject.extension
  38. 38. New Features MultiBindings
  39. 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. 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. 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. 42. New Features MapBindings
  43. 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. 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. 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. 46. New Features MapBindings •with AnnotationType
  47. 47. MapBindings MapBindings with AnnotationType /** * Ikemen annotation */ @Retention(RUNTIME) @BindingAnnotation @interface C9 { }
  48. 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. 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. 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. 51. MultiBindings summary The advantage of MultiBindings is  not found. :( It depends on guice‐multibindings‐ 2.0.jar.
  52. 52. New Features PrivateModule
  53. 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. 54. PrivateModule PrivateModule private static class Test { @Inject @Named("c9") private String c9katayama; } Test test = injector.getInstance(Test.class); assertEquals("katayama", test.c9katayama);
  55. 55. New Features PrivateModule expose
  56. 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. 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. 58. New Features PrivateModule @Exposed
  59. 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. 60. PrivateModule PrivateModule assertEquals("KATAYAMA",  injector.getInstance(Key.get( String.class, named("yaman"))) ); demo
  61. 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. 62. Guice2.0
  63. 63. New Features InjectionListener demo
  64. 64. New Features Names.bindProperties demo
  65. 65. Guice2.0 Third Party Modules
  66. 66. Third Party Modules Testing
  67. 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. 68. Third Party Modules Other Language
  69. 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. 70. Guice2.0 Summary
  71. 71. summary Enjoy! Guice2.0
  72. 72. Haiku Today very hot No Haiku❤
  73. 73. Thank you! :)

×