"What is Java SPI? What's the use?"
This is a real problem encountered in the second interview during the Ali p6 interview.
If you don't understand SPI, I suggest you watch the entire video.
Hello everyone, I'm Mic, a Java programmer who has been working for 14 years
I have organized the text version of this interview question in a 200,000-word document. If you need it, you can get it at the end of the article.
Let's look at the purpose of this investigation
Inspection target
The difficulty of this question is medium. For students who have not studied the source code very much, SPI is a very unfamiliar concept.
The survey population is mainly 3 to 5 years old.
3 to 5 years belong to the middle and high-end Java development crowd, so the purpose of the inspection is also obvious:
- Find out how well the job seeker understands the technical field
- Talent selection for advanced development
There is no talent rating standard in the Java industry, so it is difficult for the interviewer to define your rank during the interview.
Therefore, in Internet companies, the technical inspection will be more in-depth.
So, in order to answer this question, you still have to have some opinions of your own.
Problem Analysis
Java SPI, the full name is Service Provider Interface.
It is an interface-based dynamic extension mechanism, which is equivalent to a set of interfaces provided in Java.
Then the third party can implement this interface to complete the extension and implementation of the function.
Take a simple example.
In the Java SDK, a database-driven interface java.sql.Driver is provided.
Its role is to provide access to the database.
However, there is no implementation provided in Java, because different database vendors have different syntax and implementation.
Therefore, it can only be implemented by third-party database vendors. For example, Oracle is oracle.jdbc.OracleDriver, and mysql is com.mysql.jdbc.Driver.
Then when the application is developed, it is connected to the corresponding database according to the integrated driver.
The main idea of the SPI mechanism in Java is to move the control of the assembly outside the program to achieve decoupling of standards and implementations, and to provide dynamic pluggable capabilities. This idea is very important in the establishment of modularization.
To implement Java SPI, several basic formats need to be met:
- You need to define an interface first as an extension standard
- Create the META-INF/service file directory in the classpath directory
- In this directory, a configuration file named with the fully qualified name of the interface, the content of the file is the implementation class of this interface
In the application, using ServiceLoad, you can find all the extension times of the classpath according to the interface name
Then select the invocation of the implementation class completion function according to the context scenario.
Java SPI has certain shortcomings. For example, it cannot load extension implementations according to requirements. All implementation classes of the extension interface are loaded and instantiated each time. Instantiation will cause performance overhead, and some implementations that are not needed are loaded. class, will lead to a waste of memory resources.
Expert:
Java SPI is an interface extension mechanism provided in Java.
I think it does two things:
- Separating standard definitions and interface implementations to achieve good decoupling in modular development
- Realize the expansion of functions to better meet the needs of customization
In addition to Java's SPI, there are many extension implementations based on the SPI idea, such as SpringFactoriesLoader in Spring.
The ExtensionLoader in Dubbo, and Dubbo has been further optimized on the basis of SPI, providing activation extension points and adaptive extension points.
Summarize
Do you know how to answer?
If you like my work, remember to like and subscribe! ! !
Copyright notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless otherwise stated. Please indicate the source ofMic带你学架构
!
If this article is helpful to you, please help to follow and like, your persistence is the driving force for my continuous creation. Welcome to follow the WeChat public account of the same name to get more technical dry goods!
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。