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());
    }

}

打印:

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

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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值