Spring aop 的简单使用方法

Spring aop一般以下几个方面比较常用:

记录日志、方法运行时间的监控、权限控制和缓存的管理。下面是使用的例子。

一、添加依赖

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aop</artifactId>
    <version>4.3.14.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.8.9</version>
</dependency>

二、添加配置

<aop:aspectj-autoproxy/>

三、创建自己的切面类

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

@Aspect
@Component/*这个注解也是要的,默认不会注入切面类*/
public class MyAspect {
    /**
     * 定义切点
     * "*Controller":表示以Controller结尾的类
     * "add*":表示以add开头的方法
     * 符合以上条件的方法,将成为一个切点
     */
    @Pointcut("execution(* com.good.frame.controller.*Controller.add*(..))")
    public void myPointcut() {
    }

    /**
     * 前置通知
     */
    @Before("myPointcut()")
    public void before(JoinPoint joinPoint) {
        //参数集合
        Object[] args = joinPoint.getArgs();
        Signature signature = joinPoint.getSignature();
        //方法名称
        signature.getDeclaringTypeName();
        System.out.println("前置通知");
    }

    /**
     * 后置通知
     */
    @After("myPointcut()")
    public void after(JoinPoint joinPoint) {
        System.out.println("后置通知");
    }

    /**
     * 环绕通知
     */
    @Around(value = "myPointcut()")
    public Object around(ProceedingJoinPoint joinPoint) {
        System.out.println("环绕通知开始");
        Object[] args = joinPoint.getArgs();
        //修改参数值
        if (args[0] instanceof String) {
            args[0] = "river66";
        }
        if (args[1] instanceof String) {
            args[1] = "66";
        }
        try {
            Object returnValue = joinPoint.proceed(args);
            //使用returnValue,做一些判断和处理
            System.out.println("环绕通知结束");
            return "环绕通知返回的值";
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            return null;
        }
    }

    /**
     * 获取返回值
     */
    @AfterReturning(returning = "returnObject", value = "myPointcut()")
    public void getReturnValue(Object returnObject) {
        System.out.println((String) returnObject);
    }

    /**
     * 定义需要捕获异常的切点
     */
    @Pointcut("execution(* com.good.frame.controller.*Service.add*(..))")
    public void exceptionPointcut() {
    }

    /**
     * 捕获异常
     */
    @AfterThrowing(throwing = "e", pointcut = "exceptionPointcut()")
    public void catchException(Exception e) {
        System.out.println(e.getMessage());
    }

}

打印:

环绕通知开始
前置通知
环绕通知结束
后置通知
环绕通知返回的值

使用切面编程,除了新增处理逻辑,还可以修改参数值,修改返回值都是可以的 ,还有获取返回值、异常的捕获等。

发布了78 篇原创文章 · 获赞 131 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/river66/article/details/102877686