AOP在实际场景中的使用以及基于注解方式实现接口耗时统计

废话少说,直接上代码!!!

一、利用aop实现接口耗时统计

import com.xxxx.cloud.business.dataVisualize.query.SludgeCompositeQuery;
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.springframework.stereotype.Component;

/**
 * @className: TimeAspect
 * @author: xxxxxxxxxx
 * @Date: 2022/01/15
 **/
@Aspect
@Slf4j
@Component
public class TimeAspect {
     
	 
	//使用正则表达式的方式给SludgeCompositeService这个接口中的所有实现的方法调用加上耗时统计
    @Pointcut("execution(* com.xxxx.cloud.business.dataVisualize.service.SludgeCompositeService.*(*))")
    public void sludgeCompositeServicePointCut() {
    }

    @Around("sludgeCompositeServicePointCut()")
    public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
        long start = System.currentTimeMillis();
        String methodName = joinPoint.getSignature().getName();
        try {
            return joinPoint.proceed();
        } finally {
            long finish = System.currentTimeMillis();
            log.info("===========调用" + methodName + "方法总耗时 :======={} 毫秒 。", (finish - start));
        }
    }
	
	
    //给具体的方法加上耗时统计(并且带上方法上的具体参数sludgeCompositeQuery)
    @Pointcut(value = "execution(* com.xxxx.cloud.business.dataVisualize.service.SludgeCompositeService.getRingChartData(com" +
            ".xxxx.cloud.business.dataVisualize.query.SludgeCompositeQuery))  && args(sludgeCompositeQuery)", argNames =
            "sludgeCompositeQuery")
    public void getRingChartDataPointCut(SludgeCompositeQuery sludgeCompositeQuery) {
    }
	
	 @Around("getRingChartDataPointCut(sludgeCompositeQuery)")
    public Object getRingChartData(ProceedingJoinPoint joinPoint, SludgeCompositeQuery sludgeCompositeQuery) throws Throwable {
        long start = System.currentTimeMillis();
        String methodName = joinPoint.getSignature().getName();
        try {
            return joinPoint.proceed();
        } finally {
            long finish = System.currentTimeMillis();
            log.info("===========调用" + methodName + "方法总耗时 :======={} 毫秒 。", (finish - start));
        }
    }
	
    //给具体的方法加上耗时统计(方法上的具体参数用正则表达式表示)(*表示任何参数)
    @Pointcut(value = "execution(* com.xxxx.cloud.business.dataVisualize.service.SludgeCompositeService.getCurveChartData(*))")
    public void getCurveChartDataPointCut() {
    }


    @Around("getCurveChartDataPointCut()")
    public Object getCurveChartData(ProceedingJoinPoint joinPoint) throws Throwable {
        long start = System.currentTimeMillis();
        String methodName = joinPoint.getSignature().getName();
        try {
            return joinPoint.proceed();
        } finally {
            long finish = System.currentTimeMillis();
            log.info("===========调用" + methodName + "方法总耗时 :======={} 毫秒 。", (finish - start));
        }
    }

}

内心OS:虽然这种方式解决了代码污染的问题,但是每次计算一个接口的耗时统计还是要写很多代码,颜()值()这么高的我,怎么能容忍这么low的事情发生呢!!!

不多BB,还是直接上代码!!!

二、注解的方式实现接口耗时统计

1、建立注解TimeStatistics

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface TimeStatistics {
}

2、建立切面类

@Aspect
@Slf4j
@Component
public class TimeAspect {

    @Around("@annotation(TimeStatistics)")
    public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
        long start = System.currentTimeMillis();
        String methodName = joinPoint.getSignature().getName();
        try {
            return joinPoint.proceed();
        } finally {
            long finish = System.currentTimeMillis();
            log.info("===========调用" + methodName + "方法总耗时 :======={} 毫秒 。", (finish - start));
        }
    }

}

3、在需要耗时统计的接口上或方法上添加@TimeStatistics即可

    @Override
    @TimeStatistics
    public RingChartData getRingChartData(SludgeCompositeQuery sludgeCompositeQuery) {
        RingChartData ringChartData = new RingChartData();
        List<WasteTypeDataInfo> wasteTypeDataInfoList = this.getWasteTypeDataInfo(sludgeCompositeQuery);
        List<CarTypeDataInfo> carTypeDataInfoList = this.getCarTypeDataInfo(sludgeCompositeQuery);
        List<DealArtDataInfo> dealArtDataInfoList = this.getDealArtDataInfo(sludgeCompositeQuery);
        RingChartData ringChartDataProcess = this.processRingChartData(wasteTypeDataInfoList,carTypeDataInfoList,dealArtDataInfoList);
        BeanUtils.copyProperties(ringChartDataProcess, ringChartData);
        ringChartData.setWasteTypeDataInfoList(wasteTypeDataInfoList);
        ringChartData.setCarTypeDataInfoList(carTypeDataInfoList);
        ringChartData.setDealArtDataInfoList(dealArtDataInfoList);
        return ringChartData;
    }

大功告成!!!

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值