我有我的注释,每当我的方法(有我的注释)被执行时,AOP应该被调用,但它在我的Spring Boot控制器中不起作用。但是它适用于具有其他注释的方法。请帮助我了解发生了什么.
更新:我的注释
@Retention(RUNTIME)
@Target({ METHOD, CONSTRUCTOR })
public @interface MyAnnotation {
}
@Aspect
@Component
public class AnnotationAspect {
private static final String POINTCUT_METHOD1 = "@annotation(com.somepackage.MyAnnotation)";
@Around(POINTCUT_METHOD1)
public Object weaveJoinPoint(ProceedingJoinPoint joinPoint) throws Throwable {
try {
System.out.println("Beforeee " + joinPoint);
joinPoint.proceed();
} finally {
System.out.println("Afterrr " + joinPoint);
}
return null;
}
}
场景1:(工作)
@Controller
@RequestMapping("user")
public class ArticleController {
@GetMapping("article/{id}")
@MyAnnotation // here it is
public ResponseEntity<String> getArticleById(@PathVariable("id") Integer id)
{
return new ResponseEntity<String>(dummyMethod(), HttpStatus.OK);
}
public String dummyMethod() {
System.out.println("Dummy method with MyAnnotation");
return "HelloWorld!";
}
}
日志:(工作中)
Beforeee execution(ResponseEntity com.mypackage.getArticleById(Integer))
Dummy method with MyAnnotation
Afterrr execution(ResponseEntity com.mypackage.getArticleById(Integer))
场景2:(不工作)
@Controller
@RequestMapping("user")
public class ArticleController {
@GetMapping("article/{id}")
public ResponseEntity<String> getArticleById(@PathVariable("id") Integer id)
{
return new ResponseEntity<String>(dummyMethod(), HttpStatus.OK);
}
@MyAnnotation //here it is
public String dummyMethod() {
System.out.println("Dummy method with MyAnnotation");
return "HelloWorld!";
}
}
日志:(不工作)
Dummy method with MyAnnotation
场景 3:(不工作)
@Service
public class ArticleService {
@MyAnnotation //here it is
public String dummyMethod() {
System.out.println("Dummy method with MyAnnotation");
return "HelloWorld!";
}
}
它可能不起作用,因为您从同一个类调用 dummyMethod()。尝试将 dummyMethod() 移至另一个服务类。原因是同一类内的调用不会通过 Spring 代理。对 getArticleById() 的调用是代理的,将由 AOP 处理,但 dummyMethod() 也可能是私有方法。
您能解决这个问题吗?我也面临同样的情况
自定义注释:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface LogExecutionProfile {}
方面类:
@Slf4j
@Aspect
@Component
public class LoggingAspect {
@Around("@annotation(LogExecutionProfile) || execution(public * @LogExecutionProfile *.*(..)))")
public Object profileMethod(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
MethodSignature methodSignature = (MethodSignature)
proceedingJoinPoint.getSignature();
String className = methodSignature.getDeclaringType().getSimpleName();
String methodName = methodSignature.getName();
System.out.println("###-### " + className + "." + methodName + " Started profiling execution time and memory usage.");
log.debug("###-### " + className + "." + methodName + " Started profiling execution time and memory usage.");
long startTime = System.currentTimeMillis();
long memoryAtStart = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
Object result = proceedingJoinPoint.proceed();
long endTime = System.currentTimeMillis();
long memoryAtEnd = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
log.debug("###-### " + className + "." + methodName + " execution time: " + (endTime - startTime) + " ms"
+ " memory used: " + (memoryAtEnd - memoryAtStart) + " bytes of memory");
System.out.println("###-### " + className + "." + methodName + " execution time: " + (endTime - startTime) + " ms"
+ " memory used: " + (memoryAtEnd - memoryAtStart) + " bytes of memory");
return result;
}
}