Skip to content

Commit

Permalink
update spring-aop README.md spring-projects#1
Browse files Browse the repository at this point in the history
  • Loading branch information
paigeman committed Mar 29, 2022
1 parent 6b70df9 commit 5574de7
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 1 deletion.
31 changes: 31 additions & 0 deletions spring-aop/doc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,34 @@

todo

### 几种通知类型的执行顺序

todo

### AOP的整个流程

XML方式(以测试资源目录下的 `aopExample.xml` 中的 `testBean` 的获取为例 ):

1. 解析XML文件时,在 `AopNamespaceHandler#init` 中注册 `<aop:aspectj-autoproxy />` 标签对应的解析器 `AspectJAutoProxyBeanDefinitionParser`

![aop#1](resources/2022-03-29_21-19.png)

2. 调用 `AspectJAutoProxyBeanDefinitionParser#parse` 方法注册 `AnnotationAwareAspectJAutoProxyCreator`

![aop#2](resources/2022-03-29_21-26.png)

3. 因为 `AnnotationAwareAspectJAutoProxyCreator` 实现了 `BeanPostProcessor` 接口,所以会在 `AbstractApplicationContext#registerBeanPostProcessors` 中把它注册到 `AbstractBeanFactory``beanPostProcessors` 属性中

![aop#3](resources/2022-03-29_21-41.png)

4. 又因为 `AnnotationAwareAspectJAutoProxyCreator` 实现的是 `BeanPostProcessor` 的子接口 `InstantiationAwareBeanPostProcessor` ,所以在获取bean时,它的 `applyBeanPostProcessorsBeforeInstantiation` 方法和 `applyBeanPostProcessorsAfterInitialization` 方法会被 `AbstractAutowireCapableBeanFactory#resolveBeforeInstantiation` 调用

![aop#4](resources/2022-03-29_21-54.png)

5.

### AOP概念与Spring中实现的对应关系

* 切面 —— `@Aspect` 注解、 `Advisor` 接口及其实现类
* 通知 —— `Advice` 接口及其实现类、 `@Before``@After``@Around``@AfterThrowing``@AfterReturing` 注解
*
Binary file added spring-aop/doc/resources/2022-03-29_21-19.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added spring-aop/doc/resources/2022-03-29_21-26.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added spring-aop/doc/resources/2022-03-29_21-41.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added spring-aop/doc/resources/2022-03-29_21-54.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ public static void enableAspectJWeaving(
throw new IllegalStateException("No LoadTimeWeaver available");
}
}
/**
* <p>使用 {@link DefaultContextLoadTimeWeaver} 类型bean中的
* {@link DefaultContextLoadTimeWeaver#loadTimeWeaver} 属性注册转换器</p>
* */
weaverToUse.addTransformer(
new AspectJClassBypassingClassFileTransformer(new ClassPreProcessorAgentAdapter()));
}
Expand All @@ -118,10 +122,11 @@ public AspectJClassBypassingClassFileTransformer(ClassFileTransformer delegate)
@Override
public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined,
ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {

// 以org.aspectj或org/aspectj开头的类不进行处理
if (className.startsWith("org.aspectj") || className.startsWith("org/aspectj")) {
return classfileBuffer;
}
// 委托给AspectJ代理继续处理
return this.delegate.transform(loader, className, classBeingRedefined, protectionDomain, classfileBuffer);
}
}
Expand Down

0 comments on commit 5574de7

Please sign in to comment.