0
点赞
收藏
分享

微信扫一扫

Spring切面的简单使用

千白莫 2024-02-03 阅读 12

1、引入依赖

<!-- 偷个懒,就当你们是SpringBoot项目吧[滑稽] -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-aop</artifactId>
    <version>2.7.8</version>
</dependency>


2、代码示例

package com.example;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
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.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Component
@Aspect // 标注当前类为切面
@Order(1) // 定义切面作用的优先级,值越小优先级越高,默认值为int的最大值
public class TestAspect {

    /**
     * 切点
     * 第一个 * 代表任意的访问修饰符和返回值类型
     * 第二个 * 代表任意类
     * 第三个 * 代表类中任意方法
     * .. 代表任意的参数列表
     */
    @Pointcut(value = "execution(* com.example..service..*.*(..))")
    public void pointcut() {
    }

    /**
     * @Before 将方法指定为前置通知
     * 前置通知:作用于方法执行之前
     */
    @Before(value = "pointcut()")
    public void beforeMethod(JoinPoint joinPoint) {
        // 获取方法的参数
        Object[] args = joinPoint.getArgs();
        // 获取方法名
        String methodName = joinPoint.getSignature().getName();
        System.out.println("method:" + methodName + ",arguments:" + Arrays.toString(args));
    }

    /**
     * @After 将方法标注为后置通知
     * 后置通知:作用于方法的finally语句块,即不管有没有异常都会执行
     */
    @After(value = "pointcut()")
    public void afterMethod() {
        System.out.println("后置通知");
    }

    /**
     * @AfterReturning 将方法标注为返回通知
     * 返回通知:作用于方法执行之后
     * 可通过注解中的 returning 属性设置接收方法返回值的变量名,如 returning = "result"
     * 若要在方法中使用,则需保证 returning 属性的值与方法中变量名称一致(例 returning="result" ==>  Object result)
     */
    @AfterReturning(value = "pointcut()", returning = "result")
    public void afterReturningMethod(JoinPoint joinPoint, Object result) {
        String methodName = joinPoint.getSignature().getName();
        System.out.println("method:" + methodName + ",result:" + result);
    }

    /**
     * @AfterThrowing 将方法标注为异常通知
     * 异常通知:作用于方法抛出异常时
     * 可通过 throwing 设置接收方法返回的异常信息
     * 在参数列表中通过具体的异常类型,来对指定的异常信息进行操作(throwing="ex" ==> ArithmeticException ex)
     */
    @AfterThrowing(value = "pointcut()", throwing = "ex")
    public void afterThrowingMethod(ArithmeticException ex) {
        System.out.println("有异常了,messages:" + ex);
    }

    /**
     * 环绕通知
     *
     * @param joinPoint joinPoint
     * @return Object
     */
    @Around(value = "pointcut()")
    public Object aroundMethod(ProceedingJoinPoint joinPoint) {
        try {
            // 前置通知
            System.out.println("前置通知");
            // 执行方法
            Object result = joinPoint.proceed();
            // 返回通知
            System.out.println("返回通知");
            return result;
        } catch (Throwable e) {
            // 异常通知
            System.out.println("异常通知");
            e.printStackTrace();
        } finally {
            // 后置通知
            System.out.println("后置通知");
        }
        return null;
    }


}


ps:切面配合自定义注解能实现很多有趣又实用的功能哦~~,不过我就不写咯~ ╮(๑•́ ₃•̀๑)╭


Spring切面以及基本注解使用_Aspect

举报

相关推荐

0 条评论