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 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                    Aktivierung in beans.xml (Reihenfolge!)
        …

                                                  <beans>
        return result;
    }                                                 <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
Lightweight AOP with CDI and JPA

Lightweight AOP with CDI and JPA

  • 1.
    Prof. Dr. MichaelHelbig Montag, 18.02.2013
  • 2.
    IT-Berater  Professor Mathematik & Informatik
  • 3.
    CCC  AOP  CDI  JPA
  • 4.
    CCC  AOP  CDI  JPA
  • 5.
    Wikipedia: http://de.wikipedia.org/wiki/Cross-Cutting_Concern
  • 6.
    Wikipedia: http://de.wikipedia.org/wiki/Cross-Cutting_Concern
  • 7.
    DZone: http://architects.dzone.com/articles/example-cross-cutting-concerns
  • 8.
    8 Core Concerns with 5 CCC ◦ Database Access ◦ Data Entities ◦ Worker ◦ Result Processing ◦ Process Flow Manager ◦ Email/Notification ◦ Error Handling ◦ Logging
  • 9.
    HINWEIS: nur Servlet Container (Tomcat 7)  JSF mit Mojarra und Primefaces  CDI mit Weld  JPA mit Hibernate
  • 11.
    CCC  AOP  CDI  JPA
  • 12.
    Wikipedia: http://de.wikipedia.org/wiki/Aspektorientierte_Programmierung
  • 13.
    CCC  AOP  CDI  JPA
  • 14.
    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)
  • 15.
    JSR-299 auf JSR-330  Prominente Implementierungen ◦ JBoss Weld (RI) ◦ Apache Open WebBeans ◦ Resin CanDI
  • 16.
    Annotation für das InterceptorBinding: @InterceptorBinding @Target({TYPE, METHOD}) @Retention(RUNTIME) public @interface MyCCC {}
  • 17.
    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  Aktivierung in beans.xml (Reihenfolge!) … <beans> return result; } <interceptors> } <class>de.bigdev.MyCCCInterceptor</class> </interceptors> </beans>
  • 18.
    Verwendung des Interceptors @MyCCC public class AnotherClass {…} - oder - @MyCCC public void myMethod(){…}
  • 19.
    Interceptor: DebugMethod  Interceptor: Transactional
  • 20.
    CCC  AOP  CDI  JPA
  • 21.
    @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“
  • 22.
    Verwendung direkt in Entity: @Entity public class MyEntity { … Date creationDate; … @PrePersist public void setCreationDate() { this.creationDate = new Date(); } } }
  • 23.
    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 { … }
  • 24.
    Account: User und TS ergänzen  AccountAudit: Schreiben des Logs
  • 25.
    Bspe von CCCs  Heraustrennung mittels AOP  AOP mit CDI  AOP mit JPA