lightweight aop with cdi and jpa
TRANSCRIPT
Prof. Dr. Michael Helbig Montag, 18.02.2013
IT-Berater
Professor Mathematik & Informatik
CCC AOP CDI JPA
CCC AOP CDI JPA
Wikipedia: http://de.wikipedia.org/wiki/Cross-Cutting_Concern
Wikipedia: http://de.wikipedia.org/wiki/Cross-Cutting_Concern
DZone: http://architects.dzone.com/articles/example-cross-cutting-concerns
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
HINWEIS: nur Servlet Container (Tomcat 7) JSF mit Mojarra und Primefaces CDI mit Weld JPA mit Hibernate
CCC AOP CDI JPA
Wikipedia: http://de.wikipedia.org/wiki/Aspektorientierte_Programmierung
CCC AOP CDI 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)
JSR-299 auf JSR-330
Prominente Implementierungen ◦ JBoss Weld (RI) ◦ Apache Open WebBeans ◦ Resin CanDI
Annotation für das InterceptorBinding:
@InterceptorBinding @Target({TYPE, METHOD}) @Retention(RUNTIME) public @interface MyCCC {}
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>
Verwendung des Interceptors
@MyCCC public class AnotherClass {…} - oder - @MyCCC public void myMethod(){…}
Interceptor: DebugMethod Interceptor: Transactional
CCC AOP CDI 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“
Verwendung direkt in Entity:
@Entity public class MyEntity { … Date creationDate; … @PrePersist public void setCreationDate() { this.creationDate = new Date(); } } }
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 { … }
Account: User und TS ergänzen AccountAudit: Schreiben des Logs
Bspe von CCCs Heraustrennung mittels AOP AOP mit CDI AOP mit JPA