dubbo之SPI Wrapper分析

article/2025/10/22 20:39:46

写在前面

本文需要dubbo SPI的简单基础知识,对dubbo SPI不了解的朋友可以参考dubbo之SPI分析 。
源码!!!。

在dubbo之SPI分析 文章中我们分析了SPI机制,其中有种SPI是一个Wrapper类的情况,本文一起来看下Wrapper的使用,Wapper在dubbo中的作用类似于spring 中AOP,实现扩展类调用拦截的功能,现在考虑这样的一个场景,对于某个扩展类的调用,我们想要做以下的几件事:

1:统计调用时长。
2:记录调用日志。

想要满足这个需求,我们就可以通过Wrapper类来实现,分别对应4个类,如下:

BusinessTopInterface:SPI接口类。
MyConcreteBusinessTopInterface:目标扩展类。
CalWasteTimeWapper:统计调用耗时的Wapper类。
RecordInvokeLogWapper:记录调用日志的Wapper类。

类之间的结构关系如下图最终调用

在这里插入图片描述

下面我们就开始吧!

1:实例

1.1:定义扩展类接口

import com.alibaba.dubbo.common.extension.SPI;@SPI
public interface BusinessTopInterface {void sayHi();
}

1.2:定义扩展类

public class MyConcreteBusinessTopInterface implements BusinessTopInterface {@Overridepublic void sayHi() {System.out.println("MyConcreteBusinessTopInterface.sayHi ***111***");}
}

1.3:定义计算耗时Wrapper

public class CalWasteTimeWapper implements BusinessTopInterface {private BusinessTopInterface businessTopInterface;// 注意这里必须有一个接收BusinessTopInterface为唯一参数的构造函数,这样才会被识别为Wrapper类public CalWasteTimeWapper(BusinessTopInterface businessTopInterfaceIn) {this.businessTopInterface = businessTopInterfaceIn;}@Overridepublic void sayHi() {System.out.println("*** cal waste time begin***");long startTime = System.currentTimeMillis();// 调用被包裹的类,这里是RecordInvokeLogWapperbusinessTopInterface.sayHi();System.out.println("invoke waste time is: " + (System.currentTimeMillis() - startTime));}
}

1.4:记录调用日志Wrapper

public class RecordInvokeLogWapper implements BusinessTopInterface {private BusinessTopInterface businessTopInterface;public RecordInvokeLogWapper(BusinessTopInterface businessTopInterfaceIn) {this.businessTopInterface = businessTopInterfaceIn;}@Overridepublic void sayHi() {System.out.println("record invoke log to database!");// 调用被包装的类,这里是真正的扩展类MyConcreteBusinessTopInterfacethis.businessTopInterface.sayHi();}
}

1.5:配置文件

我这里是dongshi.daddy.wrapper.BusinessTopInterface

注意wrapper的顺序,越往下的越先被调用。

myConcreteBusinessTopInterface=dongshi.daddy.wrapper.MyConcreteBusinessTopInterface
recordInvokeLogWapper=dongshi.daddy.wrapper.RecordInvokeLogWapper
calWasteTimeWapper=dongshi.daddy.wrapper.CalWasteTimeWapper

1.6:main

public class WrapperMain {public static void main(String[] args) {BusinessTopInterface myConcreteBusinessTopInterface = ExtensionLoader.getExtensionLoader(BusinessTopInterface.class).getExtension("myConcreteBusinessTopInterface");myConcreteBusinessTopInterface.sayHi();}
}

运行:

*** cal waste time begin***
record invoke log to database!
MyConcreteBusinessTopInterface.sayHi ***111***
invoke waste time is: 2013

1.7:原理分析重要!!!

当我们执行方法getExtension("myConcreteBusinessTopInterface")时会执行到代码com.alibaba.dubbo.common.extension.ExtensionLoader.createExtension,源码如下:

class FakeCls {private T createExtension(String name) {Class<?> clazz = getExtensionClasses().get(name);if (clazz == null) {throw findException(name);}try {T instance = (T) EXTENSION_INSTANCES.get(clazz);if (instance == null) {EXTENSION_INSTANCES.putIfAbsent(clazz, clazz.newInstance());instance = (T) EXTENSION_INSTANCES.get(clazz);}injectExtension(instance);Set<Class<?>> wrapperClasses = cachedWrapperClasses;// 2022-01-23 14:45:21if (wrapperClasses != null && !wrapperClasses.isEmpty()) {for (Class<?> wrapperClass : wrapperClasses) {instance = injectExtension((T) wrapperClass.getConstructor(type).newInstance(instance));}}return instance;} catch (Throwable t) {throw new IllegalStateException("Extension instance(name: " + name + ", class: " +type + ")  could not be instantiated: " + t.getMessage(), t);}}
}

执行到2022-01-23 14:45:21处时instance确实是MyConcreteBusinessTopInterface,但是会被wrapperClasses进行层层包裹,如下debug:

在这里插入图片描述

执行后最终return的instance如下:

在这里插入图片描述

写在后面

没什么写的,就吟诗一首吧!

古人学问无遗力,少壮工夫老始成。
纸上得来终觉浅,绝知此事要躬行。

http://chatgpt.dhexx.cn/article/TZwR30S2.shtml

相关文章

MyBatis与QueryWrapper

目录 一、MyBatis标签 1.2 if标签 1.2 foreach标签 1.3 sql标签 1.4 where标签 1.5SQL片段的使用 二、条件构造器 2.1QueryWrapper 2.1.1查询 2.1.2 查询列2 2.1.2 删除 2.1.3子查询 2.1.4组装查询条件 2.1.5组装排序条件 2.1.6组装删除条件 2.1.7条件的优先级 2.1.8 子…

QueryWrapper常用方法

QueryWrapper常用方法 MybatisPlus 使用QueryWrapper测试用例 一、ge、gt、le、lt、isNull、isNotNull Test public void testQuery() { QueryWrapper<User> queryWrapper new QueryWrapper<>();queryWrapper.isNull("name").ge("age", 12)…

java service wrapper 中文_Java Service Wrapper实践

很久前就使用JAVA写了一个后台服务器,基于MINA框架的。并且一直部署运行于服务器中,当然有一个黑框框。 就正常运行而言,此种运行方式没有任何问题。 但是就这运行期间出现的一些问题: 1,服务器问题重启后,每次都要登录远程将服务器打开,即使我已经配置了开机自启动,但…

Gradle Wrapper 详解

Gradle Wrapper 详解 我们介绍了 Android 项目的目录及 Gradle 配置&#xff0c;我们提到有个目录是/gradle/wrapper。今天这篇文章我们来学习 Gradle Wrapper。通过这篇文章我们将了解什么是 Gradle Wrapper&#xff1f;为什么需要用 Gradle Wrapper&#xff1f;以及 Gradle …

wrapper java 64_Wrapper配置详解及高级应用

将一个简单的程度如HelloWorld 的应用包装秤Wrapper 服务并不复杂&#xff0c;甚至可以认为非常简单。但是实际项目应用过程中我们的程序一般较庞大&#xff0c;运行环境也较复杂。 通过Wrapper 配置文件的分析与配置进一步了解构建Wrapper 服务需要注意的关键点及重要部分。 首…

RDP Wrapper

说到要在非Windows Server版本上实现多用户远程桌面连接 &#xff0c;我们都知道一个大名鼎鼎的软件RDP Wrapper Library&#xff0c;它是开源的&#xff0c;所有秘密作者都没有保留&#xff0c;所有问题全部在GitHub上面写的清清楚楚&#xff0c;在这里感谢这些伟大的人&#…

wrapper后台启动java程序

文章目录 1&#xff0c;下载wrapper2&#xff0c;使用wrapper后台启动java程序2.1&#xff0c;编写java样例程序2.2&#xff0c;配置wrapper2.3&#xff0c;拷贝启动脚本2.4&#xff0c;启动服务 1&#xff0c;下载wrapper 下载地址&#xff1a;https://wrapper.tanukisoftwar…

Java常用类-包装类(Wrapper)简单介绍

目录 前言1、包装类(Wrapper) 前言 本文主要介绍java中的包装类 1、包装类(Wrapper) Java是面向对象的编程语言&#xff0c;但它也包含了8种基本数据类型&#xff0c;这8种基本数据类型不支持面向对象的编程机制&#xff0c;基本数据类型的数据也不具备“对象”的特性&#…

强大的wrapper

最近在看大数据和容器相关的东西&#xff0c;发现有一个模式被反复使用到&#xff0c;关键是被用的很恰当且优雅&#xff0c;并能在这些关键技术中都发挥着至关重要的核心作用。我想你已经猜到了&#xff0c;他就是Eminem——强大的rapper——哦&#xff0c;不对&#xff0c;是…

mybatis-plus中wrapper的用法(详细)

用到了wrapper&#xff0c;ge、le、ne、eq等的用法&#xff0c;及多表查询自写sql整理资料记录一下&#xff0c;以备后续复习。 目录------------&#xff08;可点击相应目录直接跳转&#xff09; 一、条件构造器关系介绍 条件构造器关系介绍 &#xff1a; wapper介绍 &…

锚点(anchorPoint)

转载请说明出处&#xff0c;谢谢。 原文出处&#xff1a;点击打开链接 今天研究了一下关于layer的anchorPoint问题。 附图&#xff1a; 原文出处&#xff1a;点击打开链接 位置坐标 position&#xff0b;anchorPoint&#xff08;左下角0,0原点坐标系&#xff09;。 要确…

cocos2d-x中的锚点(AnchorPoint)理解

锚点的定义 锚点是指节点在进行形状变换、位置变动时依据的基准点。可以想象为钉在墙上用于固定纸张的小图钉&#xff0c;或者公告栏上用于固定纸张用的围棋状的小磁粒。当对某个节点调用setPosition时&#xff0c;cocos2d-x即会将其锚点移动到相应位置&#xff1b;当对节点进行…

cocos2d-x3.2总结(一)Position和AnchorPoint的关系

当cocos2d-x引擎需要绘图时&#xff0c;就需要Position和AnchorPoint等属性。例如&#xff1a;在创建一个Sprite时&#xff0c;要设置Sprite的Position&#xff0c;而我们常常没有设置AnchorPoint&#xff0c;这是因为精灵的AnchorPoint默认值为&#xff08;0.5,0.5&#xff09…

Anchor3

这里介绍如何修改 Anchor 的尺寸来提高小目标的检测效果,算法tricks优化小目标检测 修改 Anchor 尺寸 在实际的应用场景中&#xff0c;我们按照 MS COCO 标准中把大小不大于 32x32 或者占原始图片比率不足 0.01 的目标物体定义为一个小目标物体。 在使用 Anchor 的检测算法&…

ios-anchorPoint、position理解

anchorPoint(锚点)是layer的一个属性&#xff0c;下面我们来看看其对view的影响&#xff0c;本文主要通过图片方式展现&#xff1a; 锚点用单位坐标来描述也就是图层的相对坐标。在苹果文档中说明左下是(0,0),右上是&#xff08;1&#xff0c;1&#xff09;&#xff0c;mac系统…

Anderson‘s pointer analysis

本文是垃圾文章&#xff0c;请直接学习其它资料 南京大学《软件分析》课程08&#xff08;Pointer Analysis&#xff09;https://www.cs.cmu.edu/~aldrich/courses/15-819O-13sp/resources/pointer.pdf 指针分析 指针分析是一类特殊的数据流问题&#xff0c;它是其它静态程序分析…

iOS围绕某点缩放或旋转的AnchorPoint的设定

经常会遇到需求&#xff0c;要求手势的缩放或者旋转操作&#xff0c;要求动作变化围绕某一个特定点&#xff0c;或者是两指的中心点&#xff0c;或者是某一个点。 这个问题首先要清晰的知道&#xff0c;iOS各个view的层次关系。特别是&#xff0c;要清除的知道&#xff0c;当前…

彻底理解CALayer的position与anchorPoint

引言 相信初接触到CALayer的人都会遇到以下几个问题&#xff1a; 为什么修改anchorPoint会移动layer的位置&#xff1f; CALayer的position点是哪一点呢? anchorPoint与position有什么关系&#xff1f; 我也迷惑过&#xff0c;找过网上的教程&#xff0c;大部分都是复制粘…

position和anchorPoint

本人录制技术视频地址&#xff1a; https://edu.csdn.net/lecturer/1899 欢迎观看。 一、理论概述 1.简单介绍 CALayer有2个非常重要的属性&#xff1a;position和anchorPoint property CGPoint position; 用来设置CALayer在父层中的位置 以父层的左上角为原点(0, 0) prop…

Ant Design - Anchor

Anchor锚点 此组件的属性有以下几点&#xff1a; 现在给出一份例子 其他属性都很简单就不多说了&#xff0c;主要我遇到的麻烦是 getContainer 属性 锚点是默认body滚动的&#xff0c;所以如果你滚动的区域是body就会看到锚点的小蓝点是会随内容滚动的&#xff0c;但是如果你…