0
In den nächsten 60 Minuten<br />Refactoring<br />the Java EE CDI way<br />
eCustomerBeispiel<br /><ul><li>Java EE Anwendung zur Online-Verwaltung von Kunden
View: Login Screen, Übersicht der Kunden, Kunde anlegen, Kunde bearbeiten, Kunde löschen
Model: User (Admin), Kunde</li></ul>Demo time...<br />
eCustomer Beispiel <br /><ul><li>Architekturansatz
Mehrschicht-architektur
Bausteine
JSF xhtml Seiten
Formularobjekte
Services
PersistenteDomain Objekte (User,...)</li></li></ul><li>Use Case Kunde bearbeiten<br /><ul><li>*.xhtml
AuthenticationBean
CustomerBean
CustomerService(EJB)
Customer</li></li></ul><li>Use Case Kunde bearbeiten<br /><ul><li>JSF AuthenticationBean</li></ul>@ManagedBean<br />@Sessi...
Use Case Kunde bearbeiten<br /><ul><li>View customer/edit.xhtml</li></ul><html ...><h:body><br /><h:form><br />Vorname: <h...
Use Case Kunde bearbeiten<br /><ul><li>JSF CustomerBean Attribute (1/3)</li></ul>@ManagedBean @SessionScoped<br />publiccl...
Use Case Kunde bearbeiten<br /><ul><li>JSF CustomerBean Audit Info (2/3)</li></ul>@ManagedBean @SessionScoped<br />publicc...
Use Case Kunde bearbeiten<br /><ul><li>JSF CustomerBean Update (3/3)</li></ul>@ManagedBean @SessionScoped<br />publicclass...
Use Case Kunde bearbeiten<br /><ul><li>CustomerService EJB</li></ul>@Stateless<br />publicclassCustomerServiceEJB<br />imp...
CDI Optimierungspotential!?<br />
CDI Refactoring<br /><ul><li>1 - CDI Wiring
2 - CDI Current User
3 - CDI Beansonly
4 - CDI Style Formulare
5 - CDI Events
6 - CDI Ausblick</li></li></ul><li>CDI Refactoring<br /><ul><li>1 - CDI Wiring
2 - CDI Current User
3 - CDI Beansonly
4 - CDI Style Formulare
5 - CDI Events
6 - CDI Ausblick</li></li></ul><li>eCustomer – CDI Wiring<br /><ul><li>JSR 330 Annotationen
@Named (für EL), @Qualifier
@Inject
JSR 299 Annotationen
@RequestScoped, @SessionScoped,@ApplicationScoped, ...</li></li></ul><li>eCustomer – CDI Wiring<br /><ul><li>JSF Authentic...
eCustomer – CDI Wiring<br /><ul><li>JSF CustomerBean</li></ul>@Named<br />@SessionScoped<br />publicclassCustomerBeanimple...
CDI Refactoring<br /><ul><li>1 - CDI Wiring
2 - CDI Current User
3 - CDI Beansonly
4 - CDI Style Formulare
5 - CDI Events
6 - CDI Ausblick</li></li></ul><li>eCustomer – CDI Current User<br /><ul><li>Producer Methods/Fields
Factory Method Pattern für fachliche Objekte
@Produces
Qualifizierung über eigene fachliche Qualifier</li></li></ul><li>eCustomer – CDI Current User<br /><ul><li>CurrentQualifie...
eCustomer – CDI Current User<br /><ul><li>JSF AuthenticationBean</li></ul>@Named @SessionScoped<br />publicclassAuthentica...
eCustomer – CDI Current User<br /><ul><li>JSF CustomerBean</li></ul>@Named @SessionScoped<br />publicclassCustomerBeanimpl...
eCustomer – CDI Current User<br /><ul><li>CustomerService EJB</li></ul>@Stateless<br />publicclassCustomerServiceEJB<br />...
CDI Refactoring<br /><ul><li>1 - CDI Wiring
2 - CDI Current User
3 - CDI Beansonly
4 - CDI Style Formulare
5 - CDI Events
Upcoming SlideShare
Loading in...5
×

Java EE Development without EJB - and without Spring - Part 2

1,406

Published on

Die grundlegenden Bausteine von CDI lassen nur ungefähr erahnen, welche enormen Chancen und Möglichkeiten diese Technologie für die Umsetzung von Java-EE-Anwendungen bietet. Der zweite Teil der Doppelsession widmet sich daher dem CDI-Programmiermodell und erläutert anhand eines praktischen Beispiels die Umsetzung von CDI-Anwendungen in Abgrenzung zu herkömmlichen Ansätzen. Dabei kommen Best Practices und Pattern sowie die sogenannten „portablen Erweiterungsmöglichkeiten“ nicht zu kurz.

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,406
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
51
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Transcript of "Java EE Development without EJB - and without Spring - Part 2"

  1. 1.
  2. 2.
  3. 3.
  4. 4. In den nächsten 60 Minuten<br />Refactoring<br />the Java EE CDI way<br />
  5. 5. eCustomerBeispiel<br /><ul><li>Java EE Anwendung zur Online-Verwaltung von Kunden
  6. 6. View: Login Screen, Übersicht der Kunden, Kunde anlegen, Kunde bearbeiten, Kunde löschen
  7. 7. Model: User (Admin), Kunde</li></ul>Demo time...<br />
  8. 8. eCustomer Beispiel <br /><ul><li>Architekturansatz
  9. 9. Mehrschicht-architektur
  10. 10. Bausteine
  11. 11. JSF xhtml Seiten
  12. 12. Formularobjekte
  13. 13. Services
  14. 14. PersistenteDomain Objekte (User,...)</li></li></ul><li>Use Case Kunde bearbeiten<br /><ul><li>*.xhtml
  15. 15. AuthenticationBean
  16. 16. CustomerBean
  17. 17. CustomerService(EJB)
  18. 18. Customer</li></li></ul><li>Use Case Kunde bearbeiten<br /><ul><li>JSF AuthenticationBean</li></ul>@ManagedBean<br />@SessionScoped<br />publicclassAuthenticationBean<br />implementsSerializable {<br />private User authenticatedUser;<br />public String authenticate() {...}<br />public User getAuthenticatedUser() {...}<br />}<br />
  19. 19. Use Case Kunde bearbeiten<br /><ul><li>View customer/edit.xhtml</li></ul><html ...><h:body><br /><h:form><br />Vorname: <h:inputText<br />value=“#{customerBean.firstname}"/><br /> Name: <h:inputText<br />value=“#{customerBean.name}"/> <br /> </h:form><br /></h:body></html><br />
  20. 20. Use Case Kunde bearbeiten<br /><ul><li>JSF CustomerBean Attribute (1/3)</li></ul>@ManagedBean @SessionScoped<br />publicclassCustomerBeanimplementsSerializable {<br /> private Customer currentCustomer;<br />publicString getFirstname() {<br />returncurrentCustomer.getFirstname();<br /> }<br />publicvoidsetFirstname(String firstname) {<br />currentCustomer.setFirstname(firstname);<br /> }<br />publicString getName() {...}<br />publicvoidsetName(String name) {...}<br />}<br />
  21. 21. Use Case Kunde bearbeiten<br /><ul><li>JSF CustomerBean Audit Info (2/3)</li></ul>@ManagedBean @SessionScoped<br />publicclassCustomerBeanimplementsSerializable {<br /> @ManagedProperty("#{auth..Bean.authenticatedUser}")<br /> private User currentUser;<br />publicvoidsetCurrentUser(User currentUser) {<br />this.currentUser= currentUser;<br /> }<br />}<br />Audit<br />Info<br />
  22. 22. Use Case Kunde bearbeiten<br /><ul><li>JSF CustomerBean Update (3/3)</li></ul>@ManagedBean @SessionScoped<br />publicclassCustomerBeanimplementsSerializable{<br />@EJB<br /> private CustomerServicecustomerService;<br />public String update() {<br />currentCustomer.<br />updateAuditInformation(currentUser);<br />customerService.updateCustomer(currentCustomer);<br />returnOutcome.SUCCESS;<br />}<br />}<br />Audit<br />Info<br />
  23. 23. Use Case Kunde bearbeiten<br /><ul><li>CustomerService EJB</li></ul>@Stateless<br />publicclassCustomerServiceEJB<br />implementsCustomerService{<br />@PersistenceContext<br /> private EntityManagerem;<br />publicCustomer updateCustomer(Customer customer) {<br />returnem.merge(customer);<br /> }<br />}<br />
  24. 24. CDI Optimierungspotential!?<br />
  25. 25. CDI Refactoring<br /><ul><li>1 - CDI Wiring
  26. 26. 2 - CDI Current User
  27. 27. 3 - CDI Beansonly
  28. 28. 4 - CDI Style Formulare
  29. 29. 5 - CDI Events
  30. 30. 6 - CDI Ausblick</li></li></ul><li>CDI Refactoring<br /><ul><li>1 - CDI Wiring
  31. 31. 2 - CDI Current User
  32. 32. 3 - CDI Beansonly
  33. 33. 4 - CDI Style Formulare
  34. 34. 5 - CDI Events
  35. 35. 6 - CDI Ausblick</li></li></ul><li>eCustomer – CDI Wiring<br /><ul><li>JSR 330 Annotationen
  36. 36. @Named (für EL), @Qualifier
  37. 37. @Inject
  38. 38. JSR 299 Annotationen
  39. 39. @RequestScoped, @SessionScoped,@ApplicationScoped, ...</li></li></ul><li>eCustomer – CDI Wiring<br /><ul><li>JSF AuthenticationBean</li></ul>@Named<br />@SessionScoped<br />publicclassAuthenticationBean<br />implementsSerializable {<br /> ...<br />}<br />
  40. 40. eCustomer – CDI Wiring<br /><ul><li>JSF CustomerBean</li></ul>@Named<br />@SessionScoped<br />publicclassCustomerBeanimplementsSerializable {<br />@Inject// no EL support in @Named<br /> private AuthenticationBeanauthBean;<br /> @Inject<br /> private CustomerServicecustomerService;<br />}<br />
  41. 41. CDI Refactoring<br /><ul><li>1 - CDI Wiring
  42. 42. 2 - CDI Current User
  43. 43. 3 - CDI Beansonly
  44. 44. 4 - CDI Style Formulare
  45. 45. 5 - CDI Events
  46. 46. 6 - CDI Ausblick</li></li></ul><li>eCustomer – CDI Current User<br /><ul><li>Producer Methods/Fields
  47. 47. Factory Method Pattern für fachliche Objekte
  48. 48. @Produces
  49. 49. Qualifizierung über eigene fachliche Qualifier</li></li></ul><li>eCustomer – CDI Current User<br /><ul><li>CurrentQualifier</li></ul>@Qualifier<br />@Target({TYPE, METHOD, PARAMETER, FIELD})<br />@Retention(RUNTIME)<br />public @interfaceCurrent{}<br />
  50. 50. eCustomer – CDI Current User<br /><ul><li>JSF AuthenticationBean</li></ul>@Named @SessionScoped<br />publicclassAuthenticationBean<br />implementsSerializable {<br /> private User authenticatedUser;<br />public String authenticate() {...}<br /> @Produces<br /> @Current<br />public User getAuthenticatedUser() {...}<br />}<br />
  51. 51. eCustomer – CDI Current User<br /><ul><li>JSF CustomerBean</li></ul>@Named @SessionScoped<br />publicclassCustomerBeanimplementsSerializable {<br />//@Inject// no EL support in @Named<br />//private AuthenticationBeanauthBean;<br />public String update() {<br />// current.updateAuditInformation(<br />// authController.getAuthenticatedUser());<br /> ...<br /> }<br />}<br />
  52. 52. eCustomer – CDI Current User<br /><ul><li>CustomerService EJB</li></ul>@Stateless<br />publicclassCustomerServiceEJB<br />implementsCustomerService{<br /> @Inject<br /> @Current<br /> private User currentUser;<br />publicCustomer updateCustomer(Customer customer) {<br />customer.updateAuditInformation(currentUser);<br />returnem.merge(customer);<br /> }<br />}<br />Alternativ:JPA Event Listener<br />
  53. 53. CDI Refactoring<br /><ul><li>1 - CDI Wiring
  54. 54. 2 - CDI Current User
  55. 55. 3 - CDI Beansonly
  56. 56. 4 - CDI Style Formulare
  57. 57. 5 - CDI Events
  58. 58. 6 - CDI Ausblick</li></li></ul><li>eCustomer – CDI Beansonly<br /><ul><li>Injizierbare CDI Ressourcen
  59. 59. Normale Java Klassen
  60. 60. Optional mit @Named oder anderen Qualifier markiert
  61. 61. Enterprise Java Beans
  62. 62. Sonstige Java EE Ressourcen
  63. 63. PersistenceContext
  64. 64. UserTransaction
  65. 65. ...</li></li></ul><li>eCustomer – CDI Beansonly<br /><ul><li>CDI CustomerService</li></ul>// nothingrequired<br />publicclassCustomerServiceBean<br />implementsCustomerService{<br /> @PersistenceContext<br /> private EntityManagerem;<br />publicCustomer updateCustomer(Customer customer) {<br />returnem.merge(customer);<br /> }<br />}<br />
  66. 66. eCustomer – CDI Beansonly<br /><ul><li>CDI CustomerService</li></ul>// nothingrequired<br />publicclassCustomerServiceBean<br />implementsCustomerService{<br /> @PersistenceContext<br /> private EntityManagerem;<br />publicCustomer updateCustomer(Customer customer) {<br />returnem.merge(customer);<br /> }<br />}<br />Transaktionen?!<br />
  67. 67. eCustomer – CDI Beansonly<br /><ul><li>Transactional Annotation</li></ul>@InterceptorBinding<br />@Retention(RetentionPolicy.RUNTIME)<br />@Target({ElementType.METHOD, ElementType.TYPE})<br />public@interfaceTransactional{<br />publicTransactionalTypevalue() <br />defaultTransactionalType.REQUIRED;<br />}<br />
  68. 68. eCustomer – CDI Beansonly<br /><ul><li>Transaction Interceptor*</li></ul>@Transactional @Interceptor<br />publicclassTransactionInterceptor{<br />@Inject<br /> private UserTransactionutx;<br />@AroundInvoke<br />publicObjectapplyTransaction(<br />InvocationContextic) throwsThrowable{<br /> // implementutx.begin(), utx.commit(), ...<br /> } <br />}<br />*XML registrationomitted<br />
  69. 69. eCustomer – CDI Beansonly<br /><ul><li>Transactional CDI CustomerService</li></ul>publicclassCustomerServiceBean<br />implementsCustomerService{<br /> @PersistenceContext<br /> private EntityManagerem;<br />@Transactional<br />publicCustomer updateCustomer(Customer customer) {<br /> ...<br /> }<br />}<br />
  70. 70. eCustomer – CDI Beansonly<br /><ul><li>Transactional JSF CustomerBean</li></ul>@Named @SessionScoped<br />publicclassCustomerBeanimplementsSerializable {<br /> @Inject<br /> private CustomerServicecustomerService;<br />@Transactional<br />publicString update() {<br /> ...<br /> }<br />}<br />
  71. 71. CDI Refactoring<br /><ul><li>1 - CDI Wiring
  72. 72. 2 - CDI Current User
  73. 73. 3 - CDI Beansonly
  74. 74. 4 - CDI Style Formulare
  75. 75. 5 - CDI Events
  76. 76. 6 - CDI Ausblick</li></li></ul><li>CDI Style Formulare<br />Und was hat CDI für die <br />Formularbehandlung im Angebot?<br />
  77. 77. CDI Style Formulare<br /><ul><li>CDI Conversations
  78. 78. @ConversationScoped
  79. 79. Conversation.begin()
  80. 80. Conversation.end()
  81. 81. JSF BackingBeans und Producer Methods
  82. 82. @Produces @Named
  83. 83. Qualifier</li></li></ul><li>CDI Style Formulare<br /><ul><li>CustomerBeanConversation(1/2)</li></ul>@Named @ConversationScoped<br />publicclassCustomerBeanimplementsSerializable{<br /> private Customer currentCustomer;<br /> @Inject<br /> private Conversationconversation; <br />public String selectCustomer(Customer customer) {<br />currentCustomer= customer;<br />conversation.begin();<br />...<br />}<br />}<br />
  84. 84. CDI Style Formulare<br /><ul><li>CustomerBeanConversation (2/2)</li></ul>@Named @ConversationScoped<br />publicclassCustomerBeanimplementsSerializable {<br />public String update() {<br />conversation.end(); <br /> ...<br /> }<br />}<br />
  85. 85. CDI Style Formulare<br /><ul><li>Achtung: Infrastruktur Teil des View</li></ul><html ...><h:body><br /><h:form><br /> Vorname: <h:inputText<br />value=“#{customerBean.firstname}"/><br /> Name: <h:inputText<br />value=“#{customerBean.name}"/> <br /> </h:form><br /></h:body></html><br />Infrastruktur!<br />
  86. 86. CDI Style Formulare<br /><ul><li>JSF und Producer Methods</li></ul>@Named@ConversationScoped<br />publicclassCustomerBeanimplementsSerializable{<br /> private Customer currentCustomer;<br />public String selectCustomer(Customer customer) {<br /> ...<br />}<br /> @Produces<br /> @Named(“selectedCustomer“)<br /> @Selected<br />public Customer getSelected() { ... } <br />}<br />
  87. 87. CDI Style Formulare<br /><ul><li>JSF und Producer Methods</li></ul><html ...><h:body><br /><h:form><br /> Vorname: <h:inputText<br />value=“#{selectedCustomer.firstname}"/><br /> Name: <h:inputText<br />value=“#{selectedCustomer.name}"/> <br /> </h:form><br /></h:body></html><br />
  88. 88. CDI Refactoring<br /><ul><li>1 - CDI Wiring
  89. 89. 2 - CDI Current User
  90. 90. 3 - CDI Beansonly
  91. 91. 4 - CDI Style Formulare
  92. 92. 5 - CDI Events
  93. 93. 6 - CDI Ausblick</li></li></ul><li>CDI Events<br /><ul><li>CDI Eventsystem
  94. 94. Java EE Observer Pattern
  95. 95. Event Object
  96. 96. Event Producer
  97. 97. Observer Method
  98. 98. Eigenschaften
  99. 99. Schichtenübergreifend
  100. 100. (Ggfs.) Transaktionsgebunden
  101. 101. Synchrone Interaktion</li></li></ul><li>CDI Events<br /><ul><li>CDI Customer Created Event (1/3)</li></ul>publicclassCustomerCreatedEvent{<br /> private Customer customer;<br />publicCustomerCreatedEvent(Customer customer) {...}<br />publicCustomer getCustomer() { ... }<br />}<br />
  102. 102. CDI Events<br /><ul><li>CDI Customer Created Event (2/3)</li></ul>publicvoidsendWelcomeMail(<br />@Observes<br />CustomerCreatedEventevent) { <br />Customer customer = event.getCustomer();<br />}<br />
  103. 103. CDI Events<br /><ul><li>CDI Customer Created Event (3/3)</li></ul> @Inject<br /> private Event<CustomerCreatedEvent> eventSource;<br />publicString createCustomer() {<br />Customer customer= ...;<br />eventSource.fire(<br />newCustomerCreatedEvent(customer)<br /> );<br />...<br />}<br />
  104. 104. CDI Events<br />Ist das Event Objekt wirklich notwendig?<br />
  105. 105. CDI Events<br /><ul><li>Qualified CDI Events (1/3)</li></ul>@Qualifier<br />@Target({TYPE, METHOD, PARAMETER, FIELD})<br />@Retention(RUNTIME)<br />public @interfaceCreated{}<br />
  106. 106. CDI Events<br /><ul><li>Qualified CDI Events (2/3)</li></ul>publicvoidsendWelcomeMail(<br /> @Observes<br />@Created<br /> Customer customer) { <br /> ...<br />}<br />
  107. 107. CDI Events<br /><ul><li>Qualified CDI Events (3/3)</li></ul> @Inject<br /> private Event<Customer> eventSource;<br />public String createCustomer() {<br /> Customer customer = ...;<br />eventSource<br /> .select(newAnnotationLiteral<Created>(){})<br /> .fire(customer);<br /> ...<br /> }<br />
  108. 108. CDI Events<br /><ul><li>Conditional Observer Methods
  109. 109. Berücksichtigung von Transaktionen (AFTER_SUCCESS, AFTER_FAILURE,...) und Bean Instanz Status (EXISTS,...)</li></ul>publicvoidsendWelcomeMail(<br /> @Observes(<br />receive=IF_EXISTS, // bean<br />during=BEFORE_COMPETION // tx<br />)<br />@Created<br /> Customer customer) { ... }<br />
  110. 110. CDI Refactoring<br /><ul><li>1 - CDI Wiring
  111. 111. 2 - CDI Current User
  112. 112. 3 - CDI Beansonly
  113. 113. 4 - CDI Style Formulare
  114. 114. 5 - CDI Events
  115. 115. 6 - CDI Ausblick</li></li></ul><li>CDI Ausblick<br /><ul><li>CDI 1.1
  116. 116. Declarative Transaktionen
  117. 117. Embedded Mode
  118. 118. Interceptor/DecoratorOrdering
  119. 119. StaticInjection
  120. 120. SpecCleanup
  121. 121. ...
  122. 122. Aufbau CDI Community
  123. 123. CDI Advocacy*</li></ul>*https://sites.google.com/site/cdipojo/get-started<br />
  124. 124. Java EE without EJB/Spring<br />Fazit<br />Extensions<br />
  125. 125. Java EE without EJB/Spring<br /><ul><li>„Java SE Injection“
  126. 126. Scope System
  127. 127. Fachliche DependencyInjection
  128. 128. Infrastruktur und Businesslogik AOP
  129. 129. Eventmodell
  130. 130. Integrationsframework</li></li></ul><li>Java EE without EJB/Spring<br /><ul><li>CDI ermöglicht ...</li></ul>„Java Enterprise Development withoutEJB – andwithoutSpring“<br /><ul><li>CDI bietet dafür...</li></ul>„typesafeundschichtenneutrales InjectionFramework“ für fachliche und technische Injection sowie Rahmenwerk für eventgetriebene Entwicklung“<br />
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×