小编man*_*gel的帖子

方法拦截的自定义注释中的元素无法解析为变量错误

Spring 和 AOP 编程新手。致力于 Spring AOP 教程,编写拦截方法调用的方面。想要启用时间记录。

\n\n

按照教程的指示,我创建了一个用于日志记录的自定义注释和一个方面来定义调用此注释时应执行的操作。\n下面的代码是 TrackTime 注释:

\n\n
package com.in28minutes.springboot.tutorial.basics.example.aop;\n\nimport java.lang.annotation.Retention;\nimport java.lang.annotation.Target;\n\n@Target(ElementType.METHOD)\n@Retention(RetentionPolicy.RUNTIME)\npublic @interface TrackTime {}\n
Run Code Online (Sandbox Code Playgroud)\n\n

但是 Eclipse 显示错误 \xe2\x80\x93\n\xe2\x80\x9cElement 无法解析为变量/保留无法解析为变量\xe2\x80\x9d

\n\n

然后,我使用 \xe2\x80\x98TrackTime\xe2\x80\x99 注释创建了一个名为 MethodExecutionCalculationAspect 的方面。

\n\n
@Around("@annotation(com.in28minutes.springboot.tutorial.\nbasics.example.aop.TrackTime)")\n
Run Code Online (Sandbox Code Playgroud)\n\n

方法执行计算方面

\n\n
package com.in28minutes.springboot.tutorial.basics.example.aop;\n\nimport org.aspectj.lang.ProceedingJoinPoint;\nimport org.aspectj.lang.annotation.Around;\nimport org.aspectj.lang.annotation.Aspect;\nimport org.slf4j.Logger;\nimport org.slf4j.LoggerFactory;\nimport org.springframework.context.annotation.Configuration;\n\n@Aspect\n@Configuration\npublic class MethodExecutionCalculationAspect {\n    private Logger logger = LoggerFactory.getLogger(this.getClass());\n\n@Around("@annotation\n(com.in28minutes.springboot.tutorial.basics.example.aop.TrackTime)")\n\n    public void around(ProceedingJoinPoint joinPoint) throws Throwable {\n        long startTime = System.currentTimeMillis();\n\n    joinPoint.proceed();\n    long timeTaken = System.currentTimeMillis() - startTime;\n    logger.info("Time Taken by {} is {}", joinPoint, timeTaken);\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

} …

spring annotations aspect

1
推荐指数
1
解决办法
3879
查看次数

标签 统计

annotations ×1

aspect ×1

spring ×1