5
我想在我的Grails項目創建一個自定義日誌註釋。AOP使用Grails
我的代碼:
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();
}
}
Spring配置:
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爲好。我錯過了什麼?
尼斯的洞察力!我認爲如果Grails提供了一些神奇的功能來委託調用相同服務類中的方法到代理類。 –