图床更新,请注意查看readme

This commit is contained in:
youthlql
2022-07-24 19:36:01 +08:00
parent 618eb5f343
commit 130c2ff4bf
59 changed files with 818 additions and 10839 deletions

View File

@@ -20,7 +20,7 @@ date: 2022-04-17 12:01:02
## 流程图-AOP运行流程原理
<img src="https://npm.elemecdn.com/youthlql@1.0.7/spring-sourcecode-v1/flow_chart/AOP%E8%BF%90%E8%A1%8C%E6%B5%81%E7%A8%8B%E5%8E%9F%E7%90%86.jpg">
<img src="https://upyunimg.imlql.cn/youthlql@1.0.7/spring-sourcecode-v1/flow_chart/AOP%E8%BF%90%E8%A1%8C%E6%B5%81%E7%A8%8B%E5%8E%9F%E7%90%86.jpg">
## 由Aop的执行流程引出方法拦截器
@@ -28,11 +28,11 @@ date: 2022-04-17 12:01:02
断点打到这里F7进入方法
<img src="https://npm.elemecdn.com/youthlql@1.0.6/spring-sourcecode-v1/chapter_07/image-20211012195045643.png"/>
<img src="https://upyunimg.imlql.cn/youthlql@1.0.6/spring-sourcecode-v1/chapter_07/image-20211012195045643.png"/>
自然而然的跳到了cglib这里
<img src="https://npm.elemecdn.com/youthlql@1.0.6/spring-sourcecode-v1/chapter_07/image-20211012195313364.png" />
<img src="https://upyunimg.imlql.cn/youthlql@1.0.6/spring-sourcecode-v1/chapter_07/image-20211012195313364.png" />
#### CglibAopProxy#intercept()
@@ -80,7 +80,7 @@ date: 2022-04-17 12:01:02
#### CglibAopProxy#getInterceptorsAndDynamicInterceptionAdvice()
<img src="https://npm.elemecdn.com/youthlql@1.0.6/spring-sourcecode-v1/chapter_07/image-20211012195459849.png" />
<img src="https://upyunimg.imlql.cn/youthlql@1.0.6/spring-sourcecode-v1/chapter_07/image-20211012195459849.png" />
1. 把5个增强器变成了方法拦截器增强器只是保存信息的真正执行还得靠方法拦截器。
2. 我们再给上面的470行打上断点看下之前是如何生成方法拦截器的。因为第一次生成的时候没有缓存肯定能进去470行。
@@ -91,9 +91,9 @@ date: 2022-04-17 12:01:02
#### Debug调用栈
<img src="https://npm.elemecdn.com/youthlql@1.0.6/spring-sourcecode-v1/chapter_07/image-20211012201950845.png" />
<img src="https://upyunimg.imlql.cn/youthlql@1.0.6/spring-sourcecode-v1/chapter_07/image-20211012201950845.png" />
<img src="https://npm.elemecdn.com/youthlql@1.0.6/spring-sourcecode-v1/chapter_07/image-20211012202049726.png" />
<img src="https://upyunimg.imlql.cn/youthlql@1.0.6/spring-sourcecode-v1/chapter_07/image-20211012202049726.png" />
可以看到就是在之前创建代理对象的时候增强器转成的拦截器
@@ -240,7 +240,7 @@ public class MethodBeforeAdviceInterceptor implements MethodInterceptor, BeforeA
然后debug回到accept()方法
<img src="https://npm.elemecdn.com/youthlql@1.0.6/spring-sourcecode-v1/chapter_07/image-20211012202236451.png" />
<img src="https://upyunimg.imlql.cn/youthlql@1.0.6/spring-sourcecode-v1/chapter_07/image-20211012202236451.png" />
@@ -248,12 +248,12 @@ public class MethodBeforeAdviceInterceptor implements MethodInterceptor, BeforeA
## 正式开始分析AOP运行流程-链式执行
<img src="https://npm.elemecdn.com/youthlql@1.0.6/spring-sourcecode-v1/chapter_07/image-20211012220057607.png"/>
<img src="https://upyunimg.imlql.cn/youthlql@1.0.6/spring-sourcecode-v1/chapter_07/image-20211012220057607.png"/>
1. F7进入方法上面讲过会调用CglibAopProxy内部类的DynamicAdvisedInterceptor#intercept()。这次我们来说下为什么会跳到DynamicAdvisedInterceptor#intercept()方法
2. HelloService是一个代理对象它的AOP代理是一个DynamicAdvisedInterceptor对象
<img src="https://npm.elemecdn.com/youthlql@1.0.6/spring-sourcecode-v1/chapter_07/image-20211012220839278.png"/>
<img src="https://upyunimg.imlql.cn/youthlql@1.0.6/spring-sourcecode-v1/chapter_07/image-20211012220839278.png"/>
3. 而DynamicAdvisedInterceptor实现了MethodInterceptor接口
@@ -347,27 +347,27 @@ public interface MethodInterceptor extends Interceptor {
}
```
<img src="https://npm.elemecdn.com/youthlql@1.0.6/spring-sourcecode-v1/chapter_07/image-20211012223909994.png" />
<img src="https://upyunimg.imlql.cn/youthlql@1.0.6/spring-sourcecode-v1/chapter_07/image-20211012223909994.png" />
在前面创建HelloService代理对象时创建好的方法拦截器然后调用proceed()
<img src="https://npm.elemecdn.com/youthlql@1.0.6/spring-sourcecode-v1/chapter_07/image-20211012224227624.png" />
<img src="https://upyunimg.imlql.cn/youthlql@1.0.6/spring-sourcecode-v1/chapter_07/image-20211012224227624.png" />
### CglibMethodInvocation#proceed()
<img src="https://npm.elemecdn.com/youthlql@1.0.6/spring-sourcecode-v1/chapter_07/image-20211012224431389.png" />
<img src="https://upyunimg.imlql.cn/youthlql@1.0.6/spring-sourcecode-v1/chapter_07/image-20211012224431389.png" />
顾名思义就是用来执行Cglib生成的代理对象的方法
<img src="https://npm.elemecdn.com/youthlql@1.0.6/spring-sourcecode-v1/chapter_07/image-20211012224629196.png"/>
<img src="https://upyunimg.imlql.cn/youthlql@1.0.6/spring-sourcecode-v1/chapter_07/image-20211012224629196.png"/>
#### CglibAopProxy#proceed()
<img src="https://npm.elemecdn.com/youthlql@1.0.6/spring-sourcecode-v1/chapter_07/image-20211012231748956.png" />
<img src="https://upyunimg.imlql.cn/youthlql@1.0.6/spring-sourcecode-v1/chapter_07/image-20211012231748956.png" />
```java
public Object proceed() throws Throwable {
@@ -429,7 +429,7 @@ public interface MethodInterceptor extends Interceptor {
}
```
<img src="https://npm.elemecdn.com/youthlql@1.0.6/spring-sourcecode-v1/chapter_07/image-20211012225055742.png"/>
<img src="https://upyunimg.imlql.cn/youthlql@1.0.6/spring-sourcecode-v1/chapter_07/image-20211012225055742.png"/>
上面5个拦截器都继承了Spring的org.aopalliance.intercept.MethodInterceptor和Cglib的MethodInterceptor没关系了
@@ -437,19 +437,19 @@ public interface MethodInterceptor extends Interceptor {
获取第一个拦截器ExposeInvocationInterceptor
<img src="https://npm.elemecdn.com/youthlql@1.0.6/spring-sourcecode-v1/chapter_07/image-20211012225804099.png"/>
<img src="https://upyunimg.imlql.cn/youthlql@1.0.6/spring-sourcecode-v1/chapter_07/image-20211012225804099.png"/>
往下走直接走到了这里准备调用ExposeInvocationInterceptor的invoke()
<img src="https://npm.elemecdn.com/youthlql@1.0.6/spring-sourcecode-v1/chapter_07/image-20211012230419970.png" />
<img src="https://upyunimg.imlql.cn/youthlql@1.0.6/spring-sourcecode-v1/chapter_07/image-20211012230419970.png" />
<img src="https://npm.elemecdn.com/youthlql@1.0.6/spring-sourcecode-v1/chapter_07/image-20211012230646766.png"/>
<img src="https://upyunimg.imlql.cn/youthlql@1.0.6/spring-sourcecode-v1/chapter_07/image-20211012230646766.png"/>
##### CglibAopProxy#proceed()
<img src="https://npm.elemecdn.com/youthlql@1.0.6/spring-sourcecode-v1/chapter_07/image-20211012230842658.png" />
<img src="https://upyunimg.imlql.cn/youthlql@1.0.6/spring-sourcecode-v1/chapter_07/image-20211012230842658.png" />
```java
public Object proceed() throws Throwable {
@@ -481,35 +481,35 @@ public Object proceed() throws Throwable {
结果又调回了,很明显这是个递归调用
<img src="https://npm.elemecdn.com/youthlql@1.0.6/spring-sourcecode-v1/chapter_07/image-20211012230930440.png" />
<img src="https://upyunimg.imlql.cn/youthlql@1.0.6/spring-sourcecode-v1/chapter_07/image-20211012230930440.png" />
#### MethodBeforeAdviceInterceptor-前置通知
然后后面的调用逻辑和前面就一样了,如下
<img src="https://npm.elemecdn.com/youthlql@1.0.6/spring-sourcecode-v1/chapter_07/image-20211012231135459.png" />
<img src="https://upyunimg.imlql.cn/youthlql@1.0.6/spring-sourcecode-v1/chapter_07/image-20211012231135459.png" />
<img src="https://npm.elemecdn.com/youthlql@1.0.6/spring-sourcecode-v1/chapter_07/image-20211012231202337.png" />
<img src="https://upyunimg.imlql.cn/youthlql@1.0.6/spring-sourcecode-v1/chapter_07/image-20211012231202337.png" />
<img src="https://npm.elemecdn.com/youthlql@1.0.6/spring-sourcecode-v1/chapter_07/image-20211012231327882.png"/>
<img src="https://upyunimg.imlql.cn/youthlql@1.0.6/spring-sourcecode-v1/chapter_07/image-20211012231327882.png"/>
这个时候就先执行切面的before方法前置通知就执行了
<img src="https://npm.elemecdn.com/youthlql@1.0.6/spring-sourcecode-v1/chapter_07/image-20211012232119227.png" />
<img src="https://upyunimg.imlql.cn/youthlql@1.0.6/spring-sourcecode-v1/chapter_07/image-20211012232119227.png" />
继续调父类的方法
<img src="https://npm.elemecdn.com/youthlql@1.0.6/spring-sourcecode-v1/chapter_07/image-20211012231412866.png"/>
<img src="https://upyunimg.imlql.cn/youthlql@1.0.6/spring-sourcecode-v1/chapter_07/image-20211012231412866.png"/>
#### AspectJAfterAdvice-后置通知
<img src="https://npm.elemecdn.com/youthlql@1.0.6/spring-sourcecode-v1/chapter_07/image-20211012232315412.png" />
<img src="https://upyunimg.imlql.cn/youthlql@1.0.6/spring-sourcecode-v1/chapter_07/image-20211012232315412.png" />
后置通知这里先不执行先继续执行下面的方法拦截器链路最后finally再执行后置通知
@@ -517,7 +517,7 @@ public Object proceed() throws Throwable {
#### AfterReturningAdviceInterceptor-返回通知
<img src="https://npm.elemecdn.com/youthlql@1.0.6/spring-sourcecode-v1/chapter_07/image-20211012232551952.png" />
<img src="https://upyunimg.imlql.cn/youthlql@1.0.6/spring-sourcecode-v1/chapter_07/image-20211012232551952.png" />
1. 先往下走,我们就继续
@@ -527,7 +527,7 @@ public Object proceed() throws Throwable {
#### AspectJAfterThrowingAdvice-异常通知
<img src="https://npm.elemecdn.com/youthlql@1.0.6/spring-sourcecode-v1/chapter_07/image-20211012232910377.png"/>
<img src="https://upyunimg.imlql.cn/youthlql@1.0.6/spring-sourcecode-v1/chapter_07/image-20211012232910377.png"/>
继续往下发现索引要超了
@@ -535,17 +535,17 @@ public Object proceed() throws Throwable {
#### 真正执行sayhello()
<img src="https://npm.elemecdn.com/youthlql@1.0.6/spring-sourcecode-v1/chapter_07/image-20211012233113154.png"/>
<img src="https://upyunimg.imlql.cn/youthlql@1.0.6/spring-sourcecode-v1/chapter_07/image-20211012233113154.png"/>
<img src="https://npm.elemecdn.com/youthlql@1.0.6/spring-sourcecode-v1/chapter_07/image-20211012233212466.png" />
<img src="https://upyunimg.imlql.cn/youthlql@1.0.6/spring-sourcecode-v1/chapter_07/image-20211012233212466.png" />
放行之后看控制台sayhello方法就打印了。并且到目前为止只有前面说的前置通知执行了。
<img src="https://npm.elemecdn.com/youthlql@1.0.6/spring-sourcecode-v1/chapter_07/image-20211012233317479.png" />
<img src="https://upyunimg.imlql.cn/youthlql@1.0.6/spring-sourcecode-v1/chapter_07/image-20211012233317479.png" />
然后咱们就往后返,
@@ -553,7 +553,7 @@ public Object proceed() throws Throwable {
#### AspectJAfterThrowingAdvice-异常通知
<img src="https://npm.elemecdn.com/youthlql@1.0.6/spring-sourcecode-v1/chapter_07/image-20211012233506053.png" />
<img src="https://upyunimg.imlql.cn/youthlql@1.0.6/spring-sourcecode-v1/chapter_07/image-20211012233506053.png" />
咱们这里没异常,就继续返回了
@@ -561,13 +561,13 @@ public Object proceed() throws Throwable {
返回到这里,准备执行返回通知
<img src="https://npm.elemecdn.com/youthlql@1.0.6/spring-sourcecode-v1/chapter_07/image-20211012233633239.png" />
<img src="https://upyunimg.imlql.cn/youthlql@1.0.6/spring-sourcecode-v1/chapter_07/image-20211012233633239.png" />
放行之后的控制台
<img src="https://npm.elemecdn.com/youthlql@1.0.6/spring-sourcecode-v1/chapter_07/image-20211012233731212.png" />
<img src="https://upyunimg.imlql.cn/youthlql@1.0.6/spring-sourcecode-v1/chapter_07/image-20211012233731212.png" />
继续返回
@@ -575,11 +575,11 @@ public Object proceed() throws Throwable {
准备执行后置通知
<img src="https://npm.elemecdn.com/youthlql@1.0.6/spring-sourcecode-v1/chapter_07/image-20211012233837744.png">
<img src="https://upyunimg.imlql.cn/youthlql@1.0.6/spring-sourcecode-v1/chapter_07/image-20211012233837744.png">
放行finally之后的控制台
<img src="https://npm.elemecdn.com/youthlql@1.0.6/spring-sourcecode-v1/chapter_07/image-20211012233944290.png" />
<img src="https://upyunimg.imlql.cn/youthlql@1.0.6/spring-sourcecode-v1/chapter_07/image-20211012233944290.png" />
@@ -591,13 +591,13 @@ public Object proceed() throws Throwable {
继续返回
<img src="https://npm.elemecdn.com/youthlql@1.0.6/spring-sourcecode-v1/chapter_07/image-20211012234108557.png" />
<img src="https://upyunimg.imlql.cn/youthlql@1.0.6/spring-sourcecode-v1/chapter_07/image-20211012234108557.png" />
#### CglibAopProxy$DynamicAdvisedInterceptor
<img src="https://npm.elemecdn.com/youthlql@1.0.6/spring-sourcecode-v1/chapter_07/image-20211012234214735.png" />
<img src="https://upyunimg.imlql.cn/youthlql@1.0.6/spring-sourcecode-v1/chapter_07/image-20211012234214735.png" />
@@ -760,7 +760,7 @@ public class AnnotationMainTest {
在还未进行refresh()十二大步刷新时容器就已经有了这两事件相关的Bean定义信息了。
<img src="https://npm.elemecdn.com/youthlql@1.0.6/spring-sourcecode-v1/chapter_07/image-20211013162535782.png"/>
<img src="https://upyunimg.imlql.cn/youthlql@1.0.6/spring-sourcecode-v1/chapter_07/image-20211013162535782.png"/>
@@ -768,7 +768,7 @@ public class AnnotationMainTest {
#### 继承树
<img src="https://npm.elemecdn.com/youthlql@1.0.6/spring-sourcecode-v1/chapter_07/image-20211013162805867.png" />
<img src="https://upyunimg.imlql.cn/youthlql@1.0.6/spring-sourcecode-v1/chapter_07/image-20211013162805867.png" />
1. 我们看到实现了SmartInitializingSingleton和BeanFactoryPostProcessor
2. BeanFactoryPostProcessor我们反复在说就是工厂后置处理环节。EventListenerMethodProcessor实现了BeanFactoryPostProcessor说明他在工厂后置处理环节会做事
@@ -776,7 +776,7 @@ public class AnnotationMainTest {
> AbstractApplicationContext#refresh() ==> AbstractApplicationContext#finishBeanFactoryInitialization() ==> DefaultListableBeanFactory#preInstantiateSingletons()
<img src="https://npm.elemecdn.com/youthlql@1.0.6/spring-sourcecode-v1/chapter_07/image-20211013164017769.png" />
<img src="https://upyunimg.imlql.cn/youthlql@1.0.6/spring-sourcecode-v1/chapter_07/image-20211013164017769.png" />
@@ -786,7 +786,7 @@ public class AnnotationMainTest {
Debug启动
<img src="https://npm.elemecdn.com/youthlql@1.0.6/spring-sourcecode-v1/chapter_07/image-20211013170249700.png"/>
<img src="https://upyunimg.imlql.cn/youthlql@1.0.6/spring-sourcecode-v1/chapter_07/image-20211013170249700.png"/>
果然是从工厂后置处理那里过来的
@@ -794,7 +794,7 @@ Debug启动
放行继续往下走。果然SmartInitializingSingleton这里开始做事了
<img src="https://npm.elemecdn.com/youthlql@1.0.6/spring-sourcecode-v1/chapter_07/image-20211013170435169.png" />
<img src="https://upyunimg.imlql.cn/youthlql@1.0.6/spring-sourcecode-v1/chapter_07/image-20211013170435169.png" />
@@ -850,7 +850,7 @@ Debug启动
我们在这里打上条件断点
<img src="https://npm.elemecdn.com/youthlql@1.0.6/spring-sourcecode-v1/chapter_07/image-20211013170852409.png"/>
<img src="https://upyunimg.imlql.cn/youthlql@1.0.6/spring-sourcecode-v1/chapter_07/image-20211013170852409.png"/>
@@ -858,15 +858,15 @@ Debug启动
<img src="https://npm.elemecdn.com/youthlql@1.0.6/spring-sourcecode-v1/chapter_07/image-20211013171247356.png" />
<img src="https://upyunimg.imlql.cn/youthlql@1.0.6/spring-sourcecode-v1/chapter_07/image-20211013171247356.png" />
F7进入下面的方法
<img src="https://npm.elemecdn.com/youthlql@1.0.6/spring-sourcecode-v1/chapter_07/image-20211013171641031.png" />
<img src="https://upyunimg.imlql.cn/youthlql@1.0.6/spring-sourcecode-v1/chapter_07/image-20211013171641031.png" />
咱们发现是创建了一个适配器
<img src="https://npm.elemecdn.com/youthlql@1.0.6/spring-sourcecode-v1/chapter_07/image-20211013171728867.png" />
<img src="https://upyunimg.imlql.cn/youthlql@1.0.6/spring-sourcecode-v1/chapter_07/image-20211013171728867.png" />
```java
public ApplicationListenerMethodAdapter(String beanName, Class<?> targetClass, Method method) {
@@ -885,17 +885,17 @@ public ApplicationListenerMethodAdapter(String beanName, Class<?> targetClass, M
为啥要创建这样一个适配器呢虽然我们的AppEventListener不是监听器它只是在方法里标注了监听注解我们自己没有写监听器。但是咱们解析@EventListener注解之后在这里生成的适配器却实现了EventListener也就说明这个适配器就是个监听器。
<img src="https://npm.elemecdn.com/youthlql@1.0.6/spring-sourcecode-v1/chapter_07/image-20211013171936784.png" />
<img src="https://upyunimg.imlql.cn/youthlql@1.0.6/spring-sourcecode-v1/chapter_07/image-20211013171936784.png" />
继续往下放行
<img src="https://npm.elemecdn.com/youthlql@1.0.6/spring-sourcecode-v1/chapter_07/image-20211013172130892.png" />
<img src="https://upyunimg.imlql.cn/youthlql@1.0.6/spring-sourcecode-v1/chapter_07/image-20211013172130892.png" />
<img src="https://npm.elemecdn.com/youthlql@1.0.6/spring-sourcecode-v1/chapter_07/image-20211013172239216.png" />
<img src="https://upyunimg.imlql.cn/youthlql@1.0.6/spring-sourcecode-v1/chapter_07/image-20211013172239216.png" />
把适配器放到了事件多播器里
@@ -907,7 +907,7 @@ public ApplicationListenerMethodAdapter(String beanName, Class<?> targetClass, M
最后就是三个方法,三个适配器
<img src="https://npm.elemecdn.com/youthlql@1.0.6/spring-sourcecode-v1/chapter_07/image-20211013172925798.png" />
<img src="https://upyunimg.imlql.cn/youthlql@1.0.6/spring-sourcecode-v1/chapter_07/image-20211013172925798.png" />
@@ -915,11 +915,11 @@ public ApplicationListenerMethodAdapter(String beanName, Class<?> targetClass, M
给下面的位置打上断点
<img src="https://npm.elemecdn.com/youthlql@1.0.6/spring-sourcecode-v1/chapter_07/image-20211013173037562.png" />
<img src="https://upyunimg.imlql.cn/youthlql@1.0.6/spring-sourcecode-v1/chapter_07/image-20211013173037562.png" />
<img src="https://npm.elemecdn.com/youthlql@1.0.6/spring-sourcecode-v1/chapter_07/image-20211013173146059.png" />
<img src="https://upyunimg.imlql.cn/youthlql@1.0.6/spring-sourcecode-v1/chapter_07/image-20211013173146059.png" />
##### AbstractApplicationContext#publishEvent()