当前位置:网站首页>@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
边栏推荐
- Maximum artificial island [how to make all nodes of a connected component record the total number of nodes? + number the connected component]
- [use electron to develop desktop on youqilin]
- How to solve the error "press any to exit" when deploying multiple easycvr on one server?
- 记一次使用Windbg分析内存“泄漏”的案例
- How to improve the thermal management in PCB design with the effective placement of thermal through holes?
- 使用JMeter录制脚本并调试
- About Estimation with Cross-Validation
- The 2022 China Xinchuang Ecological Market Research and model selection evaluation report released that Huayun data was selected as the mainstream manufacturer of Xinchuang IT infrastructure!
- Nanjing University: Discussion on the training program of digital talents in the new era
- 消除`if()else{ }`写法
猜你喜欢
Maximum artificial island [how to make all nodes of a connected component record the total number of nodes? + number the connected component]
如何获取飞机穿过雷达两端的坐标
buuctf-pwn write-ups (9)
nano的CAN通信
Wu Enda team 2022 machine learning course, coming
Le cours d'apprentissage de la machine 2022 de l'équipe Wunda arrive.
Tupu software digital twin | visual management system based on BIM Technology
Record eval() and no in pytoch_ grad()
Cmake tutorial step1 (basic starting point)
ISPRS2022/雲檢測:Cloud detection with boundary nets基於邊界網的雲檢測
随机推荐
Sophon CE Community Edition is online, and free get is a lightweight, easy-to-use, efficient and intelligent data analysis tool
Sophon autocv: help AI industrial production and realize visual intelligent perception
从类生成XML架构
Check namespaces and classes
吳恩達團隊2022機器學習課程,來啦
Vulnhub's darkhole_ two
How to solve the error "press any to exit" when deploying multiple easycvr on one server?
图片数据不够?我做了一个免费的图像增强软件
Is it safe to open an account and register stocks for stock speculation? Is there any risk? Is it reliable?
彻底理解为什么网络 I/O 会被阻塞?
Find the first k small element select_ k
【PaddleClas】常用命令
星环科技重磅推出数据要素流通平台Transwarp Navier,助力企业实现隐私保护下的数据安全流通与协作
Thoroughly understand why network i/o is blocked?
FCN: Fully Convolutional Networks for Semantic Segmentation
ConvMAE(2022-05)
JDBC reads a large amount of data, resulting in memory overflow
To solve the stubborn problem of Lake + warehouse hybrid architecture, xinghuan Technology launched an independent and controllable cloud native Lake warehouse integrated platform
How can cluster deployment solve the needs of massive video access and large concurrency?
The 2022 China Xinchuang Ecological Market Research and model selection evaluation report released that Huayun data was selected as the mainstream manufacturer of Xinchuang IT infrastructure!