package com.anno;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Component("myAs")
@Aspect
public class MyAspect {
    @Before("execution(void com.anno.TargetImpl.save2())")
    public void myBefore() {
        System.out.println("annotation. aspect before...");
    }
    // 最终增强
    @After("MyAspect.myPoint()")
    public void after() {
        System.out.println("annotation.aspect after...");
    }

    /**
     * 环绕方法
     * @param proceedingJoinPoint 正在执行的连接点 -》切点
     */
//    @Around("execution(void com.anno.TargetImpl.save2())")
    @Around("myPoint()")
    public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        System.out.println("环绕前增强...");
        // 切点方法
        Object proceed = proceedingJoinPoint.proceed();
        System.out.println("环绕后增强...");
        return proceed;
    }
    // 抽取切点。
    @Pointcut("execution(void com.anno.TargetImpl.save2())")
    public void myPoint() {

    }
    /**
     * 异常增强（在抛出异常时执行）
     */
    public void afterThrowing() {
        System.out.println("异常抛出增强...");

    }
}
