Solo  当前访客:0 开始使用

Nick1407 的个人博客

公众号:JavaCase

关于AOP


1、一些概念
image.png

2、使用
创建切面:

@Aspect
@Component
@Slf4j
public class PerformanceAspect {
    @Around("repositoryOps()")
    public Object logPerformance(ProceedingJoinPoint pjp) throws Throwable {
        long startTime = System.currentTimeMillis();
        String name = "-";
        String result = "Y";
        try {
            name = pjp.getSignature().toShortString();
            return pjp.proceed();
        } catch (Throwable t) {
            result = "N";
            throw t;
        } finally {
            long endTime = System.currentTimeMillis();
            log.info("{};{};{}ms", name, result, endTime - startTime);
        }
    }

    @Pointcut("execution(* com.aspect.aspect.repository..*(..))")
    private void repositoryOps() {
    }
}

当执行com.aspect.aspect.repository包下的方法时会预先执行切面的logPerformance方法

3、demo
aspect.zip


0 0