java Spring Boot AOP

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26414419/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 09:53:30  来源:igfitidea点击:

Spring Boot AOP

javaspring-bootspring-aop

提问by user1595702

I am having some issues trying to get my advice to execute. I tried several different pointcuts to no avail. The "@EnableAspectJProxy" seems to be working and detects my aspect. Any advice is appreciated.

我在尝试执行我的建议时遇到了一些问题。我尝试了几个不同的切入点,但无济于事。“@EnableAspectJProxy”似乎正在工作并检测到我的方面。任何建议表示赞赏。

I am using spring-boot-aop-starter.

我正在使用 spring-boot-aop-starter。

@Aspect
@Component
public class ExecutionTimeLogger {

    private Logger logger;

    public ExecutionTimeLogger() {
        logger = LoggerFactory.getLogger(getClass());
        logger.info("HEY");
    }

    @Pointcut("within(@org.springframework.stereotype.Controller *)")
    public void controller() {}

    @Pointcut("execution(* edu.x.y.z.server.web.controller.*.*(*))")
    public void methodPointcut() {}

    @Pointcut("within(@org.springframework.web.bind.annotation.RequestMapping *)")
    public void requestMapping() {}

    @Around("controller() && methodPointcut() && requestMapping()")
    public Object profile(ProceedingJoinPoint pjp) throws Throwable {
        StopWatch sw = new StopWatch();
        String name = pjp.getSignature().getName();
        try {
            sw.start();
            return pjp.proceed();
        } finally {
            sw.stop();
            logger.info("STOPWATCH: " + sw.getTime() + " - " + name);
        }
    }
}

I am trying to match any method that is within my package and is annotated with the @RequestMapping annotation. I have tried the very generic match any and all methods without any luck too.

我正在尝试匹配我的包中的任何方法,并使用 @RequestMapping 注释进行注释。我也尝试过非常通用的 match any 和 all 方法,但没有任何运气。

Here is a sample of a method that the advice should be applied to:

以下是建议应用于的方法示例:

@RequestMapping(value = "/analysis", method = RequestMethod.GET)
@ApiOperation(value = "Get analyses available for the current user")
JsonModelAndView getAllAnalyses(HttpServletRequest request)

回答by user1595702

I managed to get this resolved. I ended up creating a small spring application to test the use case with the specific pointcuts to remove other potential barriers. I found that my pointcuts needed some adjusting.

我设法解决了这个问题。我最终创建了一个小型 spring 应用程序来测试具有特定切入点的用例,以消除其他潜在障碍。我发现我的切入点需要一些调整。

@Aspect
@Component
public class ExecutionTimeLogger {

    private Logger logger;

    public ExecutionTimeLogger() {
        logger = LoggerFactory.getLogger(getClass());
        logger.info("HEY");
    }

    @Pointcut("@annotation(org.springframework.web.bind.annotation.RequestMapping)")
    public void requestMapping() {}

    @Pointcut("execution(* edu.x.y.z.server.web.controller.*Controller.*(..))")
    public void methodPointcut() {}

    @Around("requestMapping() && methodPointcut()")
    public Object profile(ProceedingJoinPoint pjp) throws Throwable {
        StopWatch sw = new StopWatch();
        String name = pjp.getSignature().getName();
        try {
            sw.start();
            return pjp.proceed();
        } finally {
            sw.stop();
            logger.info("STOPWATCH: " + sw.getTime() + " - " + name);
        }
    }
}

As you can see the big difference was the annotation pointcut.

正如您所看到的,最大的区别是注释切入点。

回答by Dave Syer

You might want to set proxyTargetClass=true (assuming your controllers do not have an interface). Use your own @EnableASpectJAutoProxyor set spring.aop.proxyTargetClass=true.

You might want to set proxyTargetClass=true (assuming your controllers do not have an interface). Use your own @EnableASpectJAutoProxyor set spring.aop.proxyTargetClass=true.