简体   繁体   中英

Spring aop by annotation on controller method does not work

Annotation

@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

@Controller
public class MyController {

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

in above condition, my aop does not work...
It works fine with other methods, which is not annotated by @Controller .
And it works fine with aop expression and controller method.

Is it possible to use aop by annotation with controller method?

Try this:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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