【spring注解驱动开发】- AOP - AOP功能测试

本博客demo源码地址
https://github.com/suchahaerkang/spring-annotation.git

1 AOP作用

AOP是spring中最重要的特性之一,底层技术用到了动态代理
AOP作用:在运行的程序中,将一段代码切入到指定方法指定位置上去运行的编程方法

2 AOP功能测试

要实现AOP的功能,首先要引入spring-aspects依赖包

 <dependency>
  	<groupId>org.springframework</groupId>
    <artifactId>spring-aspects</artifactId>
    <version>4.3.12.RELEASE</version>
</dependency>

现在有个场景,我需要一个计算器的组件,里面有加减乘除的方法。我们现在需要在每个方法执行之前,执行之后,返回之后和抛出异常时都打印日志。如果按照每个方法都去写一遍这样逻辑,这是一个非常笨重的工作,做起来毫无意义。下面我们就用AOP的原理来实现这个需求。
首先写个计算器的组件MathCalculate

/**
 * @description:
 * @author: sukang
 * @date: 2020-03-09 8:52
 */
public class MathCalculate {

    /**
     * @description: 计算除法的方法
     * @param a
     * @param b
     * @return: int
     * @author: sukang
     * @date: 2020/3/9 8:52
     */
    public int div(int a, int b){
        System.out.println("div()方法执行...");
        return a/b;
    }
}

然后写个切面类

**
 * @description: 日志切面
 * @author: sukang
 * @date: 2020-03-09 8:53
 */
//这个注解必须要写,不然不知道这个组件是个切面组件
@Aspect
public class LogAspects {

    //切入点,标志切入的目标方法为MathCalculate组件里面的所有方法
    @Pointcut("execution(public int com.wolfx.aop.MathCalculate.*(..))")
    public void pointCut(){
    }

    //前置通知:在指定方法执行之前进行执行这个方法,JoinPoint组件可以获取当前目标函数的一些信息
    @Before("pointCut()")
    public void logStart(JoinPoint joinPoint){
        //获取参数列表
        Object[] objects = joinPoint.getArgs();
        System.out.println("方法"+ joinPoint.getSignature().getName() + "执行前执行切面的前置通知@Before打印请求参数为:"+ Arrays.toString(objects));
    }

    //后置通知:在指定方法执行之后执行这个方法
    @After("pointCut()")
    public void logEnd(JoinPoint joinPoint){
        System.out.println("方法"+ joinPoint.getSignature().getName() + "执行后执行切面的后置通知");
    }

    /*
     * 返回通知:是在目标方法返回的时候进行执行,logReturn()方法的表示结果的参数值result
     * 要和注解@AfterReturning中returning的值一样,不然接受不了。另外JoinPoint这个组件作为
     * logReturn方法的参数必须要放在第一个参数,否则会报错
     */
    @AfterReturning(pointcut = "pointCut()", returning = "result")
    public void logReturn(JoinPoint joinPoint, Object result){
        System.out.println("方法"+ joinPoint.getSignature().getName() + "返回的时候执行切面的返回通知返回的结果为:" + result);
    }

    /*
     * 异常通知:是在目标方法抛出异常的时候进行执行,logException()方法的表示异常的参数值exception
     * 要和注解@AfterThrowing中exception的值一样,不然接受不了。另外JoinPoint这个组件作为
     * logException方法的参数必须要放在第一个参数,否则会报错
     */
    @AfterThrowing(pointcut = "pointCut()", throwing = "exception")
    public void logException(JoinPoint joinPoint, Exception exception){
        System.out.println("方法"+ joinPoint.getSignature().getName() + "抛出异常的时候执行切面的异常通知抛出异常信息为:" + exception);
    }

}

写个配置类,将计算器组件MathCalculate组件和LogAspects组件注册到容器中去,并且开启切面自动代理注解@EnableAspectJAutoProxy

/**
 * @description:
 * @author: sukang
 * @date: 2020-03-09 8:50
 */
@EnableAspectJAutoProxy
@Configuration
public class MainConfigOfAop {

    @Bean
    public MathCalculate mathCalculate(){
        return new MathCalculate();
    }

    @Bean
    public LogAspects logAspects(){
        return new LogAspects();
    }
}

写个测试用例

//测试AOP功能
@Test
public void test01(){
    //创建容器
    AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfigOfAop.class);
    //从容器中获取MathCalculate的对象
    MathCalculate mathCalculate = (MathCalculate) applicationContext.getBean("mathCalculate");
    //调用mathCalculate的div方法
    mathCalculate.div(4,2);
}

运行结果
在这里插入图片描述
如果发生异常,写个测试用例

 //测试AOP功能
 @Test
 public void test01(){
     //创建容器
     AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfigOfAop.class);
     //从容器中获取MathCalculate的对象
     MathCalculate mathCalculate = (MathCalculate) applicationContext.getBean("mathCalculate");
     //调用mathCalculate的div方法
     mathCalculate.div(4,0);
 }

运行结果
在这里插入图片描述

3 总结

使用spring注解驱动的切面编程必须一下几步:
1) 切面组件和目标组件必须要注册到容器中去
2) 切面组件必须用注解@Aspects标注,不然不知道这个组件为切面类
3) 配置类上面必须用@EnableAspectJAutoProxy开启aop的注解驱动,不然的话不会生效
4)调用目标方法时,必须要从容器中中获取对象,自己用new创建的对象调用目标方法,也没有切面通知的效果

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值