交流
商城
MCN
登入
注册
首页
提问
分享
讨论
建议
公告
动态
发表新帖
发表新帖
aop第5-2章:ReflectiveMethodInvocation
分享
未结
0
841
李延
LV6
2021-05-16
悬赏:20积分
# 作用 aop方法方法最终是交给了当前类来执行。其中cglib是交给当前类的子类:CglibMethodInvocation执行,对于CglibMethodInvocation部分暂时不做解析,后续有需要的时候再做解析。 # 构造函数参数 - Object proxy 代理对象 - @Nullable Object target 被代理对象 - Method method 当前执行的方法 - Object[] arguments 参数 - @Nullable Class<?> targetClass 被代理对象class - List<Object> interceptorsAndDynamicMethodMatchers 切面列表 # proceed 执行代理方法,源码如下: ```java @Nullable public Object proceed() throws Throwable { // We start with an index of -1 and increment early. if (this.currentInterceptorIndex == this.interceptorsAndDynamicMethodMatchers.size() - 1) { //调用被代理对象的方法 return invokeJoinpoint(); } Object interceptorOrInterceptionAdvice = this.interceptorsAndDynamicMethodMatchers.get(++this.currentInterceptorIndex); if (interceptorOrInterceptionAdvice instanceof InterceptorAndDynamicMethodMatcher) { // Evaluate dynamic method matcher here: static part will already have // been evaluated and found to match. InterceptorAndDynamicMethodMatcher dm = (InterceptorAndDynamicMethodMatcher) interceptorOrInterceptionAdvice; Class<?> targetClass = (this.targetClass != null ? this.targetClass : this.method.getDeclaringClass()); if (dm.methodMatcher.matches(this.method, targetClass, this.arguments)) { return dm.interceptor.invoke(this); } else { // Dynamic matching failed. // Skip this interceptor and invoke the next in the chain. return proceed(); } } else { // It's an interceptor, so we just invoke it: The pointcut will have // been evaluated statically before this object was constructed. return ((MethodInterceptor) interceptorOrInterceptionAdvice).invoke(this); } } ``` 对于代理对象的调用,其实是一个链条的调用。 1. 首先通过currentInterceptorIndex 判断是否所有切面是否都调用了,如果都调用过。则调用被代理类的方法。 2. 如果还有切面就执行下一个切面的方法((MethodInterceptor) interceptorOrInterceptionAdvice).invoke(this)。 3. 其实在所有的切面中会在合适的地方调用当前类的proceed。让当前类去调用下一个切面。直到所有切面执行完成。
回帖
消灭零回复
提交回复
热议榜
java 相关知识分享
8
好的程序员与不好的程序员
6
写给工程师的十条精进原则
5
spring boot以jar包运行配置的logback日志文件没生成
5
一步一步分析SpringBoot启动源码(一)
5
MockMvc测试
5
【吐槽向】是不是有个吐槽的板块比较好玩
4
logstash jdbc同步mysql多表数据到elasticsearch
3
IntelliJ IDEA 优质License Server
3
.gitignore忽略规则
3
SpringBoot启动源码分析
3
一步一步分析SpringBoot启动源码(三)
3
2
一步一步分析SpringBoot启动源码(二)
2
积分不够将无法发表新帖
2
官方产品
Meta-Boot - 基于MCN
MCN - 快速构建SpringBoot应用
微信扫码关注公众号