当前位置:网站首页>@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
边栏推荐
- 开户注册挖财安全吗?有没有风险的?靠谱吗?
- [paddleclas] common commands
- nano的CAN通信
- Can communication of nano
- matlab内建函数怎么不同颜色,matlab分段函数不同颜色绘图
- 图像分类,看我就够啦!
- vulnhub之darkhole_2
- [QNX hypervisor 2.2 user manual]6.3.2 configuring VM
- Sibling components carry out value transfer (there is a sequence displayed)
- U-Net: Convolutional Networks for Biomedical Images Segmentation
猜你喜欢
Sophon Base 3.1 推出MLOps功能,为企业AI能力运营插上翅膀
LeetCode 6111. 螺旋矩阵 IV
@Extension、@SPI注解原理
使用JMeter录制脚本并调试
Cmake tutorial step1 (basic starting point)
图扑软件数字孪生 | 基于 BIM 技术的可视化管理系统
About statistical power
彻底理解为什么网络 I/O 会被阻塞?
U-Net: Convolutional Networks for Biomedical Images Segmentation
FCN: Fully Convolutional Networks for Semantic Segmentation
随机推荐
【在優麒麟上使用Electron開發桌面應】
Sibling components carry out value transfer (there is a sequence displayed)
Vulnhub's darkhole_ two
The 10th global Cloud Computing Conference | Huayun data won the "special contribution award for the 10th anniversary of 2013-2022"
U-Net: Convolutional Networks for Biomedical Images Segmentation
Sophon base 3.1 launched mlops function to provide wings for the operation of enterprise AI capabilities
Copy the linked list with random pointer in the "Li Kou brush question plan"
Thoroughly understand why network i/o is blocked?
Nacos distributed transactions Seata * * install JDK on Linux, mysql5.7 start Nacos configure ideal call interface coordination (nanny level detail tutorial)
Sophon kg upgrade 3.1: break down barriers between data and liberate enterprise productivity
最大人工岛[如何让一个连通分量的所有节点都记录总节点数?+给连通分量编号]
Electron installation problems
Maximum artificial island [how to make all nodes of a connected component record the total number of nodes? + number the connected component]
小白入门NAS—快速搭建私有云教程系列(一)[通俗易懂]
ConvMAE(2022-05)
[paddlepaddle] paddedetection face recognition custom data set
小林coding的内存管理章节
Use of print function in MATLAB
第十一届中国云计算标准和应用大会 | 云计算国家标准及白皮书系列发布 华云数据全面参与编制
Eliminate the writing of 'if () else{}'