Date post: | 26-May-2015 |
Category: |
Technology |
Upload: | antonio-goncalves |
View: | 1,199 times |
Download: | 0 times |
Injection in CDI in 15 minutes
by antonio goncalves@agoncal
2 antonio goncalves
Antonio Goncalves
History of CDI
4 antonio goncalves
5 antonio goncalves
History of CDI
@Inject
7 antonio goncalves
Dependency Injection
8 antonio goncalves
Attributes
@Path("/items")public class ItemRestService {
@Inject private IsbnGenerator numberGenerator;
@Inject private ItemEJB itemEJB;
// ...}
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; }
// ...}
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; } // ...}
11 antonio goncalves
Ambiguous Dependency Injection
12 antonio goncalves
Ambiguous Dependency Injection
Qualifiers
14 antonio goncalves
Type Safe Dependency Injection
15 antonio goncalves
Qualifier
@Path("/items")public class ItemRestService {
@Inject @ThirteenDigits private NumberGenerator numberGenerator;
@Inject private ItemEJB itemEJB;
// ...}
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()); }}
17 antonio goncalves
Qualifier with Attributes
@Path("/items")public class ItemRestService {
@Inject @NumberDigits(Digits.Thirteen, true) private NumberGenerator numberGenerator;
@Inject private ItemEJB itemEJB;
// ...}
18 antonio goncalves
Multiple Qualifiers
@Path("/items")public class ItemRestService {
@Inject @ThirteenDigits @Odd private NumberGenerator numberGenerator;
@Inject private ItemEJB itemEJB;
// ...}
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()); }}
@Default@Any@Named
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(); }
// ...}
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()); } }
// ...}
Producers
24 antonio goncalves
Injectable ?
@Path("/items")public class ItemRestService {
@PersistenceContext(unitName = "myPU") private EntityManager em;
// ...}
25 antonio goncalves
Injectable ?
@Path("/items")public class ItemRestService {
@Inject(unitName = "myPU") private EntityManager em;
// ...}
26 antonio goncalves
Injectable ?
@Path("/items")public class ItemRestService {
@Inject(unitName = "myPU") private EntityManager em;
// ...}
27 antonio goncalves
Inject with Qualifier
@Path("/items")public class ItemRestService {
@Inject @Item private EntityManager em;
// ...}
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;}
Strong TypingLoose coupling
</>
31 antonio goncalves
CDI is Much More !
Q & A
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.