Dependency Injection with CDI in 15 minutes
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

Dependency Injection with CDI in 15 minutes

on

  • 1,267 views

This 15 minutes talk focuses on Dependency Injection with CDI

This 15 minutes talk focuses on Dependency Injection with CDI

Statistics

Views

Total Views
1,267
Views on SlideShare
1,238
Embed Views
29

Actions

Likes
2
Downloads
18
Comments
0

2 Embeds 29

https://twitter.com 26
http://www.linkedin.com 3

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Dependency Injection with CDI in 15 minutes Presentation Transcript

  • 1. Injection in CDI in 15 minutes by antonio goncalves @agoncal
  • 2. Antonio Goncalves antonio goncalves 2
  • 3. History of CDI
  • 4. antonio goncalves 4
  • 5. History of CDI antonio goncalves 5
  • 6. @Inject
  • 7. Dependency Injection antonio goncalves 7
  • 8. Attributes @Path("/items") public class ItemRestService { @Inject private IsbnGenerator numberGenerator; @Inject private ItemEJB itemEJB; // ... } antonio goncalves 8
  • 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. 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. Ambiguous Dependency Injection antonio goncalves 11
  • 12. Ambiguous Dependency Injection antonio goncalves 12
  • 13. Qualifiers
  • 14. Type Safe Dependency Injection antonio goncalves 14
  • 15. Qualifier @Path("/items") public class ItemRestService { @Inject @ThirteenDigits private NumberGenerator numberGenerator; @Inject private ItemEJB itemEJB; // ... } antonio goncalves 15
  • 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. Qualifier with Attributes @Path("/items") public class ItemRestService { @Inject @NumberDigits(Digits.Thirteen, true) private NumberGenerator numberGenerator; @Inject private ItemEJB itemEJB; // ... } antonio goncalves 17
  • 18. Multiple Qualifiers @Path("/items") public class ItemRestService { @Inject @ThirteenDigits @Odd private NumberGenerator numberGenerator; @Inject private ItemEJB itemEJB; // ... } antonio goncalves 18
  • 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. @Default @Any @Named
  • 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. 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. Producers
  • 24. Injectable ? @Path("/items") public class ItemRestService { @PersistenceContext(unitName = "myPU") private EntityManager em; // ... } antonio goncalves 24
  • 25. Injectable ? @Path("/items") public class ItemRestService { @Inject(unitName = "myPU") private EntityManager em; // ... } antonio goncalves 25
  • 26. Injectable ? @Path("/items") public class ItemRestService { @Inject(unitName = "myPU") private EntityManager em; // ... } antonio goncalves 26
  • 27. Inject with Qualifier @Path("/items") public class ItemRestService { @Inject @Item private EntityManager em; // ... } antonio goncalves 27
  • 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. Strong Typing Loose coupling
  • 30. </>
  • 31. CDI is Much More ! antonio goncalves 31
  • 32. Thanks www.antoniogoncalves.org antonio.goncalves@gmail.com @agoncal @devoxxfr @lescastcodeurs
  • 33. Q&A
  • 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