Dependency Injection with CDI in 15 minutes

1,984 views
1,727 views

Published on

This 15 minutes talk focuses on Dependency Injection with CDI

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

No Downloads
Views
Total views
1,984
On SlideShare
0
From Embeds
0
Number of Embeds
51
Actions
Shares
0
Downloads
40
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Dependency Injection with CDI in 15 minutes

  1. 1. Injection in CDI in 15 minutes by antonio goncalves @agoncal
  2. 2. Antonio Goncalves antonio goncalves 2
  3. 3. History of CDI
  4. 4. antonio goncalves 4
  5. 5. History of CDI antonio goncalves 5
  6. 6. @Inject
  7. 7. Dependency Injection antonio goncalves 7
  8. 8. Attributes @Path("/items") public class ItemRestService { @Inject private IsbnGenerator numberGenerator; @Inject private ItemEJB itemEJB; // ... } antonio goncalves 8
  9. 9. Constructor @Path("/items") public class ItemRestService { private IsbnGenerator numberGenerator; private ItemEJB itemEJB; @Inject public ItemRestService(IsbnGenerator numberGenerator, ItemEJB itemEJB) { this.numberGenerator = numberGenerator; this.itemEJB = itemEJB; } // ... } antonio goncalves 9
  10. 10. Setter @Path("/items") public class ItemRestService { private IsbnGenerator numberGenerator; private ItemEJB itemEJB; @Inject public void setNumberGenerator(IsbnGenerator gen) { this.numberGenerator = gen; } @Inject public void setItemEJB(ItemEJB itemEJB) { this.itemEJB = itemEJB; } // ... } antonio goncalves 10
  11. 11. Ambiguous Dependency Injection antonio goncalves 11
  12. 12. Ambiguous Dependency Injection antonio goncalves 12
  13. 13. Qualifiers
  14. 14. Type Safe Dependency Injection antonio goncalves 14
  15. 15. Qualifier @Path("/items") public class ItemRestService { @Inject @ThirteenDigits private NumberGenerator numberGenerator; @Inject private ItemEJB itemEJB; // ... } antonio goncalves 15
  16. 16. Qualifier and Bean @Qualifier @Retention(RUNTIME) @Target({FIELD, TYPE, METHOD, PARAMETER}) public @interface ThirteenDigits { } @ThirteenDigits public class IsbnGenerator implements NumberGenerator { @Override public String generateNumber() { return "13-" + Math.abs(new Random().nextInt()); } } antonio goncalves 16
  17. 17. Qualifier with Attributes @Path("/items") public class ItemRestService { @Inject @NumberDigits(Digits.Thirteen, true) private NumberGenerator numberGenerator; @Inject private ItemEJB itemEJB; // ... } antonio goncalves 17
  18. 18. Multiple Qualifiers @Path("/items") public class ItemRestService { @Inject @ThirteenDigits @Odd private NumberGenerator numberGenerator; @Inject private ItemEJB itemEJB; // ... } antonio goncalves 18
  19. 19. Qualifier and Bean @Qualifier @Retention(RUNTIME) @Target({FIELD, TYPE, METHOD, PARAMETER}) public @interface Odd { } @ThirteenDigits @Odd public class IsbnGenerator implements NumberGenerator { @Override public String generateNumber() { return "13-" + Math.abs(new Random().nextInt()); } } antonio goncalves 19
  20. 20. @Default @Any @Named
  21. 21. Programmatic Lookup @Path("/items") public class ItemRestService { @Inject @ThirteenDigits private Instance<NumberGenerator> numberGenerator; @Inject private ItemEJB itemEJB; public String getISBN() { return numberGenerator.get().generateNumber(); } // ... } antonio goncalves 21
  22. 22. Programmatic Lookup @Path("/items") public class ItemRestService { @Inject @Any private Instance<NumberGenerator> numberGenerators; @Inject private ItemEJB itemEJB; public void generateNumbers() { for(NumberGenerator generator: numberGenerators) { display(generator.generateNumber()); } } // ... } antonio goncalves 22
  23. 23. Producers
  24. 24. Injectable ? @Path("/items") public class ItemRestService { @PersistenceContext(unitName = "myPU") private EntityManager em; // ... } antonio goncalves 24
  25. 25. Injectable ? @Path("/items") public class ItemRestService { @Inject(unitName = "myPU") private EntityManager em; // ... } antonio goncalves 25
  26. 26. Injectable ? @Path("/items") public class ItemRestService { @Inject(unitName = "myPU") private EntityManager em; // ... } antonio goncalves 26
  27. 27. Inject with Qualifier @Path("/items") public class ItemRestService { @Inject @Item private EntityManager em; // ... } antonio goncalves 27
  28. 28. Produce it @Path("/items") public class ItemRestService { @Inject @Item private EntityManager em; // ... } public class DatabaseProducer { @Produces @PersistenceContext(unitName = "myPU") @Item private EntityManager em; } antonio goncalves 28
  29. 29. Strong Typing Loose coupling
  30. 30. </>
  31. 31. CDI is Much More ! antonio goncalves 31
  32. 32. Thanks www.antoniogoncalves.org antonio.goncalves@gmail.com @agoncal @devoxxfr @lescastcodeurs
  33. 33. Q&A
  34. 34. Creative Commons ● ● ● Attribution — You must attribute the work in the manner specified by the author or licensor (but not in any way that suggests that they endorse you or your use of the work). Noncommercial — You may not use this work for commercial purposes. Share Alike — If you alter, transform, or build upon this work, you may distribute the resulting work only under the same or similar license to this one. antonio goncalves 34

×