I annotate my spring boot controller some functions with a custom annotation for logging purpose. However, I find the before advice is executed twice for nested methods. Looking for some idea here. Please refer to the code snippets below.
Controller
@RequestMapping(value = "apply")
@OperationMILog
public ApplyHttpResponse apply(@RequestHeader final String custId, @RequestAttribute final String cardNo,
@RequestBody final InstallmentApplyHttpRequest installApplyReq, @PathVariable final String source) {
//test
this.test(); //**line 387**
...
}
....
@OperationMILog
private String test() {
return this.test1(); //**line 593**
}
@OperationMILog
private String test1() {
return "test1";
}
The annotation
@Target({java.lang.annotation.ElementType.METHOD, java.lang.annotation.ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface OperationMILog {
}
The Aspect
@Aspect
public class GenericLoggingAspect {
public static GenericLoggingAspect genericLoggingAspect;
@PostConstruct
public void init() {
genericLoggingAspect = this;
}
@Before("@annotation(com.mycomp.log.OperationMILog)")
public void doBefore(JoinPoint joinPoint) {
System.out.println("Before .........." + joinPoint.getSignature().getName());
}
}
When triggering the apply function in controller, the following logs are printed.
Before ..........apply
Before ..........test
Before ..........test
Before ..........test1
Before ..........test1
Settin break point at doBefore function and Looking into the stack trace in debug mode . When the first "Before ............ test" is printed,the stack trace looks fine.
GenericLoggingAspect.doBefore(JoinPoint) line: 87
InstallmentController.apply(String, String, InstallmentApplyHttpRequest, String) line: 387
InstallmentController$$FastClassBySpringCGLIB$$55eeb128.invoke(int, Object, Object[]) line: not available
When the second "Before .......... test" is going to show, the stack trace is quite wired as below
GenericLoggingAspect.doBefore(JoinPoint) line: 87
InstallmentController.test() line: 593
InstallmentController.apply(String, String, InstallmentApplyHttpRequest, String) line: 387
InstallmentController$$FastClassBySpringCGLIB$$55eeb128.invoke(int, Object, Object[]) line: not available
I am running out of idea why line 593 triggers the doBefore. The same case applies to the printing of test1.
My project doesn't have any XML configuration, all configurations are done in annotations.
joinPoint
instead of the target method name only. I would like to see the log and check something. Thanks. – kriegaex