当前位置:网站首页>@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
边栏推荐
- [paddlepaddle] paddedetection face recognition custom data set
- 写作写作写作写作
- [PM2 details]
- 个人对卷积神经网络的理解
- Sophon Base 3.1 推出MLOps功能,为企业AI能力运营插上翅膀
- Sophon autocv: help AI industrial production and realize visual intelligent perception
- 让更多港澳青年了解南沙特色文创产品!“南沙麒麟”正式亮相
- 从类生成XML架构
- Memory management chapter of Kobayashi coding
- 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
猜你喜欢
Nanjing University: Discussion on the training program of digital talents in the new era
Thoroughly understand why network i/o is blocked?
nano的CAN通信
Sophon CE社区版上线,免费Get轻量易用、高效智能的数据分析工具
Whether to take a duplicate subset with duplicate elements [how to take a subset? How to remove duplicates?]
图片数据不够?我做了一个免费的图像增强软件
Sophon kg upgrade 3.1: break down barriers between data and liberate enterprise productivity
图扑软件数字孪生 | 基于 BIM 技术的可视化管理系统
Sophon CE Community Edition is online, and free get is a lightweight, easy-to-use, efficient and intelligent data analysis tool
Tencent music launched its new product "quyimai", which provides music commercial copyright authorization
随机推荐
检查命名空间和类
音视频包的pts,dts,duration的由来.
图像分类,看我就够啦!
matlab内建函数怎么不同颜色,matlab分段函数不同颜色绘图
LeetCode笔记:Weekly Contest 300
[use electron to develop desktop on youqilin]
开户注册股票炒股安全吗?有没有风险的?靠谱吗?
《2022中国信创生态市场研究及选型评估报告》发布 华云数据入选信创IT基础设施主流厂商!
寻找第k小元素 前k小元素 select_k
GIMP 2.10教程「建议收藏」
Tupu software digital twin | visual management system based on BIM Technology
Let more young people from Hong Kong and Macao know about Nansha's characteristic cultural and creative products! "Nansha kylin" officially appeared
星环科技重磅推出数据要素流通平台Transwarp Navier,助力企业实现隐私保护下的数据安全流通与协作
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
Use JMeter to record scripts and debug
Electron installation problems
Is it safe to open an account, register and dig money? Is there any risk? Is it reliable?
Easynmon Usage Summary
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!
@Extension、@SPI注解原理