LogAspect.java 1.7 KB
Newer Older
Q
qinyingjie 已提交
1 2
package com.kwan.springbootkwan.aop;

3
import lombok.extern.slf4j.Slf4j;
Q
qinyingjie 已提交
4 5
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
6 7 8 9 10 11
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
Q
qinyingjie 已提交
12 13 14 15 16 17 18 19

/**
 * AOP 实现方法拦截
 *
 * @author : qinyingjie
 * @version : 2.2.0
 * @date : 2022/12/19 16:17
 */
20 21
//@Component
//@Aspect
22
@Slf4j
Q
qinyingjie 已提交
23
public class LogAspect {
24 25 26

    @Pointcut(value = "execution(* com.kwan.springbootkwan.service.*.*(..)) && !execution(* com.kwan.springbootkwan.service.WebSocketServer.*(..))")

Q
qinyingjie 已提交
27 28 29 30 31 32
    public void pcl() {
    }

    @Before(value = "pcl()")
    public void before(JoinPoint point) {
        String name = point.getSignature().getName();
33
        log.info(name + "方法开始执行 ...");
Q
qinyingjie 已提交
34 35 36 37 38
    }

    @After(value = "pcl()")
    public void after(JoinPoint point) {
        String name = point.getSignature().getName();
39
        log.info(name + "方法执行结束 ...");
Q
qinyingjie 已提交
40 41 42 43 44
    }

    @AfterReturning(value = "pcl()", returning = "result")
    public void afterReturning(JoinPoint point, Object result) {
        String name = point.getSignature().getName();
45
        log.info(name + "方法返回值为:" + result);
Q
qinyingjie 已提交
46 47 48 49 50
    }

    @AfterThrowing(value = "pcl()", throwing = "e")
    public void afterThrowing(JoinPoint point, Exception e) {
        String name = point.getSignature().getName();
51
        log.info(name + "方法抛异常了,异常是:" + e.getMessage());
Q
qinyingjie 已提交
52 53 54 55 56 57
    }

    @Around("pcl()")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        return pjp.proceed();
    }
58
}