Spring AOP切面学习

前置知识

@Aspect :表明是一个切面类

@Before :前置通知,在方法执行之前执行

@After :后置通知,在方法执行之后执行,不管是否有异常都会执行

@AfterRuturning :返回通知,在方法返回结果之后执行,有异常时获取不到返回值

@AfterThrowing :异常通知,在方法抛出异常之后执行

@Around :环绕通知,围绕着方法执行,包含上面四种通知

@Pointcut :切入点,方法或注解

实战

自定义注解TestMethod

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface TestMethod {
    String value() default "testMethod";
}

自定义注解TestValue

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.PARAMETER})
public @interface TestVale {
    String value() default "testValue";
}

切面

/**
 * @author chenjun
 * @Description
 * 执行顺序:
 *    环绕通知 --> 前置通知 --> 连接点 --> 后置返回通知/异常通知 --> 后置通知
 * @create 2022-09-15 15:57
 */
@Aspect
@Component
@Slf4j
public class TestAspect {

    @Pointcut("@annotation(com.cj.springboot.common.annotation.TestMethod)")
    private void cutMethod() {
    }

    @Before("cutMethod()")
    public void before(JoinPoint joinPoint) {
        log.info("前置方法执行...");
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        //方法
        Method method = methodSignature.getMethod();
        //方法名
        String methodName = method.getName();
        //参数注解
        Annotation[][] parameterAnnotations = method.getParameterAnnotations();
        for (Annotation[] parameterAnnotation : parameterAnnotations) {
            for (Annotation annotation : parameterAnnotation) {
                //方法参数上包含该注解
                if (annotation instanceof TestVale) {
                    log.info("方法参数上匹配到TestValue注解");
                }
            }
        }
        Object[] args = joinPoint.getArgs();
        Annotation[] annotations = args.getClass().getAnnotations();
        for (Annotation annotation : annotations) {
            //方法参数内部包含该注解
            if (annotation instanceof TestVale) {
                log.info("方法参数内部匹配到TestValue注解");
            }
        }
        if (args.length > 0) {
            Class<?> aClass = args[0].getClass();
            Field[] declaredFields = aClass.getDeclaredFields();
            for (Field declaredField : declaredFields) {
                if (declaredField.isAnnotationPresent(TestVale.class)) {
                    log.info("方法参数内部的字段匹配到TestValue注解");
                    declaredField.setAccessible(true);
                }
            }
        }
    }

    /**
     * 不管是否有异常都会执行
     * @param joinPoint
     */
    @After("cutMethod()")
    public void after(JoinPoint joinPoint) {
        log.info("后置方法执行...");
    }

    /**
     * 发生异常时获取不到连接点的返回值
     * @param point
     * @param result
     */
    @AfterReturning(value = "cutMethod()", returning = "result")
    public void afterReturning(JoinPoint point, Object result) {
        log.info("后置返回通知执行...:{}", result);
    }

    /**
     * 异常通知方法只在连接点方法出现异常后才会执行,否则不执行
     * @param point
     * @param ex
     */
    @AfterThrowing(value = "cutMethod()", throwing = "ex")
    public void afterThrowing(JoinPoint point, Exception ex) {
        log.info("后置异常通知执行...:{}", ex);
    }

    /**
     * 环绕通知方法可以包含上面四种通知方法
     * @param proceedingJoinPoint
     * @return
     */
    @Around(value = "cutMethod()")
    public Object around(ProceedingJoinPoint proceedingJoinPoint) {
        log.info("环绕通知执行...");
        try {
            //执行目标方法
            Object proceed = proceedingJoinPoint.proceed();
            return proceed;
        } catch (Throwable throwable) {
            log.info("后置异常通知执行...:{}", throwable.getMessage());
        }
        return null;
    }
}

实体类

@Data
@TableName("user")
public class User {
    @TableId(type = IdType.AUTO)
    private Integer id;
    @TableField(value = "name")
    @TestVale
    private String name;
}

测试类

@RestController
@RequestMapping("/test")
public class TestAspectController {

    @TestMethod
    @GetMapping("test1")
    public int test1(@TestVale User user) {
        System.out.println("test1");
        int a = 1 / 0;
        return 1;
    }
}

测试

访问localhost:7777/test/test1

结果

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring AOP(Aspect-Oriented Programming)是Spring框架提供的一种面向切面编程的现方式。它允许开发人员将横切关注点(如日志记录、事务管理等)与核心业务逻辑进行分离,从而提高代码的可重用性和可维护性。 在Spring AOP中,通过定义切面(Aspect)来描述横切关注点,然后将切面与目标对象进行关联。切面可以通过配置文件或注解的方式进行声明。在运行时,Spring AOP会根据定义的切面规则自动将切面逻辑织入到目标对象的方法中。 切面由切点(Pointcut)和通知(Advice)组成。切点定义了在哪些方法上应用切面逻辑,通知定义了切面逻辑的具体实现。通知可以在目标方法执行前、执行后、抛出异常时等不同的时机触发。 常见的通知类型包括: - 前置通知(Before Advice):在目标方法执行前执行; - 后置通知(After Advice):在目标方法执行后执行,无论是否发生异常; - 返回通知(After Returning Advice):在目标方法执行后执行,仅在目标方法成功返回时触发; - 异常通知(After Throwing Advice):在目标方法抛出异常后执行; - 环绕通知(Around Advice):在目标方法执行前后都执行,可以控制目标方法的执行流程。 Spring AOP可以应用于各个层面,如业务逻辑、数据访问、事务管理等,通过将横切关注点与核心业务逻辑解耦,可以提高代码的模块化和可维护性。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值