AOP,AspectJ 实现统一接口耗时统计

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.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

https://url.cn/50jfk0F

/**

  • @author dy

  • @since 2016-08-04 & JDK 1.8.0_91
    */
    @Component
    @Aspect
    public class ExpendAdvice {

    public Logger logger = LoggerFactory.getLogger(this.getClass());

    @Pointcut(“execution(* com.trc.funds..service.impl..do*(…)) || execution(* com.trc.funds..service.impl..select*(…)))”)
    private void aspectjMethod() {
    }

// @Before(“aspectjMethod()”)
// public void beforeAdvice(JoinPoint joinPoint) {
// System.out.println("-----beforeAdvice().invoke-----");
// System.out.println(System.currentTimeMillis());
// System.out.println("-----End of beforeAdvice()------");
// }
//
// @After(“aspectjMethod()”)
// public void afterAdvice(JoinPoint joinPoint) {
// System.out.println("-----afterAdvice().invoke-----");
// System.out.println(System.currentTimeMillis());
// System.out.println("-----End of afterAdvice()------");
// }

@Around("aspectjMethod()")
public Object aroundAdvice(ProceedingJoinPoint joinPoint) throws Throwable {
    try {
        String className = joinPoint.getThis().toString();
        String methodName = joinPoint.getSignature().getName();
        long st = System.currentTimeMillis();
        Object result = joinPoint.proceed();
        logger.info("[***类名:" + className + " ,方法名:" + methodName + " ,共计消耗:" + (System.currentTimeMillis() - st) + " ms ***]");
        return result;
    } catch (Throwable throwable) {
        logger.error("环绕增强发生异常!请联系超级管理员-dy");
        logger.error(throwable.getMessage(), throwable.getStackTrace());
        throw throwable;
    }
}

// @AfterReturning(value = “aspectjMethod()”, returning = “retVal”)
// public void afterReturningAdvice(JoinPoint joinPoint, String retVal) {
// System.out.println("-----afterReturningAdvice().invoke-----");
// System.out.println(“Return Value: " + retVal);
// System.out.println(” 此处可以对返回值做进一步处理");
// System.out.println(" 可通过joinPoint来获取所需要的内容");
// System.out.println("-----End of afterReturningAdvice()------");
// }
//
// @AfterThrowing(value = “aspectjMethod()”, throwing = “ex”)
// public void afterThrowingAdvice(JoinPoint joinPoint, Exception ex) {
// System.out.println("-----afterThrowingAdvice().invoke-----");
// System.out.println(" 错误信息:"+ex.getMessage());
// System.out.println(" 此处意在执行核心业务逻辑出错时,捕获异常,并可做一些日志记录操作等等");
// System.out.println(" 可通过joinPoint来获取所需要的内容");
// System.out.println("-----End of afterThrowingAdvice()------");
// }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值