【问题标题】:spring aop pointcut not triggeredspring aop 切入点未触发
【发布时间】:2017-05-26 10:45:19
【问题描述】:

问题是 @Before 和 @AfterReturning 工作正常,但切入点不是这样。

这是我的一面。

作为springboot服务的一部分,我想做的是用第一种方法profile触发切入点来显示执行时间和其他东西。

我错过了什么吗?

package com.myproj.service.myagg.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.util.StopWatch;

/**
 * Created by shammami on 26/05/2017.
 */
@Aspect
@Component
public class LoggingService {

    @Pointcut("execution(public void com.myproj.service.myagg.listener.MyMessageConsumer.handleMessage(..))")
    public Object profile(ProceedingJoinPoint pjp) throws Throwable {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        boolean isExceptionThrown = false;
        try {
            // execute the profiled method
            return pjp.proceed();
        } catch (RuntimeException e) {
            isExceptionThrown = true;
            throw e;
        } finally {
            stopWatch.stop();
            StopWatch.TaskInfo taskInfo = stopWatch.getLastTaskInfo();
            // Log the method's profiling result
            String profileMessage = taskInfo.getTaskName() + ": " + taskInfo.getTimeMillis() + " ms" +
                    (isExceptionThrown ? " (thrown Exception)" : "");
            System.out.println(profileMessage);
        }
    }

    @Before("execution(public void com.myproj.service.myagg.listener.MyMessageConsumer.handleMessage(..))")
    public void before(JoinPoint joinPoint) {
        System.out.println("Started: " + joinPoint.getStaticPart().getSignature().toLongString());
    }

    @AfterReturning("execution(public void com.myproj.service.myagg.listener.MyMessageConsumer.handleMessage(..))")
    public void completed(JoinPoint joinPoint) {
        System.out.println("Completed: " + joinPoint.getStaticPart().getSignature().toLongString());
    }
}

【问题讨论】:

    标签: spring-aop


    【解决方案1】:

    当您使用 @Pointcut 注释某些内容时,您基本上是在定义切入点签名,您不能在其中进行任何类型的处理。您需要做的是创建另一个方法,该方法具有所有处理细节并使用您在上面评估的切入点签名。因此,

    @Pointcut("execution(public void com.myproj.service.myagg.listener.MyMessageConsumer.handleMessage(..))")
    public void myPointcutSignature(){
    //This basically has nothing :)  
    }
    
    @Around("myPointcutSignature")
    public Object profile(ProceedingJoinPoint pjp) throws Throwable {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        boolean isExceptionThrown = false;
       //And the remaining code
      -------
     }
    

    希望这行得通。还要记住 ProceedingJoinPoint 只能与 @Around 建议一起使用。

    【讨论】:

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