【发布时间】:2020-06-13 21:27:34
【问题描述】:
编辑:已解决,因为开头的双星号,我从书中复制了它:Spring Microservices in Action。
我正在尝试建议一些控制器方法。
这是方面类:
@Aspect
public class PBLogger {
@Pointcut("execution(** com.sunwell.product..*.*(..))")
public void standardMethod() {
}
@Around("standardMethod()")
public Object log(ProceedingJoinPoint jp) {
...
}
...
}
这是目标方法,我已经确定包名是正确的。
这个不起作用(不调用建议方法):
@RequestMapping(value = "resources/items", method = RequestMethod.GET,
produces = "application/json"
)
public ResponseEntity<Map<String,Object>> getItems(
@RequestHeader(value="Authorization", required = false) String _auth, @RequestParam(value="systemId", required = false) Integer _i ) throws Exception
{ ... }
没有通用返回类型,它可以工作:
@RequestMapping(value = "resources/items", method = RequestMethod.GET,
produces = "application/json"
)
public void getItems(@RequestHeader(value="Authorization", required = false) String _auth, @RequestParam(value="systemId", required = false) Integer _i ) throws Exception
{ ... }
它也可以使用带注释的切入点而不是执行
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Example
{
public String msg();
}
@Aspect
public class PBLogger {
@Pointcut("@annotation(Example)")
public void standardMethod() {
}
@Around("standardMethod()")
public Object log(ProceedingJoinPoint jp) {
...
}
...
}
//target method:
@RequestMapping(value = "resources/items", method = RequestMethod.GET,
produces = "application/json"
)
@Example
public ResponseEntity<Map<String,Object>> getItems(
@RequestHeader(value="Authorization", required = false) String _auth, @RequestParam(value="systemId", required = false) Integer _ ) throws Exception
{ ... }
【问题讨论】:
-
getItems() 方法之间有很大的不同。首先,一个有参数和返回类型,第二个没有。请分享确切的代码。
-
@dassum 抱歉,我已经编辑了第二个 getItems 方法。参数和返回值都可以,只要它们不是通用的。
-
@Mideel ,这个问题留下了很多东西让其他人猜测和回答。如果可以共享有效/有问题的确切代码,我们将很容易为您提供帮助。
-
实际上我查看了“Spring Microservices in Action”,但那里甚至没有提到 AOP。所以我不认为你有你的
**从那里。但我在“Spring in Action”中多次发现这种奇怪的语法。 -
实际上我查看了“Spring Microservices in Action”,但那里甚至没有提到 AOP。所以我不认为你有你的
**从那里。但是我在 “Spring in Action” 中多次发现这种奇怪的语法 - 不幸的是,特别是在第 4 章中,源代码 ZIP 不包含任何内容。这真是倒霉:书中的代码错误 + 缺少那一章的源代码。
标签: spring spring-boot aop spring-aop