2013-07-01 33 views
5

Grails projemde özel bir günlük ek açıklaması oluşturmak istiyorum.AOP with Grails

Kodum:

class MyService{ 
    @AuditLog 
    def method1() { 
     println "method1 called" 
     method2() 
    } 
    @AuditLog 
    def method2() { 
     println "method2 called" 
    } 
} 

keseni:

class AuditLogInterceptor implements MethodInterceptor { 
    @Override 
    Object invoke(MethodInvocation methodInvocation) throws Throwable { 
     println "${methodInvocation.method}" 
     return methodInvocation.proceed(); 
    } 
} 

Bahar yapılandırma:

aop { 
    config("proxy-target-class": true) { 
     pointcut(id: "auditLogInterceptorPointcut", expression: "@annotation(xxx.log.AuditLog)") 
     advisor('pointcut-ref': "auditLogInterceptorPointcut", 'advice-ref': "auditLogInterceptor") 
    } 
} 

auditLogInterceptor(AuditLogInterceptor) {} 

sonucu:

public java.lang.Object xxx.MyService.method1() 
method1 called 
method2 called 

Ayrıca, yöntem 2 için ek açıklama ateşini görmek istiyorum. Neyi kaçırıyorum?

cevap

8

iç yöntemine kendine hizmet sınıfında çağırır Bunun nedeni service sınıfın vekalet edilen örneğinde yapmadıbulunmaktadır. Servis çekirdeğini uygulama içeriğinden alırsanız ve method2() numaralı telefonu aramayı denerseniz, advice'u aspect dinlemeniz gerekir.

class MyService{ 
    static transactional = false 
    def grailsApplication 

    @AuditLog 
    def method1() { 
     println "method1 called" 
     grailsApplication.mainContext.myService.method2() 
     //method2() 
    } 
    @AuditLog 
    def method2() { 
     println "method2 called" 
    } 
} 
+0

Nice insight! Grails, proxy sınıfına aynı hizmet sınıfındaki yöntemlere çağrıları devretmek için biraz sihir sağlarsa harika olur. –