aop方法耗时注解编写

方法耗时是接口优化比较重要的一个点了。
一般的方法耗时处理会直接在方法调用前后进行记然后进行计算,然后通过计算求出最后方法的执行时间,这是最简单最直接的解决方式了。如下是比较传统的处理方式:

        long startTime = System.currentTimeMillis();
        /** 调用处理方法*/
        this.testMet(Object object);
        long endTime = System.currentTimeMillis();
        log.info("操作执完毕,耗时:" + (startTime- endTime ) + "毫秒 !");

但是这种方式会写重复的很多代码!造成代码冗余,发布线上前进行删除也比较麻烦!

相同是处理操作可以用切面的形式进行处理,分为3部分:
1、创建切面自定义 方法耗时的注解

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * @description: 方法耗时
 * @author HuaShangJin
 * @date 2022/7/20 15:17
 * @version 1.0
 */
@Target({ElementType.METHOD,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface TimeConsuming {
}

2、创建对应的aop切面处理类,切面的切入方式选择环绕增强。


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


/**
 * @description: 方法耗时 切面处理类
 * @author HuaShangJin
 * @date 2022/7/20 15:50
 * @version 1.0
 */
@Aspect
@Component
@Slf4j
public class TimeConsumingAspect {
    // 定义切点
    @Pointcut("@annotation(com.nos.imt.aspect.timeConsuming.TimeConsuming)")
    public void doAspect() {
    }

    /**
     * @description: 定义环绕增强
     * @param: joinPoint
     * @return: java.lang.Object
     * @author HuaShangJin
     * @date: 2022/7/20 15:19
     */
    @Around("doAspect()")
    public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
        // 开始时间
        long startTime = System.currentTimeMillis();
        // 值是被拦截方法的返回值
        Object proceed = joinPoint.proceed();
        // 计算执行时间
        long endTime = System.currentTimeMillis();
        //获取类的字节码对象,通过字节码对象获取方法信息
        Class<?> targetCls = joinPoint.getTarget().getClass();
        //获取方法签名(通过此签名获取目标方法信息)
        MethodSignature ms = (MethodSignature) joinPoint.getSignature();
        String method = targetCls.getName() + "." + ms.getName() + "()";

        log.info("*******@TimeConsuming注解切入:" + method + "方法耗时:" + (endTime - startTime) + "毫秒 !*******");
        return proceed;
    }

}

3、在方法上使用该注解即可。

@TimeConsuming
public List<TeamUserVo> testMethod(Long id) {
        System.out.println("我是测试方法!");
}

完毕~

基于AOP(Aspect-Oriented Programming,面向切面编程)实现接口调用的耗时统计,通常会涉及以下步骤: 1. 定义切面(Aspect):创建一个新的切面,它是关注点的模块化容器,如Spring AOP中的`@Aspect`注解。在这个切面中,我们将定义一个通知(Advice),用于拦截特定的接口调用。 2. 切入点(Pointcut):确定需要应用统计的接口方法。这可以是一个接口的所有方法,也可以是特定的实例方法名。 3. 实现通知:选择合适的通知类型,如`@Before`、`@AfterReturning`等,当方法被调用前或调用后执行。在这里,你可以使用`execution()`表达式匹配接口方法。 4. 编写代码执行日志操作:在通知的方法内部,记录开始时间,然后执行原始方法,最后记录结束时间并计算耗时。可以使用`joinPoint.getDuration()`获取方法执行的时间。 5. 使用AOP框架:例如在Spring AOP中,将切面配置到应用上下文中,这样每次执行对应接口时,都会自动触发统计代码。 ```java import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; @Aspect @Component public class TimingAspect { @Around("execution(* com.example.myapp.MyInterface.*(..))") public Object measureTime(ProceedingJoinPoint joinPoint) throws Throwable { long startTime = System.currentTimeMillis(); try { return joinPoint.proceed(); // 执行目标方法 } finally { long endTime = System.currentTimeMillis(); long duration = endTime - startTime; log.info("Method {} took {} ms", joinPoint.getSignature(), duration); } } } ```
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值