Google Guice  Kirill Afanasjev Software Architect       jug.lv    Riga, Latvia
Overview   Dependency Injection   Why Guice   Getting Guice   Using Guice   Advanced Guice
Code without DI public void sendButtonClicked() {     String text = messageArea.getText();     Validator validator = new M...
Using factories public void sendButtonClicked() {      String text = messageArea.getText();      Validator validator = Val...
Testable now public void testSendButton() {      MockSender mockSender = new MockSender();      SenderFactory.setInstance(...
Well, not really public void testSendButton() {       MockSender mockSender = new MockSender();       SenderFactory.setIns...
Dependency injection private Validator validator; private MailSender mailSender; public MailForm(Validator validator, Mail...
Testing now public void testSendButton() {      MockSender mockSender = new MockSender();      Validator validator = new V...
Why DI frameworks   Avoid boilerplate code   AOP   Integrate your DI with http session/request,    data access APIs, e....
What is Guice   Open source dependency injection framework   License : Apache License 2.0   Developer : Google
Why Guice   Java API for configuration   Easier to use   Line numbers in error messages   Less overhead   Less featur...
Getting Guice   http://code.google.com/p/google-guice/   http://mvnrepository.com/   Small – 865 KB   Version without ...
Dependency injection with Guice private Validator validator; private MailSender mailSender; @Inject public MailForm(Valida...
Creating instance of MailForm Injector injector =     Guice.createInjector(new YourAppModule()); MailForm mailForm = injec...
Bindings public class YourAppModule extends AbstractModule {     protected void configure() {                bind(MailServ...
Providers public class YourAppModule extends AbstractModule {      protected void configure() {                 bind(Valid...
Injecting fields @Inject private Validator validator; @Inject private MailSender mailSender; public MailForm() { }
Injecting with setter methods   private Validator validator;   public MailForm() {   }   @Inject   public void setValidato...
Optional injection   Inject(optional=true)   Ignores values for which no bindings are    avalaible   Possible with sett...
Bind instance    protected void configure() {         bind(Integer.class).annotatedWith(ThreadCount.cla         ss).toInst...
Two implementations  protected void configure() {             bind(Validator.class).to(ValidatorImpl.class);             b...
Two implementationsException in thread "main" com.google.inject.CreationException:Guice configuration errors:1) Error at l...
Two implementations  protected void configure() {                bind(Validator.class).to(ValidatorImpl.class);           ...
Binding annotation@Retention(RetentionPolicy.RUNTIME)@Target({ElementType.FIELD, ElementType.PARAMETER})@BindingAnnotation...
Implicit binding      @Inject      public MailForm(         @ImplementedBy(StrictValidator.class) Validator         Valida...
Static injection   protected void configure() {               bind(Validator.class).to(ValidatorImpl.class);              ...
Scoping  protected void configure() {             bind(Validator.class).to(ValidatorImpl.class)                   .in(Scop...
Multibindings  Multibinder<Plugin> pluginBinder =  Multibinder.newSetBinder(binder(), Plugin.class);  pluginBinder.addBind...
Grapher   Describe the object graph in detail   Show bindings and dependencies from several    classes in a complex appl...
Advanced Guice   AOP   Warp   Google Gin   Spring integration   Using guice with Servlets
Why AOP   Transaction handling   Logging   Security   Exception handling   e.t.c
AOP   void bindInterceptor(    Match <? super Class<?>> classMatcher,    Matcher<? super Method> methodMatcher,    Method...
AOP   You can not match on private and final methods    due to technical limitations in Java   Only works for objects Gu...
Warp   http://www.wideplay.com/   Eco-system for Google Guice   Thin lightweight modules for Guice applications   Pers...
Warp-persist   Supports : Hibernate/JPA/Db4Objects   Inject DAOs & Repositories   Flexible units-of-work   Declarative...
Google Gin   Automatic dependency injection for GWT client-    side code   Code generation   Little-to-no runtime overh...
Spring integration   http://code.google.com/p/guice-spring/   @Named(”mySpringBean”)
JSR-330   javax.inject   @Inject   @Named   @Qualifier   @Scope   @Singleton   e.t.c   Supported by Guice, Spring,...
Using Guice with Servlets   @RequestScoped   @SessionScoped   ServletModule   serve(”/admin”).with(AdminPanelServlet.c...
Thank you   Questions ?
Upcoming SlideShare
Loading in …5
×

Jug Guice Presentation

1,700 views

Published on

Published in: Technology, Design
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,700
On SlideShare
0
From Embeds
0
Number of Embeds
84
Actions
Shares
0
Downloads
26
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Jug Guice Presentation

  1. 1. Google Guice Kirill Afanasjev Software Architect jug.lv Riga, Latvia
  2. 2. Overview Dependency Injection Why Guice Getting Guice Using Guice Advanced Guice
  3. 3. Code without DI public void sendButtonClicked() { String text = messageArea.getText(); Validator validator = new MailValidator(); validator.validate(text); MailSender sender = new MailSender(); sender.send(text); }
  4. 4. Using factories public void sendButtonClicked() { String text = messageArea.getText(); Validator validator = ValidatorFactory.get(); validator.validate(text); MailSender sender = SenderFactory.get(); sender.send(text); }
  5. 5. Testable now public void testSendButton() { MockSender mockSender = new MockSender(); SenderFactory.setInstance(mockSender); MailForm form = new MailForm(); form.getMessageArea().setText("Some text"); form.sendButtonClicked(); assertEquals("Some text", mockSender.getSentText()); SenderFactory.clearInstance(); }
  6. 6. Well, not really public void testSendButton() { MockSender mockSender = new MockSender(); SenderFactory.setInstance(mockSender); try { MailForm form = new MailForm(); form.getMessageArea().setText("Some text"); form.sendButtonClicked(); assertEquals("Some text", mockSender.getSentText()); } finally { SenderFactory.clearInstance(); } }
  7. 7. Dependency injection private Validator validator; private MailSender mailSender; public MailForm(Validator validator, MailSender mailSender) { this.validator = validator; this.mailSender = mailSender; } public void sendButtonClicked() { …..
  8. 8. Testing now public void testSendButton() { MockSender mockSender = new MockSender(); Validator validator = new Validator(); MailForm form = new MailForm(validator, mockSender); form.getMessageArea().setText("Some text"); form.sendButtonClicked(); assertEquals("Some text", mockSender.getSentText()); }
  9. 9. Why DI frameworks Avoid boilerplate code AOP Integrate your DI with http session/request, data access APIs, e.t.c Separate dependencies configuration from code Makes life easier
  10. 10. What is Guice Open source dependency injection framework License : Apache License 2.0 Developer : Google
  11. 11. Why Guice Java API for configuration Easier to use Line numbers in error messages Less overhead Less features, too DI in GWT client-side code (using GIN)
  12. 12. Getting Guice http://code.google.com/p/google-guice/ http://mvnrepository.com/ Small – 865 KB Version without AOP, suitable for Android – 470KB Lacks fast reflection and line numbers in errors
  13. 13. Dependency injection with Guice private Validator validator; private MailSender mailSender; @Inject public MailForm(Validator validator, MailSender mailSender) { this.validator = validator; this.mailSender = mailSender; }
  14. 14. Creating instance of MailForm Injector injector = Guice.createInjector(new YourAppModule()); MailForm mailForm = injector.getInstance(MailForm.class);
  15. 15. Bindings public class YourAppModule extends AbstractModule { protected void configure() { bind(MailService.class).to(MailServiceImpl.class); bind(Validator.class).to(ValidatorImpl.class); } }
  16. 16. Providers public class YourAppModule extends AbstractModule { protected void configure() { bind(Validator.class).to(ValidatorImpl.class); } @Provides MailService getMailService() { MailService service = new MailService(); return service; } ….
  17. 17. Injecting fields @Inject private Validator validator; @Inject private MailSender mailSender; public MailForm() { }
  18. 18. Injecting with setter methods private Validator validator; public MailForm() { } @Inject public void setValidator(Validator validator) { this.validator = validator; }
  19. 19. Optional injection Inject(optional=true) Ignores values for which no bindings are avalaible Possible with setter methods only
  20. 20. Bind instance protected void configure() { bind(Integer.class).annotatedWith(ThreadCount.cla ss).toInstance(4); } .. @ThreadCount int threadCount; IDE autocomplete, find usages e.t.c
  21. 21. Two implementations protected void configure() { bind(Validator.class).to(ValidatorImpl.class); bind(Validator.class).to(StrictValidatorImpl.class); }
  22. 22. Two implementationsException in thread "main" com.google.inject.CreationException:Guice configuration errors:1) Error at lv.jug.MailService.configure(YourAppModule.java:12):A binding to lv.jug.MailService was already configured atlv.jug.YourAppModule.configure(YourAppModule.java:11)
  23. 23. Two implementations protected void configure() { bind(Validator.class).to(ValidatorImpl.class); bind(Validator.class) .annotatedWith(Strict.class) .to(StrictValidatorImpl.class); } …. @Inject public MailForm(@Strict Validator validator) { this.validator = validator; }
  24. 24. Binding annotation@Retention(RetentionPolicy.RUNTIME)@Target({ElementType.FIELD, ElementType.PARAMETER})@BindingAnnotationpublic @interface Strict {}
  25. 25. Implicit binding @Inject public MailForm( @ImplementedBy(StrictValidator.class) Validator Validator validator, MailSender mailSender) { this.validator = validator; this.mailSender = mailSender; }
  26. 26. Static injection protected void configure() { bind(Validator.class).to(ValidatorImpl.class); requestStaticInjection(OurClass.class); } … … @Inject static Validator validator;
  27. 27. Scoping protected void configure() { bind(Validator.class).to(ValidatorImpl.class) .in(Scopes.SINGLETON); bind(MailService.class).to(MailServiceImpl.class) .asEagerSingleton(); }
  28. 28. Multibindings Multibinder<Plugin> pluginBinder = Multibinder.newSetBinder(binder(), Plugin.class); pluginBinder.addBinding().to(SomePluginImpl.class); pluginBinder.addBinding().to(AnotherPluginImpl.class); …. @Inject Set<Plugin> plugins
  29. 29. Grapher Describe the object graph in detail Show bindings and dependencies from several classes in a complex application in a unified diagram Generates .dot file
  30. 30. Advanced Guice AOP Warp Google Gin Spring integration Using guice with Servlets
  31. 31. Why AOP Transaction handling Logging Security Exception handling e.t.c
  32. 32. AOP void bindInterceptor( Match <? super Class<?>> classMatcher, Matcher<? super Method> methodMatcher, MethodInterceptor... interceptors) public interface MethodInterceptor extends Interceptor { Object invoke (MethodInvocation invocation) throws Throwable }
  33. 33. AOP You can not match on private and final methods due to technical limitations in Java Only works for objects Guice creates
  34. 34. Warp http://www.wideplay.com/ Eco-system for Google Guice Thin lightweight modules for Guice applications Persistence Transactions Servlets
  35. 35. Warp-persist Supports : Hibernate/JPA/Db4Objects Inject DAOs & Repositories Flexible units-of-work Declarative transaction management ( @Transactional ) Your own AOP for transactions management @Finder(query="from Person")
  36. 36. Google Gin Automatic dependency injection for GWT client- side code Code generation Little-to-no runtime overhead, compared to manual DI Uses Guice binding language http://code.google.com/p/google-gin/
  37. 37. Spring integration http://code.google.com/p/guice-spring/ @Named(”mySpringBean”)
  38. 38. JSR-330 javax.inject @Inject @Named @Qualifier @Scope @Singleton e.t.c Supported by Guice, Spring, EJB
  39. 39. Using Guice with Servlets @RequestScoped @SessionScoped ServletModule serve(”/admin”).with(AdminPanelServlet.class) serve("*.html", "/my/*").with(MyServlet.class) filter(”/*”).through(MyFilter.class) @Inject @RequestParameters Map<String, String[]> params;
  40. 40. Thank you Questions ?

×