Ce diaporama a bien été signalé.
Nous utilisons votre profil LinkedIn et vos données d’activité pour vous proposer des publicités personnalisées et pertinentes. Vous pouvez changer vos préférences de publicités à tout moment.
CDI : How do I ?
by antonio goncalves
@agoncal
2@agoncal
Antonio Goncalves
What is CDI ?
4@agoncal
What is CDI ?
● Dependency injection
● Lose coupling, strong typing
● Context management
● Interceptors and deco...
5@agoncal
History of CDI
6@agoncal
Implementations
Demo
-
Creating a Web App
8@agoncal
Demos with JBoss Forge
● Java EE scaffolding tool
● Shell commands
● CRUD application
● Gets you start quickly
●...
9@agoncal
Demo: Creating a Web App
Dependency Injection
11@agoncal
How Do I ?
12@agoncal
Use @Inject !
13@agoncal
@Inject on Attributes
public class BookBean implements Serializable {
@Inject
private NumberGenerator numberGen...
14@agoncal
@Inject on Constructor
public class BookBean implements Serializable {
private NumberGenerator numberGenerator;...
15@agoncal
@Inject on Setters
public class BookBean implements Serializable {
private NumberGenerator numberGenerator;
pri...
16@agoncal
Activate CDI
● In CDI 1.0 beans.xml in archive
● Since CDI 1.1 it's activated by default
●
All classes having a...
Demo
-
@Inject
18@agoncal
Demo: @Inject One Implementation
Qualifiers
20@agoncal
How Do I ?
21@agoncal
How Do I ?
22@agoncal
How Do I ?
@Default
23@agoncal
Use Qualifiers !
@ThirteenDigits
24@agoncal
Use Qualifiers !
@EightDigits
25@agoncal
A Qualifier
@Qualifier
@Retention(RUNTIME)
@Target({ METHOD, FIELD, PARAMETER, TYPE })
@Documented
public @inte...
26@agoncal
Qualifying an Injection Point
public class BookBean implements Serializable {
@Inject @ThirteenDigits
private N...
27@agoncal
Qualifying an Injection Point
public class BookBean implements Serializable {
@Inject @ThirteenDigits
private N...
28@agoncal
Qualifying a Bean
@ThirteenDigits
public class IsbnGenerator implements NumberGenerator {
@Override
public Stri...
Demo
-
Qualifiers
30@agoncal
Demo: @Inject One Implementation
Producers
32@agoncal
How Do I ?
public class BookBean implements Serializable {
@Inject
private EntityManager em;
@Inject
private Lo...
33@agoncal
How Do I ?
public class BookBean implements Serializable {
@Inject
private EntityManager em;
@Inject
private Lo...
34@agoncal
How Do I ?
public class BookBean implements Serializable {
@Inject
private EntityManager em;
@Inject
private Lo...
35@agoncal
How Do I ?
public class BookBean implements Serializable {
@Inject
private EntityManager em;
@Inject
private Lo...
36@agoncal
Use Producers !
public class BookBean implements Serializable {
@Inject
private EntityManager em;
// ...
}
publ...
37@agoncal
Use Producers !
public class BookBean implements Serializable {
@Inject
private Logger logger;
// ...
}
public ...
Demo
-
Producers
39@agoncal
Demo: Producers
Web tier
&
Service tier
41@agoncal
How Do I ?
42@agoncal
How Do I ?
43@agoncal
Use Expression Language...
44@agoncal
Use Expression Language and Scopes !
45@agoncal
Service Tier
@Transactional
public class BookBean implements Serializable {
@Inject
private EntityManager em;
p...
46@agoncal
Service Tier + Web Tier
@Named
@Transactional
public class BookBean implements Serializable {
@Inject
private E...
47@agoncal
Service Tier + Web Tier
@Named("service")
@Transactional
public class BookBean implements Serializable {
@Injec...
48@agoncal
Several scopes
● @Dependent (default)
● @ApplicationScoped, @SessionScoped,
@RequestScoped
● @ConversationScope...
49@agoncal
Just choose the right scope
@Named
@RequestScoped
@Transactional
public class BookBean implements Serializable ...
50@agoncal
Just choose the right scope
@Named
@SessionScoped
@Transactional
public class BookBean implements Serializable ...
51@agoncal
Just choose the right scope
@Named
@ConversationScoped
@Transactional
public class BookBean implements Serializ...
Demo
-
@Named & scope
53@agoncal
Demo: @Named & Scope
</>
Alternatives
56@agoncal
How Do I ?
public class MockGenerator implements NumberGenerator {
public String generateNumber() {
return "moc...
57@agoncal
How Do I ?
@Mock
public class MockGenerator implements NumberGenerator {
public String generateNumber() {
retur...
58@agoncal
How Do I ?
@Mock
public class MockGenerator implements NumberGenerator {
public String generateNumber() {
retur...
59@agoncal
Use an Alternative !
@Alternative
@EightDigits
public class MockGenerator implements NumberGenerator {
public S...
60@agoncal
Activate the Alternative
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
...
version="1.1" bean-discovery-mod...
Demo
-
Alternatives
62@agoncal
Demo: Alternatives
Events
64@agoncal
How Do I ?
65@agoncal
How Do I ?
Still too coupled
66@agoncal
Use Events !




67@agoncal
Fire and Observe
public class BookBean implements Serializable {
@Inject
private Event<Book> boughtEvent;
publi...
68@agoncal
Fire and Observe with Qualifier
public class BookBean implements Serializable {
@Inject @Paper
private Event<Bo...
Demo
-
Events
70@agoncal
Demo: Events
CDI : So Much More
72@agoncal
CDI : So Much More
73@agoncal
CDI Extension ecosystem
74@agoncal
CDI Course on PluralSight
http://www.pluralsight.com/courses/context-dependency-injection-1-1
Thanks
www.antoniogoncalves.org
antonio.goncalves@gmail.com
@agoncal
@devoxxfr
@lescastcodeurs
Q & A
77@agoncal
Creative Commons
● Attribution — You must attribute the work in
the manner specified by the author or licensor
...
CDI : How do I ?
by antonio goncalves
@agoncal
2@agoncal
Antonio Goncalves
What is CDI ?
4@agoncal
What is CDI ?
●
Dependency injection
● Lose coupling, strong typing
●
Context management
●
Interceptors and deco...
5@agoncal
History of CDI
6@agoncal
Implementations
Demo
-
Creating a Web App
8@agoncal
Demos with JBoss Forge
●
Java EE scaffolding tool
● Shell commands
●
CRUD application
●
Gets you start quickly
●...
9@agoncal
Demo: Creating a Web App
Dependency Injection
11@agoncal
How Do I ?
12@agoncal
Use @Inject !
13@agoncal
@Inject on Attributes
public class BookBean implements Serializable {
@Inject
private NumberGenerator numberGen...
14@agoncal
@Inject on Constructor
public class BookBean implements Serializable {
private NumberGenerator numberGenerator;...
15@agoncal
@Inject on Setters
public class BookBean implements Serializable {
private NumberGenerator numberGenerator;
pri...
16@agoncal
Activate CDI
● In CDI 1.0 beans.xml in archive
● Since CDI 1.1 it's activated by default
●
All classes having a...
Demo
-
@Inject
18@agoncal
Demo: @Inject One Implementation
Qualifiers
20@agoncal
How Do I ?
21@agoncal
How Do I ?
22@agoncal
How Do I ?
@Default
23@agoncal
Use Qualifiers !
@ThirteenDigits
24@agoncal
Use Qualifiers !
@EightDigits
25@agoncal
A Qualifier
@Qualifier
@Retention(RUNTIME)
@Target({ METHOD, FIELD, PARAMETER, TYPE })
@Documented
public @inte...
26@agoncal
Qualifying an Injection Point
public class BookBean implements Serializable {
@Inject @ThirteenDigits
private N...
27@agoncal
Qualifying an Injection Point
public class BookBean implements Serializable {
@Inject @ThirteenDigits
private N...
28@agoncal
Qualifying a Bean
@ThirteenDigits
public class IsbnGenerator implements NumberGenerator {
@Override
public Stri...
Demo
-
Qualifiers
30@agoncal
Demo: @Inject One Implementation
Producers
32@agoncal
How Do I ?
public class BookBean implements Serializable {
@Inject
private EntityManager em;
@Inject
private Lo...
33@agoncal
How Do I ?
public class BookBean implements Serializable {
@Inject
private EntityManager em;
@Inject
private Lo...
34@agoncal
How Do I ?
public class BookBean implements Serializable {
@Inject
private EntityManager em;
@Inject
private Lo...
35@agoncal
How Do I ?
public class BookBean implements Serializable {
@Inject
private EntityManager em;
@Inject
private Lo...
36@agoncal
Use Producers !
public class BookBean implements Serializable {
@Inject
private EntityManager em;
// ...
}
publ...
37@agoncal
Use Producers !
public class BookBean implements Serializable {
@Inject
private Logger logger;
// ...
}
public ...
Demo
-
Producers
39@agoncal
Demo: Producers
Web tier
&
Service tier
41@agoncal
How Do I ?
42@agoncal
How Do I ?
43@agoncal
Use Expression Language...
44@agoncal
Use Expression Language and Scopes !
45@agoncal
Service Tier
@Transactional
public class BookBean implements Serializable {
@Inject
private EntityManager em;
p...
46@agoncal
Service Tier + Web Tier
@Named
@Transactional
public class BookBean implements Serializable {
@Inject
private E...
47@agoncal
Service Tier + Web Tier
@Named("service")
@Transactional
public class BookBean implements Serializable {
@Injec...
48@agoncal
Several scopes
● @Dependent (default)
● @ApplicationScoped, @SessionScoped,
@RequestScoped
● @ConversationScope...
49@agoncal
Just choose the right scope
@Named
@RequestScoped
@Transactional
public class BookBean implements Serializable ...
50@agoncal
Just choose the right scope
@Named
@SessionScoped
@Transactional
public class BookBean implements Serializable ...
51@agoncal
Just choose the right scope
@Named
@ConversationScoped
@Transactional
public class BookBean implements Serializ...
Demo
-
@Named & scope
53@agoncal
Demo: @Named & Scope
</>
Alternatives
56@agoncal
How Do I ?
public class MockGenerator implements NumberGenerator {
public String generateNumber() {
return "moc...
57@agoncal
How Do I ?
@Mock
public class MockGenerator implements NumberGenerator {
public String generateNumber() {
retur...
58@agoncal
How Do I ?
@Mock
public class MockGenerator implements NumberGenerator {
public String generateNumber() {
retur...
59@agoncal
Use an Alternative !
@Alternative
@EightDigits
public class MockGenerator implements NumberGenerator {
public S...
60@agoncal
Activate the Alternative
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
...
version="1.1" bean-discovery-mod...
Demo
-
Alternatives
62@agoncal
Demo: Alternatives
Events
64@agoncal
How Do I ?
65@agoncal
How Do I ?
Still too coupled
66@agoncal
Use Events !




67@agoncal
Fire and Observe
public class BookBean implements Serializable {
@Inject
private Event<Book> boughtEvent;
publi...
68@agoncal
Fire and Observe with Qualifier
public class BookBean implements Serializable {
@Inject @Paper
private Event<Bo...
Demo
-
Events
70@agoncal
Demo: Events
CDI : So Much More
72@agoncal
CDI : So Much More
73@agoncal
CDI Extension ecosystem
74@agoncal
CDI Course on PluralSight
http://www.pluralsight.com/courses/context-dependency-injection-1-1
Thanks
www.antoniogoncalves.org
antonio.goncalves@gmail.com
@agoncal
@devoxxfr
@lescastcodeurs
Q & A
77@agoncal
Creative Commons
● Attribution — You must attribute the work in
the manner specified by the author or licensor
...
Prochain SlideShare
Chargement dans…5
×

CDI: How do I ?

8 057 vues

Publié le

In this talk I show how CDI can be used to accomplish common tasks very easily.

Publié dans : Logiciels
  • Soyez le premier à commenter

CDI: How do I ?

  1. 1. CDI : How do I ? by antonio goncalves @agoncal
  2. 2. 2@agoncal Antonio Goncalves
  3. 3. What is CDI ?
  4. 4. 4@agoncal What is CDI ? ● Dependency injection ● Lose coupling, strong typing ● Context management ● Interceptors and decorators ● Event bus ● Extensions
  5. 5. 5@agoncal History of CDI
  6. 6. 6@agoncal Implementations
  7. 7. Demo - Creating a Web App
  8. 8. 8@agoncal Demos with JBoss Forge ● Java EE scaffolding tool ● Shell commands ● CRUD application ● Gets you start quickly ● Takes care of integration ● Plugin based
  9. 9. 9@agoncal Demo: Creating a Web App
  10. 10. Dependency Injection
  11. 11. 11@agoncal How Do I ?
  12. 12. 12@agoncal Use @Inject !
  13. 13. 13@agoncal @Inject on Attributes public class BookBean implements Serializable { @Inject private NumberGenerator numberGenerator; @Inject private ItemService itemService; // ... }
  14. 14. 14@agoncal @Inject on Constructor public class BookBean implements Serializable { private NumberGenerator numberGenerator; private ItemService itemService; @Inject public BookBean(NumberGenerator numberGenerator, ItemService srv){ this.numberGenerator = numberGenerator; this.itemService = srv; } // ... }
  15. 15. 15@agoncal @Inject on Setters public class BookBean implements Serializable { private NumberGenerator numberGenerator; private ItemService itemService; @Inject public void setNumberGenerator(NumberGenerator numGen){ this.numberGenerator = numGen; } @Inject public void setItemService(ItemService itemService) { this.itemService = itemService; } // ... }
  16. 16. 16@agoncal Activate CDI ● In CDI 1.0 beans.xml in archive ● Since CDI 1.1 it's activated by default ● All classes having a bean definition annotation ● beans.xml to deactivate or activate all ● Archive vs Bean archive
  17. 17. Demo - @Inject
  18. 18. 18@agoncal Demo: @Inject One Implementation
  19. 19. Qualifiers
  20. 20. 20@agoncal How Do I ?
  21. 21. 21@agoncal How Do I ?
  22. 22. 22@agoncal How Do I ? @Default
  23. 23. 23@agoncal Use Qualifiers ! @ThirteenDigits
  24. 24. 24@agoncal Use Qualifiers ! @EightDigits
  25. 25. 25@agoncal A Qualifier @Qualifier @Retention(RUNTIME) @Target({ METHOD, FIELD, PARAMETER, TYPE }) @Documented public @interface ThirteenDigits { }
  26. 26. 26@agoncal Qualifying an Injection Point public class BookBean implements Serializable { @Inject @ThirteenDigits private NumberGenerator numberGenerator; @Inject private ItemService itemService; // ... }
  27. 27. 27@agoncal Qualifying an Injection Point public class BookBean implements Serializable { @Inject @ThirteenDigits private NumberGenerator numberGenerator; @Inject @Default private ItemService itemService; // ... }
  28. 28. 28@agoncal Qualifying a Bean @ThirteenDigits public class IsbnGenerator implements NumberGenerator { @Override public String generateNumber() { return "13-" + Math.abs(new Random().nextInt()); } }
  29. 29. Demo - Qualifiers
  30. 30. 30@agoncal Demo: @Inject One Implementation
  31. 31. Producers
  32. 32. 32@agoncal How Do I ? public class BookBean implements Serializable { @Inject private EntityManager em; @Inject private Logger logger; // ... }
  33. 33. 33@agoncal How Do I ? public class BookBean implements Serializable { @Inject private EntityManager em; @Inject private Logger logger; // ... }
  34. 34. 34@agoncal How Do I ? public class BookBean implements Serializable { @Inject private EntityManager em; @Inject private Logger logger; // ... } Several persistence units @PersistenceContext(unitName = "myPU1") @PersistenceContext(unitName = "myPU2")
  35. 35. 35@agoncal How Do I ? public class BookBean implements Serializable { @Inject private EntityManager em; @Inject private Logger logger; // ... } Third party framewok
  36. 36. 36@agoncal Use Producers ! public class BookBean implements Serializable { @Inject private EntityManager em; // ... } public class ResourceProducer { @Produces @PersistenceContext(unitName = "myPU") private EntityManager entityManager; }
  37. 37. 37@agoncal Use Producers ! public class BookBean implements Serializable { @Inject private Logger logger; // ... } public class ResourceProducer { @Produces private Logger produceLogger(InjectionPoint ip) { return Logger.getLogger(ip.getMember().getDeclaringClass().getName()); } }
  38. 38. Demo - Producers
  39. 39. 39@agoncal Demo: Producers
  40. 40. Web tier & Service tier
  41. 41. 41@agoncal How Do I ?
  42. 42. 42@agoncal How Do I ?
  43. 43. 43@agoncal Use Expression Language...
  44. 44. 44@agoncal Use Expression Language and Scopes !
  45. 45. 45@agoncal Service Tier @Transactional public class BookBean implements Serializable { @Inject private EntityManager em; public void update() { em.persist(book); } }
  46. 46. 46@agoncal Service Tier + Web Tier @Named @Transactional public class BookBean implements Serializable { @Inject private EntityManager em; public void update() { em.persist(book); } } <h:commandLink value="Create" action='#{bookBean.update}'/>
  47. 47. 47@agoncal Service Tier + Web Tier @Named("service") @Transactional public class BookBean implements Serializable { @Inject private EntityManager em; public void update() { em.persist(book); } } <h:commandLink value="Create" action='#{service.update}'/>
  48. 48. 48@agoncal Several scopes ● @Dependent (default) ● @ApplicationScoped, @SessionScoped, @RequestScoped ● @ConversationScoped ● Create your own ● @TransactionalScoped ● @ViewScoped ● @ThreadScoped ● @ClusterScoped
  49. 49. 49@agoncal Just choose the right scope @Named @RequestScoped @Transactional public class BookBean implements Serializable { public void update() { } public void delete() { } }
  50. 50. 50@agoncal Just choose the right scope @Named @SessionScoped @Transactional public class BookBean implements Serializable { public void update() { } public void delete() { } }
  51. 51. 51@agoncal Just choose the right scope @Named @ConversationScoped @Transactional public class BookBean implements Serializable { @Inject private Conversation conversation; public void update() { conversation.begin(); } public void delete() { conversation.end(); } }
  52. 52. Demo - @Named & scope
  53. 53. 53@agoncal Demo: @Named & Scope
  54. 54. </>
  55. 55. Alternatives
  56. 56. 56@agoncal How Do I ? public class MockGenerator implements NumberGenerator { public String generateNumber() { return "mock-" + Math.abs(new Random().nextInt()); } }
  57. 57. 57@agoncal How Do I ? @Mock public class MockGenerator implements NumberGenerator { public String generateNumber() { return "mock-" + Math.abs(new Random().nextInt()); } } public class BookBean implements Serializable { @Inject @Mock private NumberGenerator numberGenerator; // ... }
  58. 58. 58@agoncal How Do I ? @Mock public class MockGenerator implements NumberGenerator { public String generateNumber() { return "mock-" + Math.abs(new Random().nextInt()); } } public class BookBean implements Serializable { @Inject @Mock private NumberGenerator numberGenerator; // ... }
  59. 59. 59@agoncal Use an Alternative ! @Alternative @EightDigits public class MockGenerator implements NumberGenerator { public String generateNumber() { return "mock-" + Math.abs(new Random().nextInt()); } } public class BookBean implements Serializable { @Inject @EightDigits private NumberGenerator numberGenerator; // ... }
  60. 60. 60@agoncal Activate the Alternative <beans xmlns="http://xmlns.jcp.org/xml/ns/javaee" ... version="1.1" bean-discovery-mode="all"> <alternatives> <class>com.foo.MockGenerator</class> </alternatives> </beans>
  61. 61. Demo - Alternatives
  62. 62. 62@agoncal Demo: Alternatives
  63. 63. Events
  64. 64. 64@agoncal How Do I ?
  65. 65. 65@agoncal How Do I ? Still too coupled
  66. 66. 66@agoncal Use Events !    
  67. 67. 67@agoncal Fire and Observe public class BookBean implements Serializable { @Inject private Event<Book> boughtEvent; public void update() { boughtEvent.fire(book); } } public class InventoryService { private void observeBooks (@Observes Book book) { logger.info("Book recevied " + book.getTitle()); } }
  68. 68. 68@agoncal Fire and Observe with Qualifier public class BookBean implements Serializable { @Inject @Paper private Event<Book> boughtEvent; public void update() { boughtEvent.fire(book); } } public class InventoryService { private void observeBooks (@Observes @Paper Book book) { logger.info("Book recevied " + book.getTitle()); } }
  69. 69. Demo - Events
  70. 70. 70@agoncal Demo: Events
  71. 71. CDI : So Much More
  72. 72. 72@agoncal CDI : So Much More
  73. 73. 73@agoncal CDI Extension ecosystem
  74. 74. 74@agoncal CDI Course on PluralSight http://www.pluralsight.com/courses/context-dependency-injection-1-1
  75. 75. Thanks www.antoniogoncalves.org antonio.goncalves@gmail.com @agoncal @devoxxfr @lescastcodeurs
  76. 76. Q & A
  77. 77. 77@agoncal 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.
  78. 78. CDI : How do I ? by antonio goncalves @agoncal
  79. 79. 2@agoncal Antonio Goncalves
  80. 80. What is CDI ?
  81. 81. 4@agoncal What is CDI ? ● Dependency injection ● Lose coupling, strong typing ● Context management ● Interceptors and decorators ● Event bus ● Extensions
  82. 82. 5@agoncal History of CDI
  83. 83. 6@agoncal Implementations
  84. 84. Demo - Creating a Web App
  85. 85. 8@agoncal Demos with JBoss Forge ● Java EE scaffolding tool ● Shell commands ● CRUD application ● Gets you start quickly ● Takes care of integration ● Plugin based
  86. 86. 9@agoncal Demo: Creating a Web App
  87. 87. Dependency Injection
  88. 88. 11@agoncal How Do I ?
  89. 89. 12@agoncal Use @Inject !
  90. 90. 13@agoncal @Inject on Attributes public class BookBean implements Serializable { @Inject private NumberGenerator numberGenerator; @Inject private ItemService itemService; // ... }
  91. 91. 14@agoncal @Inject on Constructor public class BookBean implements Serializable { private NumberGenerator numberGenerator; private ItemService itemService; @Inject public BookBean(NumberGenerator numberGenerator, ItemService srv){ this.numberGenerator = numberGenerator; this.itemService = srv; } // ... }
  92. 92. 15@agoncal @Inject on Setters public class BookBean implements Serializable { private NumberGenerator numberGenerator; private ItemService itemService; @Inject public void setNumberGenerator(NumberGenerator numGen){ this.numberGenerator = numGen; } @Inject public void setItemService(ItemService itemService) { this.itemService = itemService; } // ... }
  93. 93. 16@agoncal Activate CDI ● In CDI 1.0 beans.xml in archive ● Since CDI 1.1 it's activated by default ● All classes having a bean definition annotation ● beans.xml to deactivate or activate all ● Archive vs Bean archive
  94. 94. Demo - @Inject
  95. 95. 18@agoncal Demo: @Inject One Implementation
  96. 96. Qualifiers
  97. 97. 20@agoncal How Do I ?
  98. 98. 21@agoncal How Do I ?
  99. 99. 22@agoncal How Do I ? @Default
  100. 100. 23@agoncal Use Qualifiers ! @ThirteenDigits
  101. 101. 24@agoncal Use Qualifiers ! @EightDigits
  102. 102. 25@agoncal A Qualifier @Qualifier @Retention(RUNTIME) @Target({ METHOD, FIELD, PARAMETER, TYPE }) @Documented public @interface ThirteenDigits { }
  103. 103. 26@agoncal Qualifying an Injection Point public class BookBean implements Serializable { @Inject @ThirteenDigits private NumberGenerator numberGenerator; @Inject private ItemService itemService; // ... }
  104. 104. 27@agoncal Qualifying an Injection Point public class BookBean implements Serializable { @Inject @ThirteenDigits private NumberGenerator numberGenerator; @Inject @Default private ItemService itemService; // ... }
  105. 105. 28@agoncal Qualifying a Bean @ThirteenDigits public class IsbnGenerator implements NumberGenerator { @Override public String generateNumber() { return "13-" + Math.abs(new Random().nextInt()); } }
  106. 106. Demo - Qualifiers
  107. 107. 30@agoncal Demo: @Inject One Implementation
  108. 108. Producers
  109. 109. 32@agoncal How Do I ? public class BookBean implements Serializable { @Inject private EntityManager em; @Inject private Logger logger; // ... }
  110. 110. 33@agoncal How Do I ? public class BookBean implements Serializable { @Inject private EntityManager em; @Inject private Logger logger; // ... }
  111. 111. 34@agoncal How Do I ? public class BookBean implements Serializable { @Inject private EntityManager em; @Inject private Logger logger; // ... } Several persistence units @PersistenceContext(unitName = "myPU1") @PersistenceContext(unitName = "myPU2")
  112. 112. 35@agoncal How Do I ? public class BookBean implements Serializable { @Inject private EntityManager em; @Inject private Logger logger; // ... } Third party framewok
  113. 113. 36@agoncal Use Producers ! public class BookBean implements Serializable { @Inject private EntityManager em; // ... } public class ResourceProducer { @Produces @PersistenceContext(unitName = "myPU") private EntityManager entityManager; }
  114. 114. 37@agoncal Use Producers ! public class BookBean implements Serializable { @Inject private Logger logger; // ... } public class ResourceProducer { @Produces private Logger produceLogger(InjectionPoint ip) { return Logger.getLogger(ip.getMember().getDeclaringClass().getName()); } }
  115. 115. Demo - Producers
  116. 116. 39@agoncal Demo: Producers
  117. 117. Web tier & Service tier
  118. 118. 41@agoncal How Do I ?
  119. 119. 42@agoncal How Do I ?
  120. 120. 43@agoncal Use Expression Language...
  121. 121. 44@agoncal Use Expression Language and Scopes !
  122. 122. 45@agoncal Service Tier @Transactional public class BookBean implements Serializable { @Inject private EntityManager em; public void update() { em.persist(book); } }
  123. 123. 46@agoncal Service Tier + Web Tier @Named @Transactional public class BookBean implements Serializable { @Inject private EntityManager em; public void update() { em.persist(book); } } <h:commandLink value="Create" action='#{bookBean.update}'/>
  124. 124. 47@agoncal Service Tier + Web Tier @Named("service") @Transactional public class BookBean implements Serializable { @Inject private EntityManager em; public void update() { em.persist(book); } } <h:commandLink value="Create" action='#{service.update}'/>
  125. 125. 48@agoncal Several scopes ● @Dependent (default) ● @ApplicationScoped, @SessionScoped, @RequestScoped ● @ConversationScoped ● Create your own ● @TransactionalScoped ● @ViewScoped ● @ThreadScoped ● @ClusterScoped
  126. 126. 49@agoncal Just choose the right scope @Named @RequestScoped @Transactional public class BookBean implements Serializable { public void update() { } public void delete() { } }
  127. 127. 50@agoncal Just choose the right scope @Named @SessionScoped @Transactional public class BookBean implements Serializable { public void update() { } public void delete() { } }
  128. 128. 51@agoncal Just choose the right scope @Named @ConversationScoped @Transactional public class BookBean implements Serializable { @Inject private Conversation conversation; public void update() { conversation.begin(); } public void delete() { conversation.end(); } }
  129. 129. Demo - @Named & scope
  130. 130. 53@agoncal Demo: @Named & Scope
  131. 131. </>
  132. 132. Alternatives
  133. 133. 56@agoncal How Do I ? public class MockGenerator implements NumberGenerator { public String generateNumber() { return "mock-" + Math.abs(new Random().nextInt()); } }
  134. 134. 57@agoncal How Do I ? @Mock public class MockGenerator implements NumberGenerator { public String generateNumber() { return "mock-" + Math.abs(new Random().nextInt()); } } public class BookBean implements Serializable { @Inject @Mock private NumberGenerator numberGenerator; // ... }
  135. 135. 58@agoncal How Do I ? @Mock public class MockGenerator implements NumberGenerator { public String generateNumber() { return "mock-" + Math.abs(new Random().nextInt()); } } public class BookBean implements Serializable { @Inject @Mock private NumberGenerator numberGenerator; // ... }
  136. 136. 59@agoncal Use an Alternative ! @Alternative @EightDigits public class MockGenerator implements NumberGenerator { public String generateNumber() { return "mock-" + Math.abs(new Random().nextInt()); } } public class BookBean implements Serializable { @Inject @EightDigits private NumberGenerator numberGenerator; // ... }
  137. 137. 60@agoncal Activate the Alternative <beans xmlns="http://xmlns.jcp.org/xml/ns/javaee" ... version="1.1" bean-discovery-mode="all"> <alternatives> <class>com.foo.MockGenerator</class> </alternatives> </beans>
  138. 138. Demo - Alternatives
  139. 139. 62@agoncal Demo: Alternatives
  140. 140. Events
  141. 141. 64@agoncal How Do I ?
  142. 142. 65@agoncal How Do I ? Still too coupled
  143. 143. 66@agoncal Use Events !    
  144. 144. 67@agoncal Fire and Observe public class BookBean implements Serializable { @Inject private Event<Book> boughtEvent; public void update() { boughtEvent.fire(book); } } public class InventoryService { private void observeBooks (@Observes Book book) { logger.info("Book recevied " + book.getTitle()); } }
  145. 145. 68@agoncal Fire and Observe with Qualifier public class BookBean implements Serializable { @Inject @Paper private Event<Book> boughtEvent; public void update() { boughtEvent.fire(book); } } public class InventoryService { private void observeBooks (@Observes @Paper Book book) { logger.info("Book recevied " + book.getTitle()); } }
  146. 146. Demo - Events
  147. 147. 70@agoncal Demo: Events
  148. 148. CDI : So Much More
  149. 149. 72@agoncal CDI : So Much More
  150. 150. 73@agoncal CDI Extension ecosystem
  151. 151. 74@agoncal CDI Course on PluralSight http://www.pluralsight.com/courses/context-dependency-injection-1-1
  152. 152. Thanks www.antoniogoncalves.org antonio.goncalves@gmail.com @agoncal @devoxxfr @lescastcodeurs
  153. 153. Q & A
  154. 154. 77@agoncal 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.

×