【问题标题】:Spring aop by annotation on controller method does not work通过控制器方法上的注释的Spring aop不起作用
【发布时间】:2018-08-22 23:01:02
【问题描述】:

注释

@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
    String value();
}

AOP

@Aspect
@Component
public class MyAspect {

@Around("@annotation(MyAnnotation)")
public Object aroundHandler(ProceedingJoinPoint joinPoint) throws Throwable {
    ...
}

控制器

@Controller
public class MyController {

    @RequestMapping(value="/hello", method=RequestMethod.GET)
    @ResponseBody
    @MyAnnotation(value="hello")
    public String hello() {
        return "hello";
    }
}

在上述情况下,我的 aop 不起作用...
它可以与@Controller 未注释的其他方法一起正常工作。
它适用于 aop 表达式和控制器方法。

控制器方法可以通过注解来使用aop吗?

【问题讨论】:

  • 我把你丢在这里了It works fine with other methods, which is not annotated by @Controller - And it works fine with aop expression and controller method 你也能多贴一点代码吗?

标签: java spring annotations aop spring-aop


【解决方案1】:

试试这个:

@Around("@annotation(myAnnotation)")
public Object aroundHandler(ProceedingJoinPoint joinPoint,MyAnnotation myAnnotation) throws Throwable {
    // Do Something
}

【讨论】:

    【解决方案2】:

    我认为你需要使用@within...这篇博文可能对https://www.productiveprogrammer.net/?p=49有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多