Top Banner
57

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

Jun 24, 2015

Download

Technology

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.
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Java EE Development without EJB - and without Spring - Part 2
Page 2: Java EE Development without EJB - and without Spring - Part 2
Page 3: Java EE Development without EJB - and without Spring - Part 2
Page 4: Java EE Development without EJB - and without Spring - Part 2

In den nächsten 60 Minuten

Refactoring !the Java EE CDI way!

Page 5: Java EE Development without EJB - and without Spring - Part 2

eCustomer Beispiel

§  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

Demo time...!

Page 6: Java EE Development without EJB - and without Spring - Part 2

§  Architekturansatz § Mehrschicht-

architektur

§  Bausteine §  JSF xhtml Seiten

§  Formularobjekte

§  Services

§  Persistente Domain Objekte (User,...)

eCustomer Beispiel

Page 7: Java EE Development without EJB - and without Spring - Part 2

§  *.xhtml §  AuthenticationBean

§  CustomerBean

§  CustomerService(EJB)

§  Customer

Use Case Kunde bearbeiten

Page 8: Java EE Development without EJB - and without Spring - Part 2

Use Case Kunde bearbeiten

§  JSF AuthenticationBean

@ManagedBean!@SessionScoped!public class AuthenticationBean! implements Serializable {!! private User authenticatedUser;! ! public String authenticate() {...}!! public User getAuthenticatedUser() {...}!}!!

Page 9: Java EE Development without EJB - and without Spring - Part 2

Use Case Kunde bearbeiten

§  View customer/edit.xhtml

<html ...><h:body>! <h:form>!! Vorname: <h:inputText ! value=“#{customerBean.firstname}"/>! Name: <h:inputText ! value=“#{customerBean.name}"/> ! !! </h:form>!</h:body></html>!!! !!

Page 10: Java EE Development without EJB - and without Spring - Part 2

Use Case Kunde bearbeiten

§  JSF CustomerBean Attribute (1/3)

@ManagedBean @SessionScoped!public class CustomerBean implements Serializable {! private Customer currentCustomer;! ! public String getFirstname() {! return currentCustomer.getFirstname();! }! public void setFirstname(String firstname) {! currentCustomer.setFirstname(firstname);! }! public String getName() {...}! public void setName(String name) {...}!}!

Page 11: Java EE Development without EJB - and without Spring - Part 2

Use Case Kunde bearbeiten

§  JSF CustomerBean Audit Info (2/3)

@ManagedBean @SessionScoped!public class CustomerBean implements Serializable {!! @ManagedProperty("#{auth..Bean.authenticatedUser}")! private User currentUser;!!! public void setCurrentUser(User currentUser) {! this.currentUser = currentUser;! }!}!

Audit Info

Page 12: Java EE Development without EJB - and without Spring - Part 2

Use Case Kunde bearbeiten

§  JSF CustomerBean Update (3/3)

@ManagedBean @SessionScoped!public class CustomerBean implements Serializable {! @EJB! private CustomerService customerService;!! public String update() {! currentCustomer.! updateAuditInformation(currentUser);! ! customerService.updateCustomer(currentCustomer);! return Outcome.SUCCESS;! }!}!

Audit Info

Page 13: Java EE Development without EJB - and without Spring - Part 2

Use Case Kunde bearbeiten

§  CustomerService EJB

@Stateless!public class CustomerServiceEJB !

! !implements CustomerService {! ! @PersistenceContext! private EntityManager em;!! public Customer updateCustomer(Customer customer) {! return em.merge(customer);! }!}!! !!

Page 14: Java EE Development without EJB - and without Spring - Part 2

CDI Optimierungspotential!?

Page 15: Java EE Development without EJB - and without Spring - Part 2

CDI Refactoring

§  1 - CDI Wiring

§  2 - CDI Current User

§  3 - CDI Beans only

§  4 - CDI Style Formulare

§  5 - CDI Events

§  6 - CDI Ausblick

Page 16: Java EE Development without EJB - and without Spring - Part 2

CDI Refactoring

§  1 - CDI Wiring

§  2 - CDI Current User

§  3 - CDI Beans only

§  4 - CDI Style Formulare

§  5 - CDI Events

§  6 - CDI Ausblick

Page 17: Java EE Development without EJB - and without Spring - Part 2

eCustomer – CDI Wiring

§  JSR 330 Annotationen § @Named (für EL), @Qualifier

§ @Inject

§  JSR 299 Annotationen § @RequestScoped, @SessionScoped,

@ApplicationScoped, ...

Page 18: Java EE Development without EJB - and without Spring - Part 2

eCustomer – CDI Wiring

§  JSF AuthenticationBean

@Named!@SessionScoped!public class AuthenticationBean! implements Serializable {!! ...!!}!!

Page 19: Java EE Development without EJB - and without Spring - Part 2

eCustomer – CDI Wiring

§  JSF CustomerBean

@Named !@SessionScoped!public class CustomerBean implements Serializable {! ! @Inject // no EL support in @Named! private AuthenticationBean authBean;!! @Inject! private CustomerService customerService;!!}!

Page 20: Java EE Development without EJB - and without Spring - Part 2

CDI Refactoring

§  1 - CDI Wiring

§  2 - CDI Current User

§  3 - CDI Beans only

§  4 - CDI Style Formulare

§  5 - CDI Events

§  6 - CDI Ausblick

Page 21: Java EE Development without EJB - and without Spring - Part 2

§  Producer Methods/Fields §  Factory Method Pattern für fachliche

Objekte § @Produces

§ Qualifizierung über eigene fachliche Qualifier

eCustomer – CDI Current User

Page 22: Java EE Development without EJB - and without Spring - Part 2

§  Current Qualifier

@Qualifier!@Target({TYPE, METHOD, PARAMETER, FIELD})!@Retention(RUNTIME)!public @interface Current{}!!!!!

eCustomer – CDI Current User

Page 23: Java EE Development without EJB - and without Spring - Part 2

eCustomer – CDI Current User

§  JSF AuthenticationBean

@Named @SessionScoped!public class AuthenticationBean! implements Serializable {!! private User authenticatedUser;! ! public String authenticate() {...}!! @Produces! @Current! public User getAuthenticatedUser() {...}!}!!

Page 24: Java EE Development without EJB - and without Spring - Part 2

eCustomer – CDI Current User

§  JSF CustomerBean

@Named @SessionScoped!public class CustomerBean implements Serializable {!! //@Inject // no EL support in @Named! //private AuthenticationBean authBean;!! public String update() {! // current.updateAuditInformation(! // authController.getAuthenticatedUser());! ! ...! }!}!

Page 25: Java EE Development without EJB - and without Spring - Part 2

eCustomer – CDI Current User

§  CustomerService EJB

@Stateless!public class CustomerServiceEJB !

! !implements CustomerService {! @Inject ! @Current! private User currentUser;! ! public Customer updateCustomer(Customer customer) {! customer.updateAuditInformation(currentUser);! return em.merge(customer);! }!}!! !!

Alternativ: JPA Event

Listener

Page 26: Java EE Development without EJB - and without Spring - Part 2

CDI Refactoring

§  1 - CDI Wiring

§  2 - CDI Current User

§  3 - CDI Beans only

§  4 - CDI Style Formulare

§  5 - CDI Events

§  6 - CDI Ausblick

Page 27: Java EE Development without EJB - and without Spring - Part 2

§  Injizierbare CDI Ressourcen § Normale Java Klassen

§ Optional mit @Named oder anderen Qualifier markiert

§  Enterprise Java Beans

§  Sonstige Java EE Ressourcen § PersistenceContext

§ UserTransaction §  ...

eCustomer – CDI Beans only

Page 28: Java EE Development without EJB - and without Spring - Part 2

eCustomer – CDI Beans only

§  CDI CustomerService

// nothing required!public class CustomerServiceBean !

! !implements CustomerService {! ! @PersistenceContext! private EntityManager em;!! public Customer updateCustomer(Customer customer) {! return em.merge(customer);! }!}!! !!

Page 29: Java EE Development without EJB - and without Spring - Part 2

eCustomer – CDI Beans only

§  CDI CustomerService

// nothing required!public class CustomerServiceBean !

! !implements CustomerService {! ! @PersistenceContext! private EntityManager em;!! public Customer updateCustomer(Customer customer) {! return em.merge(customer);! }!}!! !!

Transaktionen?!

Page 30: Java EE Development without EJB - and without Spring - Part 2

§  Transactional Annotation

@InterceptorBinding!@Retention(RetentionPolicy.RUNTIME)!@Target({ElementType.METHOD, ElementType.TYPE})!!public @interface Transactional {! ! public TransactionalType value() !

!default TransactionalType.REQUIRED;! !}!

eCustomer – CDI Beans only

Page 31: Java EE Development without EJB - and without Spring - Part 2

§  Transaction Interceptor*

@Transactional @Interceptor!public class TransactionInterceptor {!! @Inject! private UserTransaction utx;!! @AroundInvoke! public Object applyTransaction(!

! !InvocationContext ic) throws Throwable {!! // implement utx.begin(), utx.commit(), ...! } !}!

*XML registration omitted

eCustomer – CDI Beans only

Page 32: Java EE Development without EJB - and without Spring - Part 2

eCustomer – CDI Beans only

§  Transactional CDI CustomerService

!public class CustomerServiceBean !

! !implements CustomerService {! ! @PersistenceContext! private EntityManager em;!! @Transactional ! public Customer updateCustomer(Customer customer) {! ...! }!}!! !!

Page 33: Java EE Development without EJB - and without Spring - Part 2

eCustomer – CDI Beans only

§  Transactional JSF CustomerBean

@Named @SessionScoped!public class CustomerBean implements Serializable {!! @Inject! private CustomerService customerService;!!! @Transactional! public String update() {! ...! }!}!

Page 34: Java EE Development without EJB - and without Spring - Part 2

CDI Refactoring

§  1 - CDI Wiring

§  2 - CDI Current User

§  3 - CDI Beans only

§  4 - CDI Style Formulare

§  5 - CDI Events

§  6 - CDI Ausblick

Page 35: Java EE Development without EJB - and without Spring - Part 2

CDI Style Formulare

Und was hat CDI für die !Formularbehandlung im Angebot?!

Page 36: Java EE Development without EJB - and without Spring - Part 2

CDI Style Formulare

§  CDI Conversations § @ConversationScoped

§ Conversation.begin()

§ Conversation.end()

§  JSF Backing Beans und Producer Methods § @Produces @Named

§ Qualifier

Page 37: Java EE Development without EJB - and without Spring - Part 2

CDI Style Formulare

§  CustomerBean Conversation (1/2)

@Named @ConversationScoped!public class CustomerBean implements Serializable {! private Customer currentCustomer;! @Inject ! private Conversation conversation; !! public String selectCustomer(Customer customer) {! currentCustomer = customer;! conversation.begin();! ... ! }!}!

Page 38: Java EE Development without EJB - and without Spring - Part 2

CDI Style Formulare

§  CustomerBean Conversation (2/2)

@Named @ConversationScoped!public class CustomerBean implements Serializable {!! public String update() {! conversation.end(); ! ...! }!}!

Page 39: Java EE Development without EJB - and without Spring - Part 2

CDI Style Formulare

§  Achtung: Infrastruktur Teil des View

<html ...><h:body>! <h:form>! ! Vorname: <h:inputText ! value=“#{customerBean.firstname}"/>! Name: <h:inputText ! value=“#{customerBean.name}"/> ! ! </h:form>!</h:body></html>!!! !!

Infrastruktur!

Page 40: Java EE Development without EJB - and without Spring - Part 2

CDI Style Formulare

§  JSF und Producer Methods

@Named @ConversationScoped!public class CustomerBean implements Serializable {! private Customer currentCustomer;!! public String selectCustomer(Customer customer) {! ... ! }!! @Produces ! @Named(“selectedCustomer“)! @Selected! public Customer getSelected() { ... } !}!

Page 41: Java EE Development without EJB - and without Spring - Part 2

CDI Style Formulare

§  JSF und Producer Methods

<html ...><h:body>! <h:form>! ! Vorname: <h:inputText ! value=“#{selectedCustomer.firstname}"/>! Name: <h:inputText ! value=“#{selectedCustomer.name}"/> ! !! </h:form>!</h:body></html>!!! !!

Page 42: Java EE Development without EJB - and without Spring - Part 2

CDI Refactoring

§  1 - CDI Wiring

§  2 - CDI Current User

§  3 - CDI Beans only

§  4 - CDI Style Formulare

§  5 - CDI Events

§  6 - CDI Ausblick

Page 43: Java EE Development without EJB - and without Spring - Part 2

CDI Events

§  CDI Eventsystem §  Java EE Observer Pattern

§ Event Object

§ Event Producer § Observer Method

§  Eigenschaften § Schichtenübergreifend §  (Ggfs.) Transaktionsgebunden

§ Synchrone Interaktion

Page 44: Java EE Development without EJB - and without Spring - Part 2

CDI Events

§  CDI Customer Created Event (1/3)

public class CustomerCreatedEvent {!! private Customer customer;!! public CustomerCreatedEvent(Customer customer) {...}!! public Customer getCustomer() { ... }!}!! !

Page 45: Java EE Development without EJB - and without Spring - Part 2

CDI Events

§  CDI Customer Created Event (2/3)

public void sendWelcomeMail(! @Observes ! CustomerCreatedEvent event) { ! ! Customer customer = event.getCustomer();!!}!!

Page 46: Java EE Development without EJB - and without Spring - Part 2

CDI Events

§  CDI Customer Created Event (3/3)

@Inject! private Event<CustomerCreatedEvent> eventSource;!! public String createCustomer() {! Customer customer = ...;! ! eventSource.fire(! new CustomerCreatedEvent(customer)! );! ! ...! }!

Page 47: Java EE Development without EJB - and without Spring - Part 2

CDI Events

Ist das Event Objekt wirklich notwendig?!

Page 48: Java EE Development without EJB - and without Spring - Part 2

CDI Events

§  Qualified CDI Events (1/3)

@Qualifier!@Target({TYPE, METHOD, PARAMETER, FIELD})!@Retention(RUNTIME)!public @interface Created{}!!!!!! !

Page 49: Java EE Development without EJB - and without Spring - Part 2

CDI Events

§  Qualified CDI Events (2/3)

public void sendWelcomeMail(! @Observes ! @Created ! Customer customer) { ! ! ...!}!!

Page 50: Java EE Development without EJB - and without Spring - Part 2

CDI Events

§  Qualified CDI Events (3/3)

@Inject! private Event<Customer> eventSource;!! public String createCustomer() {! Customer customer = ...;! ! eventSource! .select(new AnnotationLiteral<Created>(){})! .fire(customer);! ! ...! }!

Page 51: Java EE Development without EJB - and without Spring - Part 2

§  Conditional Observer Methods §  Berücksichtigung von Transaktionen

(AFTER_SUCCESS, AFTER_FAILURE,...) und Bean Instanz Status (EXISTS,...)

public void sendWelcomeMail(! @Observes( ! receive=IF_EXISTS, // bean ! during=BEFORE_COMPETION // tx ! ) ! @Created ! Customer customer) { ... }!

CDI Events

Page 52: Java EE Development without EJB - and without Spring - Part 2

CDI Refactoring

§  1 - CDI Wiring

§  2 - CDI Current User

§  3 - CDI Beans only

§  4 - CDI Style Formulare

§  5 - CDI Events

§  6 - CDI Ausblick

Page 53: Java EE Development without EJB - and without Spring - Part 2

CDI Ausblick

§  CDI 1.1 § Declarative Transaktionen

§  Embedded Mode

§  Interceptor/Decorator Ordering

§  Static Injection

§  Spec Cleanup

§  ... §  Aufbau CDI Community

§  CDI Advocacy*

*https://sites.google.com/site/cdipojo/get-started

Page 54: Java EE Development without EJB - and without Spring - Part 2

Java EE without EJB/Spring

Extensions Fazit

Page 55: Java EE Development without EJB - and without Spring - Part 2

Java EE without EJB/Spring

§  „Java SE Injection“

§  Scope System

§  Fachliche Dependency Injection

§  Infrastruktur und Businesslogik AOP

§  Eventmodell

§  Integrationsframework

Page 56: Java EE Development without EJB - and without Spring - Part 2

Java EE without EJB/Spring

§  CDI ermöglicht ... „Java Enterprise Development without EJB – and without Spring“

§  CDI bietet dafür... „typesafe und schichtenneutrales Injection Framework“ für fachliche und technische Injection sowie Rahmenwerk für eventgetriebene Entwicklung“

Page 57: Java EE Development without EJB - and without Spring - Part 2

Gibt es noch Fragen?

Dann los ...