【发布时间】:2018-02-08 10:26:00
【问题描述】:
我使用自定义注释对我的 Spring Boot 控制器的一些功能进行注释以用于日志记录。但是,我发现嵌套方法执行了两次之前的建议。在这里寻找一些想法。请参考下面的代码sn-ps。
控制器
@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";
}
注释
@Target({java.lang.annotation.ElementType.METHOD, java.lang.annotation.ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface OperationMILog {
}
方面
@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());
}
}
当触发控制器中的应用功能时,会打印以下日志。
Before ..........apply
Before ..........test
Before ..........test
Before ..........test1
Before ..........test1
在 doBefore 函数处设置断点并在调试模式下查看堆栈跟踪。 打印第一个“Before .......... test”时,堆栈跟踪看起来很好。
GenericLoggingAspect.doBefore(JoinPoint) line: 87
InstallmentController.apply(String, String, InstallmentApplyHttpRequest, String) line: 387
InstallmentController$$FastClassBySpringCGLIB$$55eeb128.invoke(int, Object, Object[]) line: not available
当第二个“Before .......... test”要显示时,堆栈跟踪如下所示
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
我不知道为什么第 593 行会触发 doBefore。同样的情况也适用于 test1 的打印。
我的项目没有任何 XML 配置,所有配置都是在注解中完成的。
【问题讨论】:
-
请记录完整的
joinPoint,而不是仅记录目标方法名称。我想查看日志并检查一些内容。谢谢。 -
@kriegaex .关于这两个“Before......test”,jointPoint的完整日志是:............call(String com.mycomp .controller.InstallmentController.test()) 之前 ..........execution(String com.mycomp.controller.InstallmentController.test())
标签: annotations aop spring-aop