Spring AOP:统计各个业务层方法执行耗时

本文介绍了如何使用SpringAOP和AspectJ在UserServiceImpl类的方法执行前后记录耗时,通过@Around注解和切入点表达式实现对特定方法的包围通知。
摘要由CSDN通过智能技术生成

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

@Slf4j  //记录日志
@Component  //将当前类交给ioc容器管理,成为ioc容器中的bean对象
@Aspect //代表当前类是一个aop类,在该类中定义模板方法来面向特定方法编程
public class TimeAspect {

    /**
     * Spring AOP:统计各个业务层方法执行耗时
     *
     * @param joinPoint 封装原始方法的所有信息
     * @return Object 原始方法的返回值
     * @throws Throwable 运行原始方法可能出现异常,向上抛出
     */
    @Around("execution(* com.service.impl.UserServiceImpl.*(..))") //切入点表达式
    public Object recordTime(ProceedingJoinPoint joinPoint) throws Throwable {
        //1. 记录开始时间
        long begin = System.currentTimeMillis();    //拿到当前系统毫秒值,因为毫秒值数值较大,所以考虑用long存储

        //2. 调用原始方法运行
        Object result = joinPoint.proceed();    //原始方法运行可能会有返回值,将其交给Object对象(祖宗类)

        //3. 记录结束时间, 计算方法执行耗时
        long end = System.currentTimeMillis();  //拿到当前系统毫秒值
        //在日志中输出调用的具体方法的运行耗时
        log.info(joinPoint.getSignature()+"方法执行耗时: {}ms", end-begin);

        return result;
    }

    /**
     * 1.@Around注解
     *   (1)指定该方法作用在哪些方法上
     *   (2)切入点表达式:指定作用的具体方法
     *   (3)"execution(* com.service.impl.UserServiceImpl.*(..))"
     *          运行这个包下的所有的接口或者类中所有的方法时都会运行recordTime()方法中封装的公用的逻辑代码
     * 2.ProceedingJoinPoint:AOP中提供的一个api
     * 3.proceed()方法:调用原始方法
     * 4.getSignature():拿到原始方法的签名,
     */

}
  • 7
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
可以通过在方法前后添加时间戳,然后计算时间差来计算方法耗时。也可以使用Spring AOP的拦截器,在方法执行前后记录时间戳,然后计算时间差。以下是一个使用Spring AOP计算方法耗时的示例: 1. 定义一个切面类,实现MethodInterceptor接口 ```java @Component @Aspect public class TimingAspect implements MethodInterceptor { @Override public Object invoke(MethodInvocation invocation) throws Throwable { long startTime = System.currentTimeMillis(); Object result = invocation.proceed(); long endTime = System.currentTimeMillis(); long elapsedTime = endTime - startTime; System.out.println(invocation.getMethod().getName() + " 方法耗时:" + elapsedTime + "ms"); return result; } } ``` 2. 在配置文件中启用AOP,并将切面类作为切点 ```xml <aop:aspectj-autoproxy /> <bean id="timingAspect" class="com.example.TimingAspect" /> <aop:config> <aop:aspect ref="timingAspect"> <aop:pointcut expression="execution(* com.example.service.*.*(..))" /> <aop:around method="invoke" /> </aop:aspect> </aop:config> ``` 3. 在需要计算耗时方法上加上@LogExecutionTime注解 ```java @Service public class MyService { @LogExecutionTime public void doSomething() { // do something } } ``` 4. 定义@LogExecutionTime注解和切面类 ```java @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface LogExecutionTime { } @Component @Aspect public class TimingAspect { @Around("@annotation(com.example.LogExecutionTime)") public Object logExecutionTime(ProceedingJoinPoint joinPoint) throws Throwable { long startTime = System.currentTimeMillis(); Object result = joinPoint.proceed(); long endTime = System.currentTimeMillis(); long elapsedTime = endTime - startTime; System.out.println(joinPoint.getSignature().getName() + " 方法耗时:" + elapsedTime + "ms"); return result; } } ``` 这样,在调用doSomething方法时,就会输出方法耗时的信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值