当前位置:网站首页>@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
边栏推荐
- [performance test] full link voltage test
- About Statistical Power(统计功效)
- ClickHouse(03)ClickHouse怎么安装和部署
- 小林coding的内存管理章节
- 第十届全球云计算大会 | 华云数据荣获“2013-2022十周年特别贡献奖”
- 破解湖+仓混合架构顽疾,星环科技推出自主可控云原生湖仓一体平台
- 热通孔的有效放置如何改善PCB设计中的热管理?
- [TestLink] testlink1.9.18 solutions to common problems
- [QNX Hypervisor 2.2用户手册]6.3.2 配置VM
- nacos -分布式事务-Seata** linux安装jdk ,mysql5.7启动nacos配置ideal 调用接口配合 (保姆级细节教程)
猜你喜欢

U-Net: Convolutional Networks for Biomedical Images Segmentation
![含重复元素取不重复子集[如何取子集?如何去重?]](/img/b2/d019c3f0b85a6c0d334a092fa6c23c.png)
含重复元素取不重复子集[如何取子集?如何去重?]

nano的CAN通信

Nanjing University: Discussion on the training program of digital talents in the new era

ISPRS2022/雲檢測:Cloud detection with boundary nets基於邊界網的雲檢測

记一次使用Windbg分析内存“泄漏”的案例

Simulate the hundred prisoner problem

第十届全球云计算大会 | 华云数据荣获“2013-2022十周年特别贡献奖”

Memory management chapter of Kobayashi coding

Let more young people from Hong Kong and Macao know about Nansha's characteristic cultural and creative products! "Nansha kylin" officially appeared
随机推荐
Notes on common management commands of openshift
Copy the linked list with random pointer in the "Li Kou brush question plan"
[JMeter] advanced writing method of JMeter script: all variables, parameters (parameters can be configured by Jenkins), functions, etc. in the interface automation script realize the complete business
Record a case of using WinDbg to analyze memory "leakage"
[TestLink] testlink1.9.18 solutions to common problems
JVM third talk -- JVM performance tuning practice and high-frequency interview question record
LeetCode笔记:Weekly Contest 300
Matlab built-in function how different colors, matlab subsection function different colors drawing
Tupu software digital twin | visual management system based on BIM Technology
Record eval() and no in pytoch_ grad()
第十届全球云计算大会 | 华云数据荣获“2013-2022十周年特别贡献奖”
ConvMAE(2022-05)
金太阳开户安全吗?万一免5开户能办理吗?
Writing writing writing
[use electron to develop desktop on youqilin]
星环科技数据安全管理平台 Defensor重磅发布
记录Pytorch中的eval()和no_grad()
南京大学:新时代数字化人才培养方案探讨
《2022中国信创生态市场研究及选型评估报告》发布 华云数据入选信创IT基础设施主流厂商!
ClickHouse(03)ClickHouse怎么安装和部署