【SpringBoot_ANNOTATIONS】AOP 01 AOP功能测试

本文详细介绍了如何在Spring框架下使用AOP(面向切面编程)进行动态代理,包括前置、后置、返回和异常通知的实现,并通过MathCaculator示例演示了切面类配置和测试过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

AOP 01 AOP功能测试

AOP:动态代理

指在程序运行期间动态的将某段代码切入到指定位置进行运行的编程方式

步骤

  1. 导入AOP模块
  2. 定义业务逻辑类:在业务逻辑运行的时候打印日志(方法前后 异常 …)
  3. 定义日志切面类:切面里的方法需动态感知目标方法运行到哪里然后执行
  4. 给切面类的目标方法标注何时何地运行(通知注解)
  5. 将切面类和业务逻辑类(目标方法所在类)都加到容器中
  6. 必须告诉Spring哪个是切面类(给切面类加上@Aspect注解)
  7. 给配置类中的@EnableAspectJAutoProxy开启基于注解的AOP模式

    Spring中很多的@EnableXXX表示用来开启某一功能

通知注解

作用注解备注
前置通知@Before
后置通知@After无论正常结束还是异常结束都会调用
返回通知@AfterReturning正常返回后运行
异常通知@AfterThrowing
环绕通知@Around动态代理 手动推进目标方法运行joinPoint.proceed() 相当于一个最底层的通知

代码测试

业务逻辑类
MathCaculator.java

/**
 * @Classname MathCaculator
 * @Description AOP测试_业务逻辑类
 */

public class MathCaculator {
    public int div(int i,int j){
        System.out.println("MathCaculator 运行 div() ...");
        return i/j;
    }
}

切面类


/**
 * @Classname LogAspects
 * @Description 切面类
 * @Date 2021/8/5 10:34
 * @Created by LXLDEMO
 */
@Aspect
public class LogAspects {
    /**
     * 抽取公共的切入点表达式
     * 指向目标方法
     * 1. 本类引用
     * 2. 其他的切面引用
     */
    @Pointcut("execution(public int com.example.annotations.bean.MathCaculator.div(int ,int ))")
    public void pointCut(){

    }

    @Before("pointCut()")
    public void logStart(JoinPoint joinPoint){
        Object[] args = joinPoint.getArgs();
        System.out.println("运行@Before");
        System.out.println("方法名字 " + joinPoint.getSignature().getName());
        System.out.println("方法参数 " + Arrays.toString(args));
    }

    /**
     * 注解中参数的这种写法属于其他切面的调用方式
     * 全包名
     * @param joinPoint
     */
    @After("com.example.annotations.aop.LogAspects.pointCut()")
    public void LogEnd(JoinPoint joinPoint){
        System.out.println("运行@After " + joinPoint.getSignature().getName());
        System.out.println(joinPoint.getSignature().getName() + "运行结束");
    }

    /**
     * 当有多个参数的时候 JoinPoint必须作为第一个参数
     * @param joinPoint
     * @param result
     */
    @AfterReturning(value = "pointCut()",returning = "result")
    public void logReturning(JoinPoint joinPoint,Object result){
        System.out.println("运行@AfterReturning");
        System.out.println(joinPoint.getSignature().getName()+"正常返回" + result);
    }

    /**
     * 当有多个参数的时候 JoinPoint必须作为第一个参数
     * @param joinPoint
     * @param exception
     */
    @AfterThrowing(value = "pointCut()",throwing = "exception")
    public void logException(JoinPoint joinPoint,Exception exception){
	    System.out.println("运行@AfterThrowing");
        System.out.println("" + joinPoint.getSignature().getName() +" === 异常信息:"+exception);
    }
}

配置类

/**
 * @Classname MainConfig4AOP
 * @Description 配置类
 * @Date 2021/8/5 10:31
 * @Created by LXLDEMO
 */
@EnableAspectJAutoProxy
@Configuration
public class MainConfig4AOP {
    @Bean
    public MathCaculator caculator(){
        System.out.println("准备注入 MathCaculator ...");
        return new MathCaculator();
    }

    @Bean
    public LogAspects logAspects(){
        System.out.println("准备注入 LogAspects ...");
        return new LogAspects();
    }
}

测试

    @Test
    void Aoptest(){
        //配置类测试
        System.out.println("准备创建容器");
        // 注意要使用无参构造器
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfig4AOP.class);
        System.out.println("容器创建完毕");

        // 获取bean
        MathCaculator mathCaculator = applicationContext.getBean(MathCaculator.class);
        mathCaculator.div(1,1);

        // 关闭容器
        applicationContext.close();
        System.out.println("容器已关闭");
    }

测试结果
在这里插入图片描述

将参数换成(1,0)
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值