> [TOC]
## Spring AOP API 设计与实现
### Spring AOP API 整体设计
- Join point - `Joinpoint`
- Pointcut - `Pointcut`
- Advice 执行动作 - `Advice`
- Advice 容器 - `Advisor`
- Introduction - `IntroductionInfo`
- 代理对象创建基础类 - `ProxyCreatorSupport`
- 代理工厂 - `ProxyFactory` 、`ProxyFactoryBean`
- `AopProxyFactory` 配置管理器 - `AdvisedSupport`
- IoC 容器自动代理抽象 - `AbstractAutoProxyCreator`
### 接入点接口 - Joinpoint
- Interceptor 执行上下文 - Invocation
  - 方法拦截器执行上下文 - `MethodInvocation`
  - 构造器拦截器执行上下文 - `ConstructorInvocation`
- MethodInvocation 实现
  - 基于反射 - `ReflectiveMethodInvocation`
  - 基于 CGLIB - `CglibMethodInvocation`

### Joinpoint 条件接口 - Pointcut
核心组件:
- 类过滤器 - `ClassFilter`
- 方法匹配器 - `MethodMatcher`

#### Pointcut 操作
- 组合实现 - `org.springframework.aop.support.ComposablePointcut`
- 工具类
  - `ClassFilter` 工具类 - `ClassFilters`
  - `MethodMatcher` 工具类 - `MethodMatchers`
  - `Pointcut` 工具类 - `Pointcuts`
#### Pointcut 便利实现
- 静态 `Pointcut` - `StaticMethodMatcherPointcut`
- 正则表达式 `Pointcut` - `JdkRegexpMethodPointcut`
- 控制流 `Pointcut` - `ControlFlowPointcut`
#### Pointcut AspectJ 实现
- 实现类 - `org.springframework.aop.aspectj.AspectJExpressionPointcut`
- 指令支持 - `SUPPORTED_PRIMITIVES` 字段
- 表达式 - `org.aspectj.weaver.tools.PointcutExpression`
### Joinpoint 执行动作接口 - Advice

- Around Advice - Interceptor
  - 方法拦截器 - `MethodInterceptor`
  - 构造器拦截器 - `ConstructorInterceptor`
- 前置动作
  - 标准接口 - `org.springframework.aop.BeforeAdvice`
  - 方法级别 - `org.springframework.aop.MethodBeforeAdvice`
- 后置动作
  - `org.springframework.aop.AfterAdvice`
  - `org.springframework.aop.AfterReturningAdvice`
  - `org.springframework.aop.ThrowsAdvice`
#### Joinpoint Before Advice 标准实现

接口:
- 标准接口 - `org.springframework.aop.BeforeAdvice`
- 方法级别 - `org.springframework.aop.MethodBeforeAdvice`
实现:
- `org.springframework.aop.framework.adapter.MethodBeforeAdviceInterceptor`
#### Joinpoint Before Advice AspectJ 实现
- 实现类 - `org.springframework.aop.aspectj.AspectJMethodBeforeAdvice`
#### Joinpoint After Advice 标准实现

- 接口
  - `org.springframework.aop.AfterAdvice`
  - `org.springframework.aop.AfterReturningAdvice`
  - `org.springframework.aop.ThrowsAdvice`
- 实现
  - `org.springframework.aop.framework.adapter.ThrowsAdviceInterceptor`
  - `org.springframework.aop.framework.adapter.AfterReturningAdviceInterceptor`
#### Joinpoint After Advice AspectJ 实现
- 接口
  - `org.springframework.aop.AfterAdvice`
  - `org.springframework.aop.AfterReturningAdvice`
  - `org.springframework.aop.ThrowsAdvice`
- 实现
  - `org.springframework.aop.aspectj.AspectJAfterAdvice`
  - `org.springframework.aop.aspectj.AspectJAfterReturningAdvice`
  - `org.springframework.aop.aspectj.AspectJAfterThrowingAdvice`
### Advice 容器接口 - Advisor

- 接口 - org.springframework.aop.Advisor
  - PointcutAdvisor
    - 通用实现 - `org.springframework.aop.support.DefaultPointcutAdvisor`
  - `IntroductionAdvisor`
#### Pointcut 与 Advice 连接器 - PointcutAdvisor
- 接口 - org.springframework.aop.PointcutAdvisor
  - 通用实现
    - `org.springframework.aop.support.DefaultPointcutAdvisor`
  - AspectJ 实现
    - `org.springframework.aop.aspectj.AspectJExpressionPointcutAdvisor`
    - `org.springframework.aop.aspectj.AspectJPointcutAdvisor`
  - 静态方法实现
    - `org.springframework.aop.support.StaticMethodMatcherPointcutAdvisor`
  - IoC 容器实现
    - `org.springframework.aop.support.AbstractBeanFactoryPointcutAdvisor`
### Introduction 与 Advice 连接器 - IntroductionAdvisor

- 接口 - org.springframework.aop.IntroductionAdvisor
  - 元信息
    - `org.springframework.aop.IntroductionInfo`
  - 通用实现
    - `org.springframework.aop.support.DefaultIntroductionAdvisor`
  - AspectJ 实现
    - `org.springframework.aop.aspectj.DeclareParentsAdvisor`
### Advisor 的 Interceptor 适配器 - AdvisorAdapter

- 接口 - org.springframework.aop.framework.adapter.AdvisorAdapter
  - MethodBeforeAdvice实现
    - `org.springframework.aop.framework.adapter.MethodBeforeAdviceAdapter`
  - AfterReturningAdvice实现
    - `org.springframework.aop.framework.adapter.AfterReturningAdviceAdapter`
  - ThrowsAdvice 实现
    - `org.springframework.aop.framework.adapter.ThrowsAdviceAdapter`
### AOP 代理接口 - AopProxy

- 接口 - `org.springframework.aop.framework.AopProxy`
- 实现
  - JDK 动态代理
    - `org.springframework.aop.framework.JdkDynamicAopProxy`
  - CGLIB 字节码提升
    - `org.springframework.aop.framework.CglibAopProxy`
    - `org.springframework.aop.framework.ObjenesisCglibAopProxy`
#### AopProxy 工厂接口与实现

- 接口 - `org.springframework.aop.framework.AopProxyFactory`
- 默认实现:org.springframework.aop.framework.DefaultAopProxyFactory
  - 返回类型
    - `org.springframework.aop.framework.JdkDynamicAopProxy`
    - `org.springframework.aop.framework.CglibAopProxy`
    - `org.springframework.aop.framework.ObjenesisCglibAopProxy`
#### JDK AopProxy 实现 - JdkDynamicAopProxy
- 实现 - org.springframework.aop.framework.JdkDynamicAopProxy
  - 配置 - `org.springframework.aop.framework.AdvisedSupport`
  - 来源 - `org.springframework.aop.framework.DefaultAopProxyFactory`
#### CGLIB AopProxy 实现 - CglibAopProxy
- 实现 - org.springframework.aop.framework.CglibAopProxy
  - 配置 - `org.springframework.aop.framework.AdvisedSupport`
  - 来源 - `org.springframework.aop.framework.DefaultAopProxyFactory`
#### AopProxyFactory 配置管理器 - AdvisedSupport
- 核心 API - org.springframework.aop.framework.AdvisedSupport
  - 语义 - 代理配置
  - 基类 - `org.springframework.aop.framework.ProxyConfig`
  - 实现接口 - `org.springframework.aop.framework.Advised`
  - 使用场景 - `org.springframework.aop.framework.AopProxy` 实现
### Advisor 链工厂接口与实现 - AdvisorChainFactory
- 核心 API - org.springframework.aop.framework.AdvisorChainFactory
  - 默认实现 - `org.springframework.aop.framework.DefaultAdvisorChainFactory`
  - 返回类型
    - `org.springframework.aop.framework.InterceptorAndDynamicMethodMatcher`
    - `org.aopalliance.intercept.Interceptor`
### 目标对象来源接口与实现 - TargetSource

- 核心 API - org.springframework.aop.TargetSource
  - 实现
    - `org.springframework.aop.target.HotSwappableTargetSource`
    - `org.springframework.aop.target.AbstractPoolingTargetSource`
    - `org.springframework.aop.target.PrototypeTargetSource`
    - `org.springframework.aop.target.ThreadLocalTargetSource`
    - `org.springframework.aop.target.SingletonTargetSource`
### 代理对象创建基础类 - ProxyCreatorSupport

- 核心 API - org.springframework.aop.framework.ProxyCreatorSupport
  - 语义 - 代理对象创建基类
  - 基类 - `org.springframework.aop.framework.AdvisedSupport`
#### ProxyCreatorSupport 标准实现 - ProxyFactory
- 核心 API - org.springframework.aop.framework.ProxyFactory
  - 基类 - `org.springframework.aop.framework.ProxyCreatorSupport`
  - 特性增强
    - 提供一些便利操作
#### ProxyCreatorSupport IoC 容器实现 - ProxyFactoryBean
- 核心 API - org.springframework.aop.framework.ProxyFactoryBean
  - 基类 - `org.springframework.aop.framework.ProxyCreatorSupport`
  - 特点
    - Spring IoC 容器整合
      - `org.springframework.beans.factory.BeanClassLoaderAware`
      - `org.springframework.beans.factory.BeanFactoryAware`
  - 特性增强
    - 实现 `org.springframework.beans.factory.FactoryBean`
#### ProxyCreatorSupport AspectJ 实现 - AspectJProxyFactory
- 核心 API - org.springframework.aop.aspectj.annotation.AspectJProxyFactory
  - 基类 - `org.springframework.aop.framework.ProxyCreatorSupport`
  - 特点
    - AspectJ 注解整合
  - 相关 API
    - AspectJ 元数据 - `org.springframework.aop.aspectj.annotation.AspectMetadata`
    - AspectJ Advisor 工厂 - `org.springframework.aop.aspectj.annotation.AspectJAdvisorFactory`
### AdvisedSupport 事件监听器 - AdvisedSupportListener
- 核心 API - org.springframework.aop.framework.AdvisedSupportListener
  - 事件对象 - `org.springframework.aop.framework.AdvisedSupport`
  - 事件来源 - `org.springframework.aop.framework.ProxyCreatorSupport`
  - 激活事件触发 - `ProxyCreatorSupport#createAopProxy`
  - 变更事件触发 - 代理接口变化时、 Advisor 变化时、配置复制
### IoC 容器自动代理抽象 - AbstractAutoProxyCreator

- API - org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator
  - 基类 - `org.springframework.aop.framework.ProxyProcessorSupport`
  - 特点
    - 与 Spring Bean 生命周期整合
      - `org.springframework.beans.factory.config.SmartInstantiationAwareBeanPostProcessor`
#### IoC 容器自动代理标准实现 - AbstractAutoProxyCreator
- 基类 - org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator
  - 默认实现 - `DefaultAdvisorAutoProxyCreator`
  - Bean 名称匹配实现 - `BeanNameAutoProxyCreator`
  - Infrastructure Bean 实现 - `InfrastructureAdvisorAutoProxyCreator`
#### IoC 容器自动代理 AspectJ 实现 - AspectJAwareAdvisorAutoProxyCreator
- org.springframework.aop.aspectj.autoproxy.AspectJAwareAdvisorAutoProxyCreator
  - 基类 - `org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator`
### AOP Infrastructure Bean 接口 - AopInfrastructureBean
- 接口 - org.springframework.aop.framework.AopInfrastructureBean
  - 语义 - Spring AOP 基础 Bean 标记接口
  - 实现
    - `org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator`
    - `org.springframework.aop.scope.ScopedProxyFactoryBean`
  - 判断逻辑
    - `AbstractAutoProxyCreator#isInfrastructureClass`
    - `ConfigurationClassUtils#checkConfigurationClassCandidate`
### AOP 上下文辅助类 - AopContext
- API - org.springframework.aop.framework.AopContext
  - 语义 - `ThreadLocal` 的扩展,临时存储 AOP 对象
### AOP 代理工具类 - AopProxyUtils
- API - org.springframework.aop.framework.AopProxyUtils
  - 代表方法
    - `getSingletonTarget` - 从实例中获取单例对象
    - `ultimateTargetClass` - 从实例中获取最终目标类
    - `completeProxiedInterfaces` - 计算 `AdvisedSupport` 配置中所有被代理的接口
    - `proxiedUserInterfaces` - 从代理对象中获取代理接口
    - `isAopProxy` - 判断对象是否为代理对象
    - `isJdkDynamicProxy` - 判断对象是否为 JDK 动态代理对象
    - `isCglibProxy` - 判断对象是否为 CGLIB 代理对象
    - `getTargetClass` - 从对象中获取目标类型
    - `invokeJoinpointUsingReflection` - 使用 Java 反射调用 Joinpoint(目标方法)
### AspectJ Enable 模块驱动实现 - EnableAspectJAutoProxy
- 注解 - `org.springframework.context.annotation.EnableAspectJAutoProxy`
- 属性方法
  - `proxyTargetClass` - 是否已类型代理
  - `exposeProxy` - 是否将代理对象暴露在 `AopContext` 中
- 设计模式 - @Enable 模块驱动
  - `ImportBeanDefinitionRegistrar` 实现 - `org.springframework.context.annotation.AspectJAutoProxyRegistrar`
- 底层实现
  - `org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator`
### AspectJ XML 配置驱动实现 - ``
- XML 元素 - 
  - 属性
    - `proxy-target-class` - 是否已类型代理
    - `expose-proxy` - 是否将代理对象暴露在 `AopContext` 中
  - 设计模式 - Extensible XML Authoring
  - 底层实现
    - `org.springframework.aop.config.AspectJAutoProxyBeanDefinitionParser`
#### AOP 配置 Schema-based 实现 - `` 、``
- XML 元素 - 
  - 属性
    - `proxy-target-class` - 是否已类型代理
    - `expose-proxy` - 是否将代理对象暴露在 `AopContext` 中
  - 嵌套元素
    - `pointcut`
    - `advisor`
    - `aspect`
  - 底层实现
    - `org.springframework.aop.config.ConfigBeanDefinitionParser`
- XML 元素 - 
  - 父元素 - ``
  - 属性
    - `ref` - Spring Bean 引用的名称
    - `order` - Aspect 顺序数
  - 嵌套元素
    - `pointcut`
    - `declare-parents`
    - `before`
    - `after`
    - `after-returning`
    - `after-throwing`
    - `around`
#### Pointcut Schema-based 实现 - ``
- XML 元素 - 
  - 父元素 - `` 或 ``
  - 属性
    - `id` - Pointcut ID
    - `expression` - (必须)AspectJ 表达式
  - 底层实现
    - `org.springframework.aop.Pointcut`
#### Around Advice Schema-based 实现 - ``
- XML 元素 - 
  - 父元素 - ``
  - 属性
    - `pointcut` - AspectJ Pointcut 表达式
    - `pointcut-ref` - 引用的 AspectJ Pointcut 名称
    - `method` - 拦截目标方法
    - `arg-names` - 目标方法参数名称
#### Before Advice Schema-based 实现 - ``
- XML 元素 - 
  - 父元素 - ``
  - 属性
    - `pointcut` - AspectJ Pointcut 表达式
    - `pointcut-ref` - 引用的 AspectJ Pointcut 名称
    - `method` - 拦截目标方法
    - `arg-names` - 目标方法参数名称
#### After Advice Schema-based 实现 - ``
- XML 元素 - 
  - 父元素 - ``
  - 属性
    - `pointcut` - AspectJ Pointcut 表达式
    - `pointcut-ref` - 引用的 AspectJ Pointcut 名称
    - `method` - 拦截目标方法
    - `arg-names` - 目标方法参数名称
#### After Returning Advice Schema-based 实现 - ``
- XML 元素 - 
  - 父元素 - ``
  - 属性
    - `pointcut` - AspectJ Pointcut 表达式
    - `pointcut-ref` - 引用的 AspectJ Pointcut 名称
    - `method` - 拦截目标方法
    - `arg-names` - 目标方法参数名称
    - `returning` - 方法参数名称
#### After Throwing Advice Schema-based 实现 - ``
- XML 元素 - 
  - 父元素 - ``
  - 属性
    - `pointcut` - AspectJ Pointcut 表达式
    - `pointcut-ref` - 引用的 AspectJ Pointcut 名称
    - `method` - 拦截目标方法
    - `arg-names` - 目标方法参数名称
    - `throwing` - 方法参数名称
#### Adviser Schema-based 实现 - ``
- XML 元素 - 
  - 父元素 - ``
  - 属性
    - `advice-ref` - Advice Bean 引用
    - `pointcut` - AspectJ Pointcut 表达式
    - `pointcut-ref` - AspectJ Pointcut Bean 引用
    - `order` - Advisor 顺序数
#### Introduction Schema-based 实现 - ``
- XML 元素 - 
  - 父元素 - ``
  - 属性
    - `types-matching` - 是否已类型代理
    - `implement-interface` - 实现接口全类名
    - `default-impl` - 默认实现全类名
    - `delegate-ref` - 委派实现 Bean 引用
#### 作用域代理 Schema-based 实现 - ``
- XML 元素 - ``
- 属性
  - `proxy-target-class` - 是否已类型代理