2013-07-01 3 views
5

Grails 프로젝트에서 맞춤 로깅 주석을 만들고 싶습니다.Grails가 포함 된 AOP

내 코드 :

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

인터셉터 :

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

봄 설정 :

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

auditLogInterceptor(AuditLogInterceptor) {} 

결과 :

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

방법 2의 주석 화재도보고 싶습니다. 내가 뭘 놓치고 있니?

답변

8

클래스의 프록시 된 인스턴스에서 이 아닌이 수행되지 않았기 때문에 내부 서비스 메서드 클래스에서 내부 메서드 호출이 발생합니다. 응용 프로그램 컨텍스트에서 서비스 bean을 가져 와서 method2()을 호출하려고 시도하면 advice을 청취하여 aspect을 확인해야합니다.

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

멋진 통찰력! Grails가 동일한 서비스 클래스에있는 메소드에 대한 호출을 프록시 클래스에 위임 할 수있는 마법을 제공하면 좋을 것 같습니다. –