Download - Dependency Injection with CDI in 15 minutes
![Page 1: Dependency Injection with CDI in 15 minutes](https://reader034.vdocument.in/reader034/viewer/2022042714/55649e6cd8b42afd4f8b4e8b/html5/thumbnails/1.jpg)
Injection in CDI in 15 minutes
by antonio goncalves@agoncal
![Page 2: Dependency Injection with CDI in 15 minutes](https://reader034.vdocument.in/reader034/viewer/2022042714/55649e6cd8b42afd4f8b4e8b/html5/thumbnails/2.jpg)
2 antonio goncalves
Antonio Goncalves
![Page 3: Dependency Injection with CDI in 15 minutes](https://reader034.vdocument.in/reader034/viewer/2022042714/55649e6cd8b42afd4f8b4e8b/html5/thumbnails/3.jpg)
History of CDI
![Page 4: Dependency Injection with CDI in 15 minutes](https://reader034.vdocument.in/reader034/viewer/2022042714/55649e6cd8b42afd4f8b4e8b/html5/thumbnails/4.jpg)
4 antonio goncalves
![Page 5: Dependency Injection with CDI in 15 minutes](https://reader034.vdocument.in/reader034/viewer/2022042714/55649e6cd8b42afd4f8b4e8b/html5/thumbnails/5.jpg)
5 antonio goncalves
History of CDI
![Page 6: Dependency Injection with CDI in 15 minutes](https://reader034.vdocument.in/reader034/viewer/2022042714/55649e6cd8b42afd4f8b4e8b/html5/thumbnails/6.jpg)
@Inject
![Page 7: Dependency Injection with CDI in 15 minutes](https://reader034.vdocument.in/reader034/viewer/2022042714/55649e6cd8b42afd4f8b4e8b/html5/thumbnails/7.jpg)
7 antonio goncalves
Dependency Injection
![Page 8: Dependency Injection with CDI in 15 minutes](https://reader034.vdocument.in/reader034/viewer/2022042714/55649e6cd8b42afd4f8b4e8b/html5/thumbnails/8.jpg)
8 antonio goncalves
Attributes
@Path("/items")public class ItemRestService {
@Inject private IsbnGenerator numberGenerator;
@Inject private ItemEJB itemEJB;
// ...}
![Page 9: Dependency Injection with CDI in 15 minutes](https://reader034.vdocument.in/reader034/viewer/2022042714/55649e6cd8b42afd4f8b4e8b/html5/thumbnails/9.jpg)
9 antonio goncalves
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; }
// ...}
![Page 10: Dependency Injection with CDI in 15 minutes](https://reader034.vdocument.in/reader034/viewer/2022042714/55649e6cd8b42afd4f8b4e8b/html5/thumbnails/10.jpg)
10 antonio goncalves
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; } // ...}
![Page 11: Dependency Injection with CDI in 15 minutes](https://reader034.vdocument.in/reader034/viewer/2022042714/55649e6cd8b42afd4f8b4e8b/html5/thumbnails/11.jpg)
11 antonio goncalves
Ambiguous Dependency Injection
![Page 12: Dependency Injection with CDI in 15 minutes](https://reader034.vdocument.in/reader034/viewer/2022042714/55649e6cd8b42afd4f8b4e8b/html5/thumbnails/12.jpg)
12 antonio goncalves
Ambiguous Dependency Injection
![Page 13: Dependency Injection with CDI in 15 minutes](https://reader034.vdocument.in/reader034/viewer/2022042714/55649e6cd8b42afd4f8b4e8b/html5/thumbnails/13.jpg)
Qualifiers
![Page 14: Dependency Injection with CDI in 15 minutes](https://reader034.vdocument.in/reader034/viewer/2022042714/55649e6cd8b42afd4f8b4e8b/html5/thumbnails/14.jpg)
14 antonio goncalves
Type Safe Dependency Injection
![Page 15: Dependency Injection with CDI in 15 minutes](https://reader034.vdocument.in/reader034/viewer/2022042714/55649e6cd8b42afd4f8b4e8b/html5/thumbnails/15.jpg)
15 antonio goncalves
Qualifier
@Path("/items")public class ItemRestService {
@Inject @ThirteenDigits private NumberGenerator numberGenerator;
@Inject private ItemEJB itemEJB;
// ...}
![Page 16: Dependency Injection with CDI in 15 minutes](https://reader034.vdocument.in/reader034/viewer/2022042714/55649e6cd8b42afd4f8b4e8b/html5/thumbnails/16.jpg)
16 antonio goncalves
Qualifier and Bean
@Qualifier@Retention(RUNTIME)@Target({FIELD, TYPE, METHOD, PARAMETER})public @interface ThirteenDigits { }
@ThirteenDigitspublic class IsbnGenerator implements NumberGenerator {
@Override public String generateNumber() { return "13-" + Math.abs(new Random().nextInt()); }}
![Page 17: Dependency Injection with CDI in 15 minutes](https://reader034.vdocument.in/reader034/viewer/2022042714/55649e6cd8b42afd4f8b4e8b/html5/thumbnails/17.jpg)
17 antonio goncalves
Qualifier with Attributes
@Path("/items")public class ItemRestService {
@Inject @NumberDigits(Digits.Thirteen, true) private NumberGenerator numberGenerator;
@Inject private ItemEJB itemEJB;
// ...}
![Page 18: Dependency Injection with CDI in 15 minutes](https://reader034.vdocument.in/reader034/viewer/2022042714/55649e6cd8b42afd4f8b4e8b/html5/thumbnails/18.jpg)
18 antonio goncalves
Multiple Qualifiers
@Path("/items")public class ItemRestService {
@Inject @ThirteenDigits @Odd private NumberGenerator numberGenerator;
@Inject private ItemEJB itemEJB;
// ...}
![Page 19: Dependency Injection with CDI in 15 minutes](https://reader034.vdocument.in/reader034/viewer/2022042714/55649e6cd8b42afd4f8b4e8b/html5/thumbnails/19.jpg)
19 antonio goncalves
Qualifier and Bean
@Qualifier@Retention(RUNTIME)@Target({FIELD, TYPE, METHOD, PARAMETER})public @interface Odd { }
@ThirteenDigits @Oddpublic class IsbnGenerator implements NumberGenerator {
@Override public String generateNumber() { return "13-" + Math.abs(new Random().nextInt()); }}
![Page 20: Dependency Injection with CDI in 15 minutes](https://reader034.vdocument.in/reader034/viewer/2022042714/55649e6cd8b42afd4f8b4e8b/html5/thumbnails/20.jpg)
@Default@Any@Named
![Page 21: Dependency Injection with CDI in 15 minutes](https://reader034.vdocument.in/reader034/viewer/2022042714/55649e6cd8b42afd4f8b4e8b/html5/thumbnails/21.jpg)
21 antonio goncalves
Programmatic Lookup
@Path("/items")public class ItemRestService {
@Inject @ThirteenDigits private Instance<NumberGenerator> numberGenerator;
@Inject private ItemEJB itemEJB;
public String getISBN() { return numberGenerator.get().generateNumber(); }
// ...}
![Page 22: Dependency Injection with CDI in 15 minutes](https://reader034.vdocument.in/reader034/viewer/2022042714/55649e6cd8b42afd4f8b4e8b/html5/thumbnails/22.jpg)
22 antonio goncalves
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()); } }
// ...}
![Page 23: Dependency Injection with CDI in 15 minutes](https://reader034.vdocument.in/reader034/viewer/2022042714/55649e6cd8b42afd4f8b4e8b/html5/thumbnails/23.jpg)
Producers
![Page 24: Dependency Injection with CDI in 15 minutes](https://reader034.vdocument.in/reader034/viewer/2022042714/55649e6cd8b42afd4f8b4e8b/html5/thumbnails/24.jpg)
24 antonio goncalves
Injectable ?
@Path("/items")public class ItemRestService {
@PersistenceContext(unitName = "myPU") private EntityManager em;
// ...}
![Page 25: Dependency Injection with CDI in 15 minutes](https://reader034.vdocument.in/reader034/viewer/2022042714/55649e6cd8b42afd4f8b4e8b/html5/thumbnails/25.jpg)
25 antonio goncalves
Injectable ?
@Path("/items")public class ItemRestService {
@Inject(unitName = "myPU") private EntityManager em;
// ...}
![Page 26: Dependency Injection with CDI in 15 minutes](https://reader034.vdocument.in/reader034/viewer/2022042714/55649e6cd8b42afd4f8b4e8b/html5/thumbnails/26.jpg)
26 antonio goncalves
Injectable ?
@Path("/items")public class ItemRestService {
@Inject(unitName = "myPU") private EntityManager em;
// ...}
![Page 27: Dependency Injection with CDI in 15 minutes](https://reader034.vdocument.in/reader034/viewer/2022042714/55649e6cd8b42afd4f8b4e8b/html5/thumbnails/27.jpg)
27 antonio goncalves
Inject with Qualifier
@Path("/items")public class ItemRestService {
@Inject @Item private EntityManager em;
// ...}
![Page 28: Dependency Injection with CDI in 15 minutes](https://reader034.vdocument.in/reader034/viewer/2022042714/55649e6cd8b42afd4f8b4e8b/html5/thumbnails/28.jpg)
28 antonio goncalves
Produce it
@Path("/items")public class ItemRestService {
@Inject @Item private EntityManager em;
// ...}
public class DatabaseProducer {
@Produces @PersistenceContext(unitName = "myPU") @Item private EntityManager em;}
![Page 29: Dependency Injection with CDI in 15 minutes](https://reader034.vdocument.in/reader034/viewer/2022042714/55649e6cd8b42afd4f8b4e8b/html5/thumbnails/29.jpg)
Strong TypingLoose coupling
![Page 30: Dependency Injection with CDI in 15 minutes](https://reader034.vdocument.in/reader034/viewer/2022042714/55649e6cd8b42afd4f8b4e8b/html5/thumbnails/30.jpg)
</>
![Page 31: Dependency Injection with CDI in 15 minutes](https://reader034.vdocument.in/reader034/viewer/2022042714/55649e6cd8b42afd4f8b4e8b/html5/thumbnails/31.jpg)
31 antonio goncalves
CDI is Much More !
![Page 33: Dependency Injection with CDI in 15 minutes](https://reader034.vdocument.in/reader034/viewer/2022042714/55649e6cd8b42afd4f8b4e8b/html5/thumbnails/33.jpg)
Q & A
![Page 34: Dependency Injection with CDI in 15 minutes](https://reader034.vdocument.in/reader034/viewer/2022042714/55649e6cd8b42afd4f8b4e8b/html5/thumbnails/34.jpg)
34 antonio goncalves
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.