小编Bal*_*oth的帖子

Spring AOP - 带有 args 的注释

我在 Spring Boot 中遇到了问题。我正在尝试为某些 RestControllers 提供额外的功能,并且我正在尝试使用一些自定义注释来实现它。这是一个例子。

我的注释:

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyCustomAnnotation {
  String someArg();
}
Run Code Online (Sandbox Code Playgroud)

我的方面:

@Aspect
@Component
public class MyAspect {
  @Around(
    value = "@annotation(MyCustomAnnotation)",
    argNames = "proceedingJoinPoint,someArg"
  )
  public Object addMyLogic(ProceedingJoinPoint proceedingJoinPoint, String someArg)
    throws Throwable
  {
    System.out.println(someArg);
    return proceedingJoinPoint.proceed();
  }
}
Run Code Online (Sandbox Code Playgroud)

我的方法:

@MyCustomAnnotation(someArg = "something")
@GetMapping("/whatever/route")
public SomeCustomResponse endpointAction(@RequestParam Long someId) {
  SomeCustomResult result = someActionDoesNotMatter(someId);
  return new SomeCustomResponse(result);
}
Run Code Online (Sandbox Code Playgroud)

主要基于文档(https://docs.spring.io/spring/docs/3.0.3.RELEASE/spring-framework-reference/html/aop.html - 7.2.4.6 Advice parameters)我很确定,它应该管用。

我在这里,因为它不...

让我发疯的是,即使是 Intellij,当试图帮助处理 argNames(空字符串 -> 红色下划线 -> alt+enter -> 正确的 …

spring-aop spring-annotations spring-boot

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