【发布时间】:2015-06-25 10:20:37
【问题描述】:
我正在研究需要拦截所有使用自定义注释注释的类和方法的日志记录方面。
下面是自定义注解类,可以在类和方法上进行注解:
@Documented
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
public @interface Loggable {
LogLevel value();
}
我正在使用这些切入点表达式来拦截带有注解@Loggable 的方法和类,它适用于所有简单类,但不适用于扩展或实现的类。
//Works for annotation @Loggable on class level
@Pointcut("execution(* *(..)) && within(@com.logger.Loggable *)")
public void classAnnotationLogger() {
}
//Working for methods annotated with @Loggable
@Before(value = "@annotation(loggable)", argNames = "jp, loggable")
public void logBeforeAdvice(JoinPoint jp, Loggable loggable) {
..
..
}
下面是超类的代码
@Component
@Loggable(LogLevel.INFO)
public abstract class Processor{
public void process(){
readProcess();
}
public abstract void readProcess();
}
下面是子类的代码
@Service
@Loggable(LogLevel.INFO)
public class MyServiceProcessor extends Processor {
@Override
public void readProcess(){
...
...
}
}
在应用程序中readProcess()被doing调用
Processor processor = applicationContext.getBean(MyServiceProcessor.class);
processor.readProcess();
即使我在Processor 和MyServiceProcessor 上有@Loggable,但当readProcess 被调用时,建议不会被调用。
但是会为 process() 而不是 readProcess 调用建议。
当注解@Logabble应用于任何类或方法时,我如何编写切入点表达式,它也拦截对任何子类方法的调用?
【问题讨论】:
标签: java spring aspectj spring-aop pointcut