【发布时间】:2018-07-24 08:55:43
【问题描述】:
我在 spring boot 中遇到了问题。我正在尝试为一些 RestControllers 提供额外的功能,并且我正在尝试使用一些自定义注释来实现它。这是一个例子。
我的注释:
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyCustomAnnotation {
String someArg();
}
我的方面:
@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();
}
}
我的方法:
@MyCustomAnnotation(someArg = "something")
@GetMapping("/whatever/route")
public SomeCustomResponse endpointAction(@RequestParam Long someId) {
SomeCustomResult result = someActionDoesNotMatter(someId);
return new SomeCustomResponse(result);
}
主要基于文档(https://docs.spring.io/spring/docs/3.0.3.RELEASE/spring-framework-reference/html/aop.html - 7.2.4.6 建议参数)我很确定,它应该可以工作。
我在这里,因为它不...
让我抓狂的是,即使是 Intellij,在尝试帮助处理 argNames(空字符串 -> 红色下划线 -> alt+enter -> 正确的 argNames 属性)时也会给我这个,并保持红色......
根据文档,甚至不需要proceedingJoinPoint(没有它也不起作用):“如果第一个参数是JoinPoint,ProceedingJoinPoint...”
使用当前设置,它显示“未绑定切入点参数'someArg'”
在这一点上,我还应该注意,没有 args 它可以正常工作。
其实我有两个问题:
为什么这不起作用? (这很明显)
如果我想为某些控制器提供一些额外的功能,并且我想从外部对其进行参数化,那么它在 Spring Boot 中是正确的模式吗? (对于 python,使用装饰器很容易做到这一点 - 我不太确定,我不会被类似的语法误导)
一个例子(上面的例子很抽象):
我想创建一个@LogEndpointCall 注解,路由的开发者稍后可以将它放在他正在开发的端点上
...但是,如果他可以添加一个字符串(或者更可能是一个枚举)作为参数,那就太好了
@LogEndpointCall(EndpointCallLogEnum.NotVeryImportantCallWhoCares)
或
@LogEndpointCall(EndpointCallLogEnum.PrettySensitiveCallCheckItALot)
这样会触发相同的逻辑,但使用不同的参数 -> 并将保存到不同的目的地。
【问题讨论】:
标签: spring-boot spring-aop spring-annotations