博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring AOP(二)AOPAlliance与SpringAOP核心接口介绍
阅读量:4160 次
发布时间:2019-05-26

本文共 6466 字,大约阅读时间需要 21 分钟。

目录

AOP联盟是java对于AOP提供的一系列标准接口,顶层接口有:	Advice通知,及其继承接口MethodInterceptor方法拦截器;	JointPoint连接点,及其继承接口MethodInvocation。Spring或其他具有AOP概念的框架都会依赖于此,从Spring扩展的接口来看,对于AOP的支持局限于方法的拦截。比如Spring AOP中就实现了Advice的扩展接口:方法前置增强、方法后置增强、异常增强等;另外还有对这些Advice的包装实现:MethodInterceptor方法拦截器。Spring中对于JoinPoint的实现包括了ProxyMethodInvocation,是AOP的核心类。另外Spring新增了一个PointCut切点的概念,一个PointCut对应多个JointPoint。

在分析源码实现之前,先来看一些基本概念与核心接口。

AOP联盟

AOP Alliance 是java中对于面向切面提供了一系列标准化接口,Spring或其他具有AOP概念的框架会依赖这个包。

在这里插入图片描述
这个包中有两个顶层接口:

  • Advice:代表要织入的逻辑
  • Joinpoint:连接点,增强逻辑的织入地点

1. Advice、MethodInterceptor拦截器(invoke方法:调用invocation.proceed)

Advice接口及其继承接口:

  • Advice:增强(通知),代表要织入的逻辑
  • Interceptor:拦截器,代表了以拦截器方式去实现通知
  • MethodInterceptor:方法拦截器(Spring中提供了实现类)
  • ConstructorInterceptor:构造器拦截器

接口关系:

Advice   |   ├── Interceptor   |       |   |       ├── MethodInterceptor   |       ├── ConstructorInterceptor

接口方法:

(注意此处的MethodInterceptor是org.aopalliance.intercept.MethodInterceptor而非cglib包中的同名接口)

public interface Advice {
}public interface Interceptor extends Advice {
}//Spring中实现了此接口:MethodBeforeAdviceInterceptor、AfterReturningAdviceInterceptor等public interface MethodInterceptor extends Interceptor {
//需要执行的时候,调用invocation.proceed() Object invoke(MethodInvocation invocation) throws Throwable;}public interface ConstructorInterceptor extends Interceptor {
Object construct(ConstructorInvocation invocation) throws Throwable;}

2.Joinpoint 、MethodInvocation连接点(proceed方法:执行此拦截点)

Joinpoint接口及其继承接口:

  • Joinpoint:连接点
  • Invocation:调用连接点, 表示程序中的调用 ,是一个可以被拦截器拦截的连接点
  • MethodInvocation:方法调用连接点(Spring中提供了实现类)
  • ConstructorInvocation:构造器调用连接点
Joinpoint   |   ├── Invocation   |       |   |       ├── MethodInvocation   |       ├── ConstructorInvocation

接口方法:

(注意此处的Joinpoint是org.aopalliance.intercept.Joinpoint,而非指Spring中的org.aspectj.lang.JoinPoint)

public interface Joinpoint {
// 执行此拦截点,并进入到下一个连接点 Object proceed() throws Throwable; // 返回保存当前连接点静态部分的对象 Object getThis(); // 返回此静态连接点 一般就为当前的Method AccessibleObject getStaticPart();}public interface Invocation extends Joinpoint {
// 获得参数,如方法入参 Object[] getArguments();}//作为AOP Alliance中的底层接口,Spring AOP中实现了此接口:ReflectiveMethodInvocationpublic interface MethodInvocation extends Invocation {
// 返回当前被调用的Method Method getMethod();}public interface ConstructorInvocation extends Invocation {
Constructor
getConstructor();}

Spring AOP

Spring AOP并不是自立门户,而是在AOP联盟定义的一系列接口上,提供实现类或者进行封装。

1.Advice 通知-扩展接口(MethodBeforeAdvice、ThrowsAdvice、AfterReturningAdvice)

Spring对于Advice接口继承扩展:

  • BeforeAdvice:前置增强
  • AfterAdvice:后置增强
  • MethodBeforeAdvice:方法前置增强
  • AfterReturningAdvice:方法后置增强
  • ThrowsAdvice:异常增强
Advice    |   ├── Interceptor   |       |   |       ├── MethodInterceptor   |       ├── ConstructorInterceptor   |   ├── BeforeAdvice   |       |   |       ├── MethodBeforeAdvice   |   ├── AfterAdvice   |       |   |       ├── ThrowsAdvice   |       ├── AfterReturningAdvice

各个接口中方法:

public interface BeforeAdvice extends Advice {
}public interface MethodBeforeAdvice extends BeforeAdvice {
void before(Method method, Object[] args, Object target) throws Throwable;}public interface AfterAdvice extends Advice {
}public interface ThrowsAdvice extends AfterAdvice {
}public interface AfterReturningAdvice extends AfterAdvice {
void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable;}

2.MethodInterceptor方法拦截器-扩展接口(对Advice的包装)

Spring中对于Advice接口继承扩展:

  • IntroductionInterceptor:引介增强,类级别的增强器
  • MethodBeforeAdviceInterceptor
  • AfterReturningAdviceInterceptor
  • ThrowsAdviceInterceptor
  • … …
Advice    |   ├── Interceptor   |       |   |       ├── MethodInterceptor   |       |        |   |       |        ├── IntroductionInterceptor   |       |        ├── MethodBeforeAdviceInterceptor   |       |        ├── AfterReturningAdviceInterceptor   |       |        ├── ThrowsAdviceInterceptor   |       ├── ConstructorInterceptor   |   ├── BeforeAdvice   |       |   |       ├── MethodBeforeAdvice   |   ├── AfterAdvice   |       |   |       ├── ThrowsAdvice   |       ├── AfterReturningAdvice

接口方法,以MethodBeforeAdviceInterceptor为例:

public class MethodBeforeAdviceInterceptor implements MethodInterceptor, Serializable {
private MethodBeforeAdvice advice; // MethodBeforeAdviceInterceptor只是将MethodBeforeAdvice进行了一个包装 public MethodBeforeAdviceInterceptor(MethodBeforeAdvice advice) {
Assert.notNull(advice, "Advice must not be null"); this.advice = advice; } @Override public Object invoke(MethodInvocation mi) throws Throwable {
this.advice.before(mi.getMethod(), mi.getArguments(), mi.getThis() ); return mi.proceed(); }}

同样的,AfterReturningAdviceInterceptor是对AfterReturningAdvice的包装、ThrowsAdviceInterceptor是对ThrowsAdvice的包装。

3.MethodInvocation 方法调用连接点-扩展接口ProxyMethodInvocation、实现类ReflectiveMethodInvocation

Spring对于Joinpoint接口的继承扩展:

  • ProxyMethodInvocation:继承自MethodInvocation接口,是Spring AOP的核心接口
  • ReflectiveMethodInvocation:ProxyMethodInvocation的实现
Joinpoint   |   ├── Invocation   |       |   |       ├── MethodInvocation   |        |        |   |        |        ├── ProxyMethodInvocation   |        |        |        |   |        |        |        ├── ReflectiveMethodInvocation   |       ├── ConstructorInvocation

接口方法:

public interface ProxyMethodInvocation extends MethodInvocation {
Object getProxy(); MethodInvocation invocableClone(); MethodInvocation invocableClone(Object... arguments); void setArguments(Object... arguments); void setUserAttribute(String key, Object value); Object getUserAttribute(String key);}

4. Spring新增接口:Pointcut 切点

Pointcut是Spring AOP新增的接口,定义了Joinpoint连接点的匹配规则,即:一个Pointcut对应多个Joinpoint,也就是 Advice逻辑织入的Joinpoint连接点的集合

接口定义如下

public interface Pointcut {
ClassFilter getClassFilter(); MethodMatcher getMethodMatcher(); Pointcut TRUE = TruePointcut.INSTANCE;}

5. Spring新增接口:Advisor 通知器

Spring AOP新增了一个接口Advisor,用来包装 Advice通知 和 Pointcut切点 两个对象。

  • Advisor:持有一个Advice
  • PointcutAdvisor:在Advisor的基础上,持有一个Pointcut
  • IntroductionAdvisor:引介切面
Advisor    |   ├── PointcutAdvisor   ├── IntroductionAdvisor

接口方法:

public interface Advisor {
Advice getAdvice(); boolean isPerInstance();}public interface PointcutAdvisor extends Advisor {
Pointcut getPointcut();}public interface IntroductionAdvisor extends Advisor, IntroductionInfo {
ClassFilter getClassFilter(); void validateInterfaces() throws IllegalArgumentException;}

总结

上述几个概念之间的关系可以概括为

在这里插入图片描述

从Spring中所扩展的接口或者实现类来看,Spring 对 AOP 的支持局限于 方法的拦截

(如果需求超过了简单的方法调用,如构造器或属性拦截,那么需要考虑使用 AspectJ 来实现切面)

转载地址:http://dajxi.baihongyu.com/

你可能感兴趣的文章
【Python】学习笔记——-7.3、继承和多态
查看>>
【Python】学习笔记——-7.5、实例属性和类属性
查看>>
git中文安装教程
查看>>
虚拟机 CentOS7/RedHat7/OracleLinux7 配置静态IP地址 Ping 物理机和互联网
查看>>
Jackson Tree Model Example
查看>>
常用js收集
查看>>
如何防止sql注入
查看>>
springmvc传值
查看>>
在Eclipse中查看Android源码
查看>>
Android使用webservice客户端实例
查看>>
[转]C语言printf
查看>>
C 语言 学习---回调、时间定时更新程序
查看>>
第十一章 - 直接内存
查看>>
Single Number II --出现一次的数(重)
查看>>
对话周鸿袆:从程序员创业谈起
查看>>
Mysql中下划线问题
查看>>
Xcode 11 报错,提示libstdc++.6 缺失,解决方案
查看>>
vue项目打包后无法运行报错空白页面
查看>>
Vue 解决部署到服务器后或者build之后Element UI图标不显示问题(404错误)
查看>>
element-ui全局自定义主题
查看>>