lightweight aop with cdi and jpa

26
Prof. Dr. Michael Helbig Montag, 18.02.2013

Upload: mh0708

Post on 03-Jul-2015

875 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Lightweight AOP with CDI and JPA

Prof. Dr. Michael Helbig Montag, 18.02.2013

Page 2: Lightweight AOP with CDI and JPA

IT-Berater

Professor Mathematik & Informatik

Page 3: Lightweight AOP with CDI and JPA

CCC AOP CDI JPA

Page 4: Lightweight AOP with CDI and JPA

CCC AOP CDI JPA

Page 5: Lightweight AOP with CDI and JPA

Wikipedia: http://de.wikipedia.org/wiki/Cross-Cutting_Concern

Page 6: Lightweight AOP with CDI and JPA

Wikipedia: http://de.wikipedia.org/wiki/Cross-Cutting_Concern

Page 7: Lightweight AOP with CDI and JPA

DZone: http://architects.dzone.com/articles/example-cross-cutting-concerns

Page 8: Lightweight AOP with CDI and JPA

8 Core Concerns ◦ Database Access ◦ Data Entities ◦ Worker ◦ Result Processing ◦ Process Flow Manager ◦ Email/Notification ◦ Error Handling ◦ Logging

8 Core Concerns with 5 CCC ◦ Database Access ◦ Data Entities ◦ Worker ◦ Result Processing ◦ Process Flow Manager ◦ Email/Notification ◦ Error Handling ◦ Logging

Page 9: Lightweight AOP with CDI and JPA

HINWEIS: nur Servlet Container (Tomcat 7) JSF mit Mojarra und Primefaces CDI mit Weld JPA mit Hibernate

Page 10: Lightweight AOP with CDI and JPA
Page 11: Lightweight AOP with CDI and JPA

CCC AOP CDI JPA

Page 12: Lightweight AOP with CDI and JPA

Wikipedia: http://de.wikipedia.org/wiki/Aspektorientierte_Programmierung

Page 13: Lightweight AOP with CDI and JPA

CCC AOP CDI JPA

Page 14: Lightweight AOP with CDI and JPA

more than a DI framework… JEE, auch Java-SE DI (Hollywood Principle: „Don‘t call us, we‘ll

call you“) Producer Methods (Factory-Method-Pattern) Events (lokales Messaging) Interceptors (AOP) Decorators (Decorator-Pattern,

„domainnahes“ AOP)

Page 15: Lightweight AOP with CDI and JPA

JSR-299 auf JSR-330

Prominente Implementierungen ◦ JBoss Weld (RI) ◦ Apache Open WebBeans ◦ Resin CanDI

Page 16: Lightweight AOP with CDI and JPA

Annotation für das InterceptorBinding:

@InterceptorBinding @Target({TYPE, METHOD}) @Retention(RUNTIME) public @interface MyCCC {}

Page 17: Lightweight AOP with CDI and JPA

Interceptor-Implementierung @Interceptor @MyCCC public class MyCCCInterceptor { @AroundInvoke

public Object doCCC(InvocationContext context) { Object result = null; // logic before method call … // method call result = context.proceed(); // logic after method call … return result;

} }

Aktivierung in beans.xml (Reihenfolge!) <beans> <interceptors> <class>de.bigdev.MyCCCInterceptor</class> </interceptors> </beans>

Page 18: Lightweight AOP with CDI and JPA

Verwendung des Interceptors

@MyCCC public class AnotherClass {…} - oder - @MyCCC public void myMethod(){…}

Page 19: Lightweight AOP with CDI and JPA

Interceptor: DebugMethod Interceptor: Transactional

Page 20: Lightweight AOP with CDI and JPA

CCC AOP CDI JPA

Page 21: Lightweight AOP with CDI and JPA

@PostLoad -„After Select Trigger“, z.B. Filtern

@PrePersist - „Before Insert Trigger“ @PostPersist - „After Insert Trigger“

@PreUpdate -„Before Update Trigger“ (Dirty Check) @PostUpdate - „After Update Trigger“ (Dirty Check)

@PreRemove - „Before Delete Trigger“ @PostRemove - „After Delete Trigger“

Page 22: Lightweight AOP with CDI and JPA

Verwendung direkt in Entity:

@Entity public class MyEntity { … Date creationDate; … @PrePersist public void setCreationDate() { this.creationDate = new Date(); } } }

Page 23: Lightweight AOP with CDI and JPA

Separate EntityListener-Klasse

public class MyEntityListener { @PrePersist public void setCreationDate(Object entity) { if (entity instanceof Person) { Person person = (Person) entity;

person.setCreatedAt(new Date()); }}} Registrierung an den Entities

@Entity @EntityListeners(MyEntityListener.class) public class Person { … }

Page 24: Lightweight AOP with CDI and JPA

Account: User und TS ergänzen AccountAudit: Schreiben des Logs

Page 25: Lightweight AOP with CDI and JPA

Bspe von CCCs Heraustrennung mittels AOP AOP mit CDI AOP mit JPA

Page 26: Lightweight AOP with CDI and JPA