AOP的几个成员

JoinPoint

image.png

公民之Pointcut

就是表达JoinPoint的公式

公民之Advice(横切逻辑的载体)

Before Advice

image.png

After Advice(有三种)

image.png

Around Advice

image.png

Introduction(**不是根据执行时间来分的,根据完成的功能来分的)

image.png
image.png

公民之Aspect(就是把切点和加入的逻辑模块化封装起来)

image.png

织入和织入器

我们有了Aspect(切面之后),要把增强逻辑放到指定的pointcut上,这个过程就叫做织入,完成这个过程的工具就叫织入器。
image.png

目标对象

image.png

代理模式

介绍代理模式

image.png

静态代理

image.png

动态代理

jdk动态代理

image.png

cglib动态代理

image.png

Spring Aop的成员

pointcut

Pointcut的结构图(针对MethodMatcher)

image.png

Pointcut的介绍

image.png

几个常见的Pointcut实现

NameMatchMethodPointcut

image.png

JdkRegexpMethodPointcut

image.png

AnnotationMatchingPointcut

image.png

ComposablePointcut

image.png

ControlFlowPointcut(最特殊)

image.png

自定义Pointcut

image.png
image.png

ioc中的PointCut

image.png

spring Aop的Advice(Introduction将单独讲)

结构图(图中列出的都是per-class类型的Advice)

image.png

per-class类型的Advice

Advice实例能在所有目标对象类中共享,通常只拦截方法,不会为目标对象添加新特性或者保留一些状态

Before Advice

image.png

After Advice

ThrowsAdvice

image.png

AfterReturning Advice

image.png

AroundAdvice

image.png

per-instance类型的advice(introduction)

不会在目标类所有对象实例之间共享,为不同的实例对象保存不同的状态和特性。目标对象的代理对象就会具有新的行为
image.png

Introduction的结构图

image.png

Introduction之静态分支IntroductionInfoimage.png

DelegatingIntroductionInterceptor(假的Introduction)

image.png

DelegatePerTargetObjectIntroductionInterceptor(真正意义上的IntroductionAdvice)

image.png

spring aop的Aspect(将Pointcut和advice装在一起的箱子Advisor)

两类Advisor

image.png

PointcutAdvisor

结构图

image.png

DefaultPointcutAdvisor(最常用的,所有Pointcut和除Introduction的Advice都可以)

使用实例:

  1. 编码形式

image.png

  1. 容器注册形式

image.png

NameMatchMethodPointcutAdvisor(要求Pointcut是NameMatchMethodPointcut,Advice要求不是Introduction)

使用示例:

  1. 编码方式

image.png
image.png

注意:调用setMappedName()或者setMappedNames()底层调用的其实是内部持有的NameMatchMethodPointcut.setMappedName()或者setMappedNames()。
image.png

  1. 容器方式

image.png

RegexpMethodPointcutAdvisor(Pointcut只能是正则表达式,Advice是除Introduction的所有)

内部Pointcut的类型—正则表达式

image.png
image.png

使用示例(ioc注册)

image.png

DefaultBeanFactoryPointcutAdvisor(用的很少)

image.png

IntroductionAdvisor

特点

只能进行类级别的拦截,并且Advice只能是Introduction,为Introduction特定的

结构图

image.png

DefaultIntroductionAdvisor(另外一个后面介绍)

image.png

Spring aop的织入

最基本的织入器ProxyFactory,生产代理对象

使用示例

image.png
image.png

基于接口生成代理对象

image.png

基于子类生成代理对象

image.png

两个参数改变类的代理方式

  1. 设置代理类的class

image.png

  1. optimize设置为true

image.png

Introduction的织入

image.png

ProxyFactory的本质

AopProxyFactory的结构图

image.png

AopProxyFactory:
image.png

AopProxy:
image.png

AopProxyFactory的实现原理

子类DefaultAopProxyFactory(根据AdvisedSupport实例config 或者 判断有无实现接口来考虑使用jdk动态代理)

image.png

AdvisedSupport(根据这个来判断用哪种方式创建代理对象,生成代理对象所需要的信息的载体)

结构图(图中可得出,承载的信息主要分两类)

image.png

ProxyConfig—-将要生成的代理对象的控制信息

image.png

Advised—-将要生成的代理对象的必要信息

image.png
image.png

ProxyFactory的结构图

image.png

ProxyFactory的几个兄弟

image.png

ioc容器中的织入器—-ProxyFactory的兄弟之ProxyFactoryBean

ProxyFactoryBean的本质

image.png

ioc容器中的织入器—-ProxyFactory的兄弟之AspectJProxyFactory

通过编程方式织入

image.png

自动装配方式织入入

image.png

加速织入过程(自动代理,基于ApplicationContext)

实现原理

image.png

自动代理的实现类(AutoProxyCreator)

结构图

image.png

BeanNameAutoProxyCreator

image.png
image.png

image.png
image.png

DefaultAdvisorAutoProxyCreator

image.png
image.png
image.png
image.png

TargetSource(目标对象的容器)

介绍

image.png

可用的TargetSource实现类

SingletonTargetSource

image.png
image.png

prototypeTargetSource

image.png

HotSwappableTagetSource(可以用来换数据源)

image.png
image.png
image.png

CommonsPoolTagetSource

image.png

ThreadLocalTagetSource

image.png

Aspect中注解的本质

@Pointcut本质

本质:@pointcut通过解析最终会转换成面向Aspectj的Pointcut具体实现—->AspectJExpressionPointcut(属于Spring Aop中pointcut的子类)
image.png

AOP的小陷阱

场景再现

image.png

原因分析

image.png

解决办法

image.png