Aop学习笔记-- 基于xml和Aspect 注解方式实现AOP

AOP 术语

  1. 通知: 就是增强的方法(如记录日志,事务等)
  2. 连接点:就是指Spring允许你增强的地方都可以称为连接点,Spring支持方法连接点,所以方法的前后都可以是连接点。
  3. 切入点: 就是在连击点的基础上,来定义切入点。不想把通知运用到所有的方法上,就可以定义切入点。
  4. 切面就是通知+切入点。

通知类型:
1> @Before: 前置通知,在方法执行前执行
2> @After: 后置通知,在方法执行后执行
3>@AfterReturning: 返回通知,在方法返回结果后执行。
4>@AfterThrowing:异常通知,在方法抛出异常后执行
5>@Around: 环绕通知,围绕着方法执行。

基于注解的AOP实现
  1. 定义一个切面:
    定义一个类,用 @Component 将该类交给容器管理
    使用@Aspect 注解来申明该类是 一个切面

  2. 在 通知方法 上申明通知类型,如 @Before

  3. 通过切入点表达式定义切点
    @Before(value = “execution(*
    com.test.spring.aop.dao.ArithmeticCalculator.*(…))”)

  4. 启用Aspect 注解,让Spring 自动创建代理对象
    <aop:aspectj-autoproxy />

切入点表达式详解:
  1. 只有执行ArithmeticCalculator中的add方法时才会执行通知

execution(public int cn.xiechengxu.spring.aop.dao.ArithmeticCalculator.add(int,
int))*

  1. 只有执行ArithmeticCalculator中的所有方法时才会执行通知

    execution(public int cn.xiechengxu.spring.aop.dao.ArithmeticCalculator.*(int, int))

  2. 只有执行ArithmeticCalculator中的所有方法时才会执行通知而且不考虑权限修饰符和返回值类型

    execution(*
    cn.xiechengxu.spring.aop.dao.ArithmeticCalculator.*(int, int))

  3. 只有执行ArithmeticCalculator中的所有方法时才会执行通知而且不考虑权限修饰符和返回值类型以及参数的类型和个数

execution(*
com.atguigu.spring.aop.dao.ArithmeticCalculator.*(…))

  1. 执行所有类中的所有方法时都会执行通知而且不考虑权限修饰符和返回值类型以及参数的类型和个数

execution(* .(…))

在定义通知方法时,可以传入JoinPoint 参数,

// 获取方法名
String methodName = jp.getSignature().getName();
// 获取参数
Object[] args = jp.getArgs();

在返回类型的通知上,可以指定返回值。

@AfterReturning(pointcut = “execution(*
cn.xiechengxu.spring.aop.dao.ArithmeticCalculator.*(…))”,
returning = “result”)

定义环绕通知时,传入ProceedingJoinPoint 类型的参数
可以把切入点表达式单独提出来:

@Pointcut(value=“execution(*
cn.xiechengxu.spring.aop.dao.ArithmeticCalculator.*(…))”)
public void pointCut(){
};

@Around(“pointCut()”)
public Object aroundLogging(ProceedingJoinPoint pjp) { }

//获取方法名
String methodName = pjp.getSignature().getName();
//获取参数
Object[] args = pjp.getArgs();

执行方法:
result = pjp.proceed();

切面的优先级

通过@Order 注解指定切面优先级,value 值越小,优先级越高。 默认值是Integer 的最大值。

@Order(100)

基于Xml 实现AOP
  1. 在xml中配置切面bean
  2. 使用 <aop: config> 标签
  3. 在标签中配置切点
  4. 配置切面


<!-- 配置切面bean -->
<bean id="loggingAspect" 
class="cn.xiechengxu.spring.xml.LoggingAspect"></bean>


<!-- 配置AOP -->
<aop:config>     
<!-- 配置切入点表达式 -->    
<aop:pointcut expression="execution(* 
com.atguigu.spring.xml.ArithmeticCalculator.*(..))"           id="pointCut"/>    

<!-- 配置切面 -->      
<aop:aspect ref="loggingAspect">     
<!-- 配置前置通知 -->        
<aop:before method="beforeLogging" 
pointcut-ref="pointCut"/>         
<!-- 配置后置通知 -->        
<aop:after method="AfterLogging" 
pointcut-ref="pointCut"/>      
<!-- 配置返回通知 -->        
<aop:after-returning method="returnLogging" 
pointcut-ref="pointCut" returning="result"/>        
<!-- 配置异常通知 -->        
<aop:after-throwing method="throwLogging" 
pointcut-ref="pointCut" throwing="e"/>         
<!-- 配置环绕通知 -->     
<aop:around method="aroundLogging" 
pointcut-ref="pointCut"/>    
</aop:aspect>
</aop:config>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值