当前位置:网站首页>[04] [02] [02] SPI mechanism
[04] [02] [02] SPI mechanism
2022-04-21 18:44:00 【Safe new driver】
List of articles
1. Basic concepts
SPI (Service Provider Interface), yes Java A set of functions provided to be implemented or extended by a third party API, It can be used to enable framework extensions and replace components

Java SPI It's actually “ Interface based programming + The strategy pattern + The configuration file ” Dynamic loading mechanism of composite implementation
2. Use scenarios
- Database driven
- journal SLF4J
- Spring Use SPI
- Dubbo Use SPI, But for the original SPI Do the packaging , Allow users to extend the implementation Filter Interface
3. Use requirement
To use SPI, The following conventions need to be followed
- SPI There must be a parameterless constructor in the implementation class of
- When the service provider provides the implementation of the interface , stay jar Bag META-INF Create a directory to “ The interface is fully qualified ” Name the file , The content is the fully qualified name of the implementation class
- JDK Middle path time META-INF/services/
- Spring The configuration in is META-INF/spring.factories
- Interface implementation class jar The package is in the main program classpath in
- adopt Loader Class will META-INF The configuration file under the directory is loaded , Resolve the fully qualified class name in the file , Load the class into JVM
- JDK It is used in ServiceLoader load META-INF/services/ The files under the
- Spring Is used in SpringFactoriesLoader load META-INF/spring.factories
4. JDK SPI Realization
Analysis of database driven source code
MySQL The driver is om.mysql.cj.jdbc It's a bag Driver Implement the interface JDK Of java.sql.Driver, And in META-INF/services The file name created in the directory is java.sql.Driver The file of , Fill in the full path information of the implementation class in the file com.mysql.cj.jdbc.Driver
// Realization JDK Of java.sql.Driver class
public class Driver extends NonRegisteringDriver implements java.sql.Driver {
// SPI There must be a parameterless constructor in the implementation class of
public Driver() throws SQLException {
}
static {
try {
DriverManager.registerDriver(new Driver());
} catch (SQLException var1) {
throw new RuntimeException("Can't register driver!");
}
}
}

see java.util.ServiceLoader Source code
public final class ServiceLoader<S> implements Iterable<S> {
private static final String PREFIX = "META-INF/services/";
// Represents the loaded class or interface
private final Class<S> service;
// Used to locate , Load and instantiate providers Class loader for
private final ClassLoader loader;
// establish ServiceLoader The access control context used when
private final AccessControlContext acc;
// cache providers, In the order of instantiation
private LinkedHashMap<String,S> providers = new LinkedHashMap<>();
// Lazy search iterator
private LazyIterator lookupIterator;
......
}
ServiceLoader Can span jar Package for META-INF/services The configuration file under the directory
try {
String fullName = PREFIX + service.getName();
if (loader == null)
configs = ClassLoader.getSystemResources(fullName);
else
configs = loader.getResources(fullName);
} catch (IOException x) {
fail(service, "Error locating configuration files", x);
}
After obtaining the full class name of the implementation class , By reflection Class.forName() Load class objects
5. Spring SPI Realization
Spring Of SPI Implementation is made by org.springframework.core.io.support Under bag SpringFactoriesLoader Realized , The configuration file path is META-INF/spring.factories And JDK There are differences
public final class SpringFactoriesLoader {
public static final String FACTORIES_RESOURCE_LOCATION = "META-INF/spring.factories";
public static <T> List<T> loadFactories(Class<T> factoryClass, @Nullable ClassLoader classLoader) {
}
private static Map<String, List<String>> loadSpringFactories(@Nullable ClassLoader classLoader) {
...
// scanning jar It's a bag META-INF/spring.factories file
Enumeration<URL> urls = (classLoader != null ?
classLoader.getResources(FACTORIES_RESOURCE_LOCATION) :
ClassLoader.getSystemResources(FACTORIES_RESOURCE_LOCATION));
...
}
}
stay Spring Of spring-data-redis Chinese vs JDK and Spring All done. SPI Realization

版权声明
本文为[Safe new driver]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204211841053070.html
边栏推荐
- 【js学习笔记四十一】单体模式
- How to detect how many cameras are plugged into the PC? And conduct multi camera synchronous recording?
- 央行明确2022年支付监管工作重点,各平台企业要注意二清风险
- 什么品牌的无线耳机质量最好?口碑好的蓝牙耳机推荐
- 无线蓝牙耳机哪个品牌好?无线蓝牙耳机推荐
- Win10 uwp asynchronous progress bar
- Daily blog - Devops whole process
- 【无标题】
- Lap library installation
- 微信QQ支付宝三合一收款二维码实现原理
猜你喜欢
![[04][02][02] SPI 机制](/img/62/c9852086e7893d392ef407c294dd1e.png)
[04][02][02] SPI 机制

Advanced mathematical formulas (Part 5)

Les produits qui ont été achetés sont de retour.

C language operator summary

CVPR 2022 | 基于Transformer的新型人脸属性编辑框架TransEditor

手把手教会你搭建组件与应用

MySQL数据库学习---第六章多表查询的课后习题

What does the fragment state in viewpager save

Serialized object + properties + IO framework

DX12渲染引擎目录
随机推荐
如何查看redis源码中的 zskiplist 结构
Best performing C language
如何用PS制作动图
【短时幅度谱】短时幅度谱估计在语音增强方面的MATLAB仿真
[JS learning notes 40] complex factory mode
什么品牌的无线耳机质量最好?口碑好的蓝牙耳机推荐
我好菜,发慌
Dx12 rendering engine directory
C language operator summary
The central bank defines the key points of payment supervision in 2022, and all platform enterprises should pay attention to clearing risks
Building local canal middleware for data migration -- Inspiration from cache breakdown
干货|app自动化测试之Appium 原理 与 JsonWP 协议分析
T-SQL language of SQL Server database
OBS access network camera
Is the securities account of qiniu app safe? Who's driving here?
What should I do for interface testing? Charles with certificate, what can I do for you?
一个简单易用的文件上传方案
Appium principle and jsonwp protocol analysis of automatic testing of dry goods app
干货|app自动化测试之Appium 源码修改定制分析
Joli langage Tommy C