I am working on a logging aspect which need to intercept all the classes and methods annotated with a custom annotation.
Below is custom annotation class which can be annotated on class and methods:
@Documented
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
public @interface Loggable {
LogLevel value();
}
I am using these pointcut expressions to intercept methods and classes with annotation @Loggable
, which is working for all the simple classes but does not work for classses which extend or implement.
//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) {
..
..
}
Below is code for super class
@Component
@Loggable(LogLevel.INFO)
public abstract class Processor{
public void process(){
readProcess();
}
public abstract void readProcess();
}
Below is code for subclass
@Service
@Loggable(LogLevel.INFO)
public class MyServiceProcessor extends Processor {
@Override
public void readProcess(){
...
...
}
}
In the application readProcess()
is called by doing
Processor processor = applicationContext.getBean(MyServiceProcessor.class);
processor.readProcess();
Even though I have @Loggable
on Processor
and MyServiceProcessor
, when readProcess
is called the advice is not being invoked.
But advice is invoked for process()
and not readProcess
.
How do I write the pointcut expression which also intercepts the call to any subclass methods, when annotation @Logabble
is applied on any class or method?