This is the sixth day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021
preface
Some of the open source components we use, such as Dubbo, shardingJDBC, etc., provide some extensible apis. For example, the custom primary key generation strategy in shardingJDBC introduced earlier is the API that implements the extension in JavaSPI.
SPI is introduced
SPI stands for Service Provider Interface. It is a set of apis provided by Java that can be implemented or extended by third parties. It can be used to enable framework extensions and replace components.
The Java SPI is actually a dynamic loading mechanism implemented by a combination of interface-based programming + policy pattern + configuration files.
The use of SPI mechanisms in common open source frameworks
Application scenario, the caller enables, extends, or replaces the implementation strategy of the framework based on actual usage needs
-
JDBC loads drivers for different types of databases
-
SLF4J loads logging implementation classes from different vendors
-
SPI is used extensively in Spring, such as: For implementation of ServletContainerInitializer servlet3.0 specification, automatic Type Conversion Type Conversion SPI (Converter SPI, the Formatter SPI), etc
-
Dubbo also makes extensive use of SPI extensions to the framework, but it encapsulates the native SPI provided by Java and allows users to extend the Filter interface
-
ShardingJDBC User-defined primary key generation policy.