Spring AOP Spring注解驱动之AOP功能测试

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Spring AOP Spring注解驱动之AOP功能测试

TomDu   2021-04-21 我要评论

前言

Spring的AOP指的是在程序运行期间动态的将某段代码切入到指定方法指定位置进行运行的编程方式【动态代理】。

AOP功能测试

①导入AOP模块

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

②定义逻辑组件和切面类

逻辑组件

在业务逻辑运行的时候将日志进行打印(方法之前、方法运行结束、方法出现异常,xxx)

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

}

切面类

切面类里面的方法需要动态感知MathCalculator.div运行到哪里然后执行;

/**
 * 切面类  必须告诉Spring哪个类是切面类(给切面类上加一个注解:@Aspect)
 * @Aspect: 告诉Spring当前类是一个切面类
 *
 */
@Aspect
public class LogAspects {
  
  //抽取公共的切入点表达式
  //1、本类引用 pointCut()
  //2、其他的切面引用 com.atneusoft.springboot.aop.LogAspects.pointCut()
  @Pointcut("execution(public int com.atneusoft.springboot.aop.MathCalculator.*(..))")
  public void pointCut(){};
  
  //@Before在目标方法之前切入;切入点表达式(指定在哪个方法切入)
  //给切面类的目标方法标注何时何地运行(通知注解@Before\@After\@AfterReturning\@AfterThrowing)  //前置通知(@Before):在目标方法(div)运行之前运行
  @Before("pointCut()")
  public void logStart(JoinPoint joinPoint){
    Object[] args = joinPoint.getArgs();
    System.out.println(""+joinPoint.getSignature().getName()+"运行。。。@Before:参数列表是:{"+Arrays.asList(args)+"}");
  }
    //后置通知(@After):在目标方法(div)运行结束之后运行(无论方法正常结束还是异常结束)
  @After("com.atneusoft.springboot.aop.LogAspects.pointCut()")
  public void logEnd(JoinPoint joinPoint){
    System.out.println(""+joinPoint.getSignature().getName()+"结束。。。@After");
  }
  
  //JoinPoint一定要出现在参数表的第一位  //返回通知(@AfterReturning):在目标方法(div)正常返回之后运行
  @AfterReturning(value="pointCut()",returning="result")
  public void logReturn(JoinPoint joinPoint,Object result){
    System.out.println(""+joinPoint.getSignature().getName()+"正常返回。。。@AfterReturning:运行结果:{"+result+"}");
  }
  //异常通知(@AfterThrowing):在目标方法(div)出现异常以后运行
  @AfterThrowing(value="pointCut()",throwing="exception")
  public void logException(JoinPoint joinPoint,Exception exception){
    System.out.println(""+joinPoint.getSignature().getName()+"异常。。。异常信息:{"+exception+"}");
  }

}

③将切面类和业务逻辑类(目标方法所在类)都加入到容器中,给配置类中加 @EnableAspectJAutoProxy 【开启基于注解的aop模式,与配置文件的以下形式相同

<!-- 开启基于注解版的切面功能 -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>

@EnableAspectJAutoProxy
@Configuration
public class MainConfigOfAOP {
   
  //业务逻辑类加入容器中
  @Bean
  public MathCalculator calculator(){
    return new MathCalculator();
  }

  //切面类加入到容器中
  @Bean
  public LogAspects logAspects(){
    return new LogAspects();
  }
}
@Test
  public void test01(){
    AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfigOfAOP.class);
    
    //1、不要自己创建对象
//    MathCalculator mathCalculator = new MathCalculator();
//    mathCalculator.div(1, 1);
    MathCalculator mathCalculator = applicationContext.getBean(MathCalculator.class);
    
    mathCalculator.div(1, 0);
    
    applicationContext.close();
  }
07:49:45.185 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'mathCalculator'
div运行。。。@Before:参数列表是:{[1, 1]}
MathCalculator...div...
div结束。。。@After
div正常返回。。。@AfterReturning:运行结果:{1}
com.atneusoft.springboot.aop.MathCalculator@5965be2d

总结

三步:

1)、将业务逻辑组件和切面类都加入到容器中;告诉Spring哪个是切面类(@Aspect)

2)、在切面类上的每一个通知方法上标注通知注解,告诉Spring何时何地运行(切入点表达式)

3)、开启基于注解的aop模式;@EnableAspectJAutoProxy

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们