当前位置: 首页 > 知识库问答 >
问题:

spring aop切入点未触发

锺离慈
2023-03-14

问题是@before和@afterreturn有效,但对于pointcut却不是这样。

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());
    }
}

共有1个答案

柯唯
2023-03-14

当您用@pointcut注释一些东西时,您基本上是在定义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建议一起使用。

 类似资料:
  • 我对Spring和AOP是新手。我正在尝试这个简单的事情,我已经创建了一个自定义注释,当放在任何方法之前时,它应该执行一些代码。这是我创建的注释 问题是我的spring aop从来没有被触发过。我在中包含了一个bean 有人能指出我缺少什么吗?

  • 我目前正在尝试使用aspectj和Spring aop为调用HttpServletResponse.send重定向(api dochttp://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletResponse.html)添加一个切入点。类和切入点的代码如下: 目前,尽管上面的代码编译了,但它实际上从未执行过(日志中缺少警告输出和

  • wanner测试spring boot(1.5.20)aop(最小代码) 类被aopped时, 请让我知道我错过了什么

  • 在Spring boot AOP应用程序中,我有一个切入点。如果放在使用此注释注释的执行对象或注释的方法上,则应执行建议。 当我运行应用程序时 这里讨论了一个类似的问题,但切入点似乎太宽了,而我的切入点不是,因为我在项目中只有几个带有注释的类。 我把这个注释放在假客户端界面上,就像这样。 你知道我的切入点怎么了吗?

  • 我试图在方法注释上创建一个Aeyj切入点,但我总是用不同的方法失败。我使用的是aspectj自动代理(我在Spring上下文中没有配置其他编织)。我的类如下所示: 所以我想知道为什么aspectj不会创建切入点。我设法使用执行(**(…)使其工作抛出一些exc)这对我来说很好,但我仍然想知道我做错了什么。 另外,由于是在接口中定义的,我指定了实现类的注释,有没有办法让它以这种方式工作?其他代理机制

  • 主要内容:读者,前提条件,Spring AOP 概述Spring框架的关键组件之一是面向方面编程(AOP)框架。 面向方面的编程需要将程序逻辑分解成不同的部分。 此教程将通过简单实用的方法来学习Spring框架提供的AOP/面向方面编程。 读者 本教程主要是为Spring 面向方面编程(AOP)初学者准备的,帮助他们了解与Spring的AOP框架相关的基础到高级概念。 前提条件 在开始练习本教程系列文章中给出的各种类型的示例之前,我们假设您已经了解