当前位置:网站首页>@Extension, @spi annotation principle
@Extension, @spi annotation principle
2022-07-05 18:20:00 【Little boy_】
stay dubbo Source code , It involves some dubbo Custom comments , This note mainly records the principle and use of these annotations , Now that you know that , It's convenient for us to treat dubbo Second expansion
@SPI
dubbo Of SPI Mechanism , and Java Native SPI Mechanism compared to , Added name, You can specify the corresponding name, When we use it , According to name, Get the extension class you want to use , If you get the implementation class , Appoint name by true, The default return is @spi Specified in the annotation name The corresponding implementation class
Simply speaking :@SPI Specified in the annotation value value , Is the default implementation class of the current interface , such as , The following screenshot , It means Protocol The default implementation class is DubboProtocol
Where can this be reflected ?
com.alibaba.dubbo.common.extension.ExtensionLoader#loadExtensionClasses
In this method , Is the file added to the specified file directory , First of all, it will be based on type, Get the corresponding @SPI annotation , there type, Is the corresponding interface ; therefore , Here you can see , Will be able to @SPI In the annotations value Corresponding key, Set to CacheDefaultName In this variable
com.alibaba.dubbo.common.extension.ExtensionLoader#getExtension
In this method , It is based on the name, Find the corresponding implementation class , such as : At present type yes protocol, If you participate dubbo, That is to find key yes dubbo Of protocol Implementation class , Usually dubboProtocol
Here we will see , If so, then name yes true, Indicates that the default implementation class is to be obtained
Here we will see , It's from CacheDefaultName From
public T getDefaultExtension() {
getExtensionClasses();
if (null == cachedDefaultName || cachedDefaultName.length() == 0
|| "true".equals(cachedDefaultName)) {
return null;
}
return getExtension(cachedDefaultName);
}
therefore :@SPI annotation , Is added to the interface , Represents the default implementation class of the interface , At the time of acquisition , If you enter name yes true, Will take the default implementation class
@Extension
Usually in use dubbo Of SPI Mechanism , We need to use it in the document by ourselves key = Full class name ; In this format name; But if we don't specify in the file , That can be used @Extension annotation , Add to the implementation class , And designate key( there key It is equivalent to key), Because in dubbo Source code , When parsing files , If not specified name, Will resolve whether the implementation class has been added @Extension
What needs to be noted here is : This note and @SPI Note between the two , I don't think it matters much , because @SPI Annotated value, We are designating name by true When , Will be with @SPI Notes have something to do with
and @Extension The note is key A setting method of
com.alibaba.dubbo.common.extension.ExtensionLoader#loadClass
In this method , There's a piece of code 
The context of this method , A brief introduction , stay dubbo Source code , Will be added to type All corresponding implementation classes , According to the implementation class , To distinguish between , The current implementation class is a wrapper class ? Or adaptive extension class ? Or ordinary implementation classes , If it is an ordinary implementation , Will execute the method in the screenshot , When executed , If we do not specify key = Implementation class , At this time, we will judge the implementation class ( there clazz It is already an implementation class , because dubbo According to the full class name in the file , Generate a clazz) Is there any addition @Extension annotation
private String findAnnotationName(Class<?> clazz) {
com.alibaba.dubbo.common.Extension extension = clazz.getAnnotation(com.alibaba.dubbo.common.Extension.class);
if (extension == null) {
String name = clazz.getSimpleName();
if (name.endsWith(type.getSimpleName())) {
name = name.substring(0, name.length() - type.getSimpleName().length());
}
return name.toLowerCase();
}
return extension.value();
}
therefore ,@Extension The meaning of annotation is : If we are in the document , The implementation class is not specified key, Then we will judge whether the implementation class is used @Extension annotation , Yes , Will use the corresponding annotation value As a key
边栏推荐
- How to improve the thermal management in PCB design with the effective placement of thermal through holes?
- 登录连接 CDB 和 PDB
- 吴恩达团队2022机器学习课程,来啦
- 消除`if()else{ }`写法
- Is it safe to open an account and register stocks for stock speculation? Is there any risk? Is it reliable?
- JDBC reads a large amount of data, resulting in memory overflow
- 第十届全球云计算大会 | 华云数据荣获“2013-2022十周年特别贡献奖”
- 使用JMeter录制脚本并调试
- 让更多港澳青年了解南沙特色文创产品!“南沙麒麟”正式亮相
- Nanjing University: Discussion on the training program of digital talents in the new era
猜你喜欢

模拟百囚徒问题

node_ Exporter memory usage is not displayed
![Maximum artificial island [how to make all nodes of a connected component record the total number of nodes? + number the connected component]](/img/8b/a60fc36115580f018445e4c2a28a9d.png)
Maximum artificial island [how to make all nodes of a connected component record the total number of nodes? + number the connected component]

JVM third talk -- JVM performance tuning practice and high-frequency interview question record

图像分类,看我就够啦!

Star ring technology data security management platform defender heavy release

The 11th China cloud computing standards and Applications Conference | cloud computing national standards and white paper series release, and Huayun data fully participated in the preparation

Find the first k small element select_ k

Record a case of using WinDbg to analyze memory "leakage"

buuctf-pwn write-ups (9)
随机推荐
Nacos distributed transactions Seata * * install JDK on Linux, mysql5.7 start Nacos configure ideal call interface coordination (nanny level detail tutorial)
Gimp 2.10 tutorial "suggestions collection"
写作写作写作写作
Vulnhub's darkhole_ two
IDC report: Tencent cloud database ranks top 2 in the relational database market!
分享:中兴 远航 30 pro root 解锁BL magisk ZTE 7532N 8040N 9041N 刷机 刷面具原厂刷机包 root方法下载
瀚升优品app翰林优商系统开发功能介绍
英语句式参考
Sophon CE Community Edition is online, and free get is a lightweight, easy-to-use, efficient and intelligent data analysis tool
matlab内建函数怎么不同颜色,matlab分段函数不同颜色绘图
[PM2 details]
消除`if()else{ }`写法
pytorch yolov5 训练自定义数据
JVM第三话 -- JVM性能调优实战和高频面试题记录
Record eval() and no in pytoch_ grad()
生词生词生词生词[2]
Notes on common management commands of openshift
在一台服务器上部署多个EasyCVR出现报错“Press any to exit”,如何解决?
Sophon CE社区版上线,免费Get轻量易用、高效智能的数据分析工具
Simulate the hundred prisoner problem