Advice通知

  Advice(通知)定义在连接点做什么,为切面增强织入接口。在Spring AOP中,它主要描述Spring AOP

围绕方法调用而注入的切面行为。Advice是AOP联盟定义的一个接口,具体的接口定义在org.aopalliance.aop.

Advice中。在Spring AOP的实现中,使用了这个统一接口,并通过这个接口,为AOP切面增强的织入功能做了

更多的细化和扩展,比如提供了更具体的通知类型,如BeforeAdvice、AfterAdvice、ThrowsAdvice等。作为

Spring AOP定义的接口类,具体的切面增强可以通过这些接口集成到AOP框架中去发挥作用。我们先从

BeforeAdvice开始。如下是Advice的类层级关系

  在BeforeAdvice的继承关系中,定义了为待增强的目标方法设置的前置增强接口MethodBeforeAdvice,

如下

 1 /**
 2  * Advice invoked before a method is invoked. Such advices cannot
 3  * prevent the method call proceeding, unless they throw a Throwable.
 4  *
 5  * @see AfterReturningAdvice
 6  * @see ThrowsAdvice
 7  *
 8  * @author Rod Johnson
 9  */
10 public interface MethodBeforeAdvice extends BeforeAdvice {
11
12     /**
13      * Callback before a given method is invoked.
14      * @param method method being invoked
15      * @param args arguments to the method
16      * @param target target of the method invocation. May be {@code null}.
17      * @throws Throwable if this object wishes to abort the call.
18      * Any exception thrown will be returned to the caller if it's
19      * allowed by the method signature. Otherwise the exception
20      * will be wrapped as a runtime exception.
21      */
22     void before(Method method, Object[] args, Object target) throws Throwable;
23
24 }

为了说明BeforeAdvice子类MethodBeforeAdvice的具体使用,来看一下子类CountingBeforeAdvice

1 public class CountingBeforeAdvice extends MethodCounter implements MethodBeforeAdvice {
2
3     @Override
4     public void before(Method m, Object[] args, Object target) throws Throwable {
5         count(m);
6     }
7
8 }

  

  这里调用了count方法,count方法在CountingBeforeAdvice的基类MethodCounter中实现

 1 public class MethodCounter implements Serializable {
 2
 3     /** Method name --> count, does not understand overloading */
 4     private HashMap<String, Integer> map = new HashMap<String, Integer>();
 5
 6     private int allCount;
 7
 8     protected void count(Method m) {
 9         count(m.getName());
10     }
11
12     protected void count(String methodName) {
13         Integer i = map.get(methodName);
14         i = (i != null) ? new Integer(i.intValue() + 1) : new Integer(1);
15         map.put(methodName, i);
16         ++allCount;
17     }
18 }

这个切面增强完成的统计实现并不复杂,它在对象中维护一个哈希表,用来存储统计数据

AfterAdvice

  在Advice的实现体系中,Spring还提供了AfterAdvice这种通知类型,以AfterReturningAdvice通知的

实现为例,分析一下AfterAdvice通知类型的实现原理

 1 public interface AfterReturningAdvice extends AfterAdvice {
 2
 3     /**
 4      * Callback after a given method successfully returned.
 5      * @param returnValue the value returned by the method, if any
 6      * @param method method being invoked
 7      * @param args arguments to the method
 8      * @param target target of the method invocation. May be {@code null}.
 9      * @throws Throwable if this object wishes to abort the call.
10      * Any exception thrown will be returned to the caller if it's
11      * allowed by the method signature. Otherwise the exception
12      * will be wrapped as a runtime exception.
13      */
14     void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable;
15
16 }

  afterReturning方法也是一个回调函数,AOP应用需要在这个接口实现中提供切面增强的具体设计,在

这个Advice通知被正确配置以后,在目标方法调用结束并成功返回的时候,接口会被Spring AOP回调。

与前面分析BeforeAdvice一样,同样可以看到一个CountingAfterReturningAdvice,它的实现基本上与

CountingBeforeAdvice是一样的。

  所不同的是调用发生的时间。尽管增强逻辑相同,但是,如果它实现不同的AOP通知接口,就会被AOP

编织到不同的调用场合中。尽管它们完成的增强行为是一样的,都是根据目标方法名对调用次数进行统计,

但是它们的最终实现却有很大的不同,一个是在目标方法调用前实现切面增强,一个在目标方法成功调用

返回结果后实现切面增强。由此可见,AOP技术给应用带来的灵活性,使得相同的代码完全可以根据应用

的需要灵活地出现在不同的应用场合。

ThrowsAdvice

  了解了BeforeAdvice和AfterAdvice,在Spring AOP中,还有一种ThrowsAdvice,它在抛出异常是被回调

, 可以在CountingThrowsAdvice来了解ThrowsAdvice的使用方法,如下

 1     @SuppressWarnings("serial")
 2     public static class CountingThrowsAdvice extends MethodCounter implements ThrowsAdvice {
 3
 4         public void afterThrowing(IOException ex) throws Throwable {
 5             count(IOException.class.getName());
 6         }
 7
 8         public void afterThrowing(UncheckedException ex) throws Throwable {
 9             count(UncheckedException.class.getName());
10         }
11
12     }

  在afterThrowing中,从输入的异常对象中得到异常的名字并进行统计。

转载于:https://www.cnblogs.com/toUpdating/p/9762063.html

spring AOP 概述(一) Advice相关推荐

  1. Spring AOP概述及底层实现原理

    Spring AOP概述及底层实现原理 aop概述 AOP全称为Aspect Oriented Programming的缩写,意为:面向切面编程.将程序中公用代码进行抽离,通过动态代理实现程序功能的统 ...

  2. 第14章-Spring AOP通知(Advice)详解

    文章目录 一.概述 二.通知的定义 1. 前置通知 2. 后置通知 3. 环绕通知 4. 最终通知 5. 异常通知 三.通知的参数 1. 切入点 2. 通知的参数传递 四.通知的顺序 五.附录 1. ...

  3. Spring AOP通知实例 – Advice

    Spring AOP(面向方面编程)框架,用于在模块化方面的横切关注点.简单得说,它只是一个拦截器拦截一些过程,例如,当一个方法执行,Spring AOP 可以劫持一个执行的方法,在方法执行之前或之后 ...

  4. spring aop之各advice通知的执行顺序

    背景:学习测试时发现各测试情况不一,故进行记录. 详解参看:https://blog.csdn.net/rainbow702/article/details/52185827

  5. 【小家Spring】Spring AOP各个组件概述与总结【Pointcut、Advice、Advisor、Advised、TargetSource、AdvisorChainFactory...】

    每篇一句 基础技术总是枯燥但有价值的.数学.算法.网络.存储等基础技术吃得越透,越容易服务上层的各种衍生技术或产品 相关阅读 [小家Spring]Spring AOP原理使用的基础类打点(AopInf ...

  6. Spring Aop 组件概述

    Spring Aop 概述 AOP(Aspect-Oriented Programming) 面向切面编程, 这种编程模型是在 OOP(Object-Oriented Programming) 的基础 ...

  7. Spring AOP示例教程 - Aspect,Advice,Pointcut,JoinPoint,Annotations,XML Configuration

    Spring AOP示例教程 - Aspect,Advice,Pointcut,JoinPoint,Annotations,XML Configuration Spring Framework是基于两 ...

  8. Spring AOP知识详解

    本文来详细说下spring中的aop内容 文章目录 Spring AOP概述 Spring AOP一代 Pointcut Jointpoint Advice Advisor 织入 Spring AOP ...

  9. Spring AOP核心原理分析

    "横切"的技术,剖解开封装的对象内部,并将那些影响了多个类的公共行为封装到一个可重用模块,并将其命名为"Aspect",即切面.所谓"切面" ...

最新文章

  1. php JSON数据格式化输出方法
  2. easyui数据表格重置_Python办公自动化,自动更新表格,告别繁琐
  3. H5中使用Web Storage来存储结构化数据
  4. c语言ffffff错误,C语言打印16进制出现0xffffff现象的问题剖析!
  5. JZOJ100047.基因变异 (Standard IO)
  6. SQL注入——基于时间的盲注(九)
  7. [转]maven与java命名规则
  8. leetcode剑指 Offer 14- I. 剪绳子(动态规划)
  9. 【POJ - 2728】Desert King (最有比率生成树,分数规划)
  10. Jetty和Tomcat
  11. 微课|中学生可以这样学Python(例5.4):计算决赛现场选手得分
  12. SAP License:SAP MM配置中的一些常用的 TCODE
  13. 别怨自己命不好,先看看“厚德载物”你有几德?
  14. 从HDFS看分布式文件系统的设计需求
  15. SNMP(Simple Network Management Protocol)——简单网络管理协议
  16. 激光SLAM:LOAM-Livox 算法研究(1) -- 功能包编译与验证
  17. linux设置合上电脑,CentOS7设置笔记本合盖不休眠
  18. 论文细读:HOLMES:Real-time APT Detection through Correlation of Suspicious Information Flows
  19. 毕业设计 - 题目:基于机器视觉的图像矫正 (以车牌识别为例) - 图像畸变校正
  20. 1、野火freertos学习笔记

热门文章

  1. dlopen failed: couldn‘t map “/data/xxxx.so“ segment 1: Permission denied
  2. 苹果微信多开_一个手机能登两个微信吗
  3. 国产CPU的6大品牌,3大路线对比
  4. 计算机的应用技术包括CAE,CAE技术
  5. Activiti 介绍
  6. 关于数据字典的理解与设计
  7. linux查看固态硬盘寿命,CentOS下查看 ssd 寿命
  8. 期刊论文发表什么是省级刊物
  9. 多个excel合并到一个excel的不同sheet中
  10. J2EE基础之map集合框架