使用Spring Aop如何配置AspectJ注解

  介绍

这篇文章将为大家详细讲解有关使用Spring Aop如何配置AspectJ注解,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

采用的jdk代理,接口和实现类代码请参考上篇博文。主要是将方面类分享一下:

包com.tgb.aop;
  
  进口org.aspectj.lang.JoinPoint;
  进口org.aspectj.lang.ProceedingJoinPoint;
  进口org.aspectj.lang.annotation.After;
  进口org.aspectj.lang.annotation.AfterReturning;
  进口org.aspectj.lang.annotation.AfterThrowing;
  进口org.aspectj.lang.annotation.Around;
  进口org.aspectj.lang.annotation.Aspect;
  进口org.aspectj.lang.annotation.Before;
  进口org.aspectj.lang.annotation.DeclareParents;
  进口org.aspectj.lang.annotation.Pointcut;/* *
  *测试之后,,,投掷,返回的建议。
  * @author管理
  *
  */@Aspect
  公开课AspceJAdvice {/* *
  *切入点
  *定义切入点,切入点的名称为aspectjMethod(),此方法没有返回值和参数
  *该方法就是一个标识,不进行调用
  */@Pointcut(“执行(*找到* (. .))“)
  私人空间aspectjMethod () {};/* *
  *之前
  *在核心业务执行前执行,不能阻止核心业务的调用。
  * @param连接点
  */@Before (“aspectjMethod ()“)
  公共空beforeAdvice(连接点的连接点){
  System.out.println (“——-beforeAdvice () .invoke——产生绯闻;);
  System.out.println (“;此处意在执行核心业务逻辑前,做一些安全性的判断等等“);
  System.out.println (“;可通过的连接点来获取所需要的内容“);
  System.out.println(“——端beforeAdvice () - - - - - -“);
  }/* *
  *后
  *核心业务逻辑退出后(包括正常执行结束和异常退出),执行此建议
  * @param连接点
  */@After(值=https://www.yisu.com/zixun/盿spectjMethod ())
  公共空afterAdvice(连接点的连接点){
  System.out.println (“——-afterAdvice () .invoke - - - - - -”);
  system . out。println(“此处意在执行核心业务逻辑之后,做一些日志记录操作等”等);
  system . out。println(“可通过的连接点来获取所需要的内容”);
  system . out。println (“afterAdvice——端()- - - - - -”);
  }/* *
  *在
  *手动控制调用核心业务逻辑,以及调用前和调用后的处理,
  *
  *注意:当核心业务抛异常后,立即退出,转向AfterAdvice
  *执行完AfterAdvice,再转到ThrowingAdvice
  * @param pjp
  * @return
  * @throws Throwable
  */@Around (value=" aspectjMethod ()”)
  公共对象aroundAdvice (ProceedingJoinPoint pjp)抛出Throwable {
  System.out.println (“——-aroundAdvice () .invoke - - - - - -”);
  system . out。println(“此处可以做类似于之前建议的事情”);//调用核心逻辑
  对象retVal=pjp.proceed ();
  system . out。println(“此处可以做类似于后建议的事情”);
  system . out。println (“aroundAdvice——端()- - - - - -”);
  返回retVal;
  }/* *
  * AfterReturning
  *核心业务逻辑调用正常退出后,不管是否有返回值,正常退出后,均执行此建议
  * @param连接点
  */@AfterReturning (value=" aspectjMethod (),返回=" retVal”)
  公共空afterReturningAdvice(连接点的连接点,字符串retVal) {
  System.out.println (“——-afterReturningAdvice () .invoke - - - - - -”);
  system . out。println(“返回值:“+ retVal);
  system . out。println(“此处可以对返回值做进一步处理”);
  system . out。println(“可通过的连接点来获取所需要的内容”);
  system . out。println (“afterReturningAdvice——端()- - - - - -”);
  }/* *
  *核心业务逻辑调用异常退出后,执行此建议,处理错误信息
  *
  *注意:执行顺序在Around通知之后
  * @param连接点
  * @param交货
  */@AfterThrowing (value=" aspectjMethod (),=扒芭选?
  公共空间afterThrowingAdvice(连接点的连接点,异常的前女友){
  System.out.println (“——-afterThrowingAdvice () .invoke - - - - - -”);
  system . out。println(“错误信息:“+ ex.getMessage ());
  system . out。println(“此处意在执行核心业务逻辑出错时,捕获异常,并可做一些日志记录操作等”等);
  system . out。println(“可通过的连接点来获取所需要的内容”);
  system . out。println (“afterThrowingAdvice——端()- - - - - -”);
  }
  }
  
  

application-config。xml中,只需要配置业务逻辑bean bean和方面,并启用方面注解即可:

& lt;及# 63;xml version=?.0”;编码=癠TF-8", # 63;比;      http://www.springframework.org/schema/beans" & lt;豆类xmlns=?   xmlns: xsi=癶ttp://www.w3.org/2001/XMLSchema-instance"   xmlns: aop=癶ttp://www.springframework.org/schema/aop"   xmlns: tx=癶ttp://www.springframework.org/schema/tx"   xsi: schemaLocation=癶ttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd   http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd   ,http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"的在      & lt; !——启用AspectJ对注释的支持——比;   & lt; aop: aspectj-autoproxy/比;      & lt; bean id=皍serManager"类=癱om.tgb.aop.UserManagerImpl"/比;   & lt; bean id=癮spcejHandler"类=癱om.tgb.aop.AspceJAdvice"/比;      & lt;/beans>null   null   null

使用Spring Aop如何配置AspectJ注解