我想在我的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的注解触发。我遗漏了什么?
发布于 2013-07-02 05:59:38
这是因为服务类中对self的内部方法调用是在service类的代理实例上而不是完成的。如果您从应用程序上下文获取服务bean并尝试调用method2(),您应该会看到aspect正在侦听advice。
class MyService{
static transactional = false
def grailsApplication
@AuditLog
def method1() {
println "method1 called"
grailsApplication.mainContext.myService.method2()
//method2()
}
@AuditLog
def method2() {
println "method2 called"
}
}https://stackoverflow.com/questions/17410018
复制相似问题