繁体   English   中英

通过控制器方法上的注释的Spring AOP不起作用

[英]Spring aop by annotation on controller method does not work

注解

@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?

尝试这个:

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

我认为您需要使用@within ...此博客文章可能会对https://www.productiveprogrammer.net/?p=49有所帮助

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM