繁体   English   中英

AOP和控制器的春季启动问题

[英]spring boot problems with aop and controller

当我使用spring boot时,aop不能与注释控制器一起使用,我该怎么办?

@Aspect
@Component
@Configuration
public class MethodStatisticsPointcutAspect {

    @Resource
    private CounterService counterService;

    // aop defined
    @Around("@annotation(com.xxx.xxx.metrics.annotation.MethodStatistics)")
    private void around(ProceedingJoinPoint pjp) {
        // do sth
    }
}

我的控制器定义如下:

@RestController
@RequestMapping("/usertest")
public class UserTestController {

    @RequestMapping("/test")
    @MethodStatistics
    String test() {
       // do sth
   }
}

我希望使用所有带有注解@MethodStatistics的方法的aop管理器,但它根本无法与@controller一起使用〜

spring(和spring-boot)中的运行时aop绑定只能插入公共方法。 如果要插入受保护的,私有的或受包保护的方法,则必须使用AspectJ代码编织器,而不是spring实现的运行时代理。

我在自己的项目中偶然发现了这个问题,直到阅读了指出这一点的文档(我的方法受到保护)

来自文档:[ http://docs.spring.io/spring/docs/current/spring-framework-reference/html/aop.html ]

由于Spring的AOP框架基于代理的性质,因此从定义上讲,无论是JDK代理(不适用)还是CGLIB代理(在技术上可行,但不建议用于AOP的目的),都不会拦截受保护的方法。 结果,任何给定的切入点将仅与公共方法匹配!

如果您的拦截需求包括受保护的/私有方法甚至构造函数,请考虑使用Spring驱动的本机AspectJ编织,而不是Spring的基于代理的AOP框架。 这构成了具有不同特性的AOP使用模式的不同,因此请确保在做出决定之前先熟悉编织。

暂无
暂无

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

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