AOP拦截参数、反回值、执行耗时

2020-12-31 16:27:09 浏览数 (1)

代码语言:javascript复制
import com.google.common.base.Stopwatch;
import com.google.common.collect.Lists;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Component
@Aspect
public class ControllerAspect {
    public static final Logger logger = LoggerFactory.getLogger(ControllerAspect.class);

    @Around("execution(public  com.lh.hous.config.Result  com.lh.hous.controller..*.*(..))")
    public Object handleControllerMethod(ProceedingJoinPoint joinPoint) throws Throwable {
        Stopwatch stopwatch = Stopwatch.createStarted();
        Result result;
        logger.info(
                new StringBuilder("执行Controller开始:")
                        .append(joinPoint.getSignature())
                        .append(";  参数:")
                        .append(Lists.newArrayList(joinPoint.getArgs()).toString())
                        .toString());
        result = (Result) joinPoint.proceed(joinPoint.getArgs());
        logger.info(
                new StringBuilder("执行Controller结束:")
                        .append(joinPoint.getSignature())
                        .append(";  返回值:")
                        .append(result.toString()).toString());
        logger.info(
                new StringBuilder("执行时间:")
                        .append(stopwatch.stop().elapsed(TimeUnit.MILLISECONDS))
                        .append("(毫秒)。").toString());
        return result;
    }


}
aop

0 人点赞