In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how to analyze the Java SPI mechanism. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.
Why do I need SPI?
Consider a scenario where we encapsulate a set of services, and others can use these interfaces API to complete the corresponding operations by introducing the packages we have written. This is not a problem, but there will be entities using the service with different business requirements and need to be further expanded, but because the api is written, it is not so easy to expand. What should we do if there is such a scenario?
You can use the SPI mechanism provided by Java
What is SPI? The difference between SPI and API
SPI
The full name of SPI is Service Provider Interface, which is a mechanism provided by Java that can be used for third-party implementation and extension. Through this mechanism, we can achieve decoupling, the SPI interface is responsible for defining and providing the default implementation, and SPI callers can extend as needed.
The full name of API is Application Programming Interface, and in a broad sense, it is the interface, which is responsible for the channel of cooperation between programs, just like the example given above. [we have packaged a set of services and provided them to others in the form of API, and others can get what they want using API]
So the difference between the two is obvious. The caller of API can only rely on the implementation of the provider. SPI is like a customizable API. Callers can customize the implementation to replace the default implementation provided by API.
Come on, have a little confrontation.
First, let's create an empty maven project with two packages in it.
Spi-provider can tell by name that it is the provider of SPI.
The user of spi-user SPI
Spi-provider
We simply define a SPI interface, which is called ISpiTest, with a saySomething method in it, and provide a default implementation
Public interface ISpiTest {void saySomething ();} public class DefaultSpiImplementation implements ISpiTest {@ Override public void saySomething () {System.out.println ("[default implementation]-> Today is also a promising day");}}
Then, simulate the process and note that step 4 is the one we want to customize and replace later.
/ * simulate a set of processes * @ author Amg * @ date 2021-12-9 * / public class TestUtils {public static void workFlow (ISpiTest s) {System.out.println ("1, step 1."); System.out.println ("2, step 2.") System.out.println ("3, step 3."); System.out.print ("4, step 4:"); s.saySomething (); System.out.println ("5, step 5.");}}
Next, the point comes, we need to create a / META-INF/services folder under the resources directory, and then create a file with the fully qualified class name of the SPI interface
Fill in the fully qualified class name of the implementation class in the file, as follows
Com.amg.spi.DefaultSpiImplementation
At this point, the spi-provider module is completed. As for how to use it later, it will be further explained in the spi-user module.
Spi-user
First of all, we introduce the spi-provider coordinate dependency in the pom file
Then define the main method and call the SPI interface defined in spi-provider in the main method, using the default configuration
You can notice that we use the load method of the class ServiceLoader to pass in the bytecode of the SPI interface to construct. We give a default implementation in resources in spi-provider, but we call it in spi-user. ServiceLoader will automatically read the configuration file under META-INF, even across jar packages.
Then let's define an implementation class in spi-user and configure it under META-INF (note that the fully qualified class name of this configuration still needs to be the path where the SPI interface is defined in spi-provider).
The contents of META-INF under spi-user are as follows
Com.amg.spiuser.service.impl.WantHamburger
It can be found that we have not changed any client code, but simply modified the configuration file to complete the custom implementation. This is the charm of using SPI.
? Think about how we did it before.
First of all, an interface is defined, and the interface-oriented programming defines each custom implementation class of the configuration file. You only need to rewrite the configuration file according to the rules.
SPI is an embodiment of policy pattern. With the idea of interface-oriented programming and necessary configuration files, the definition and implementation can be decoupled, and it is a customizable API.
SPI has the following advantages
Realization of interface decoupling by customization
The disadvantages of SPI are as follows
By observing ServiceLoader, we can find that there is no additional locking mechanism, so there is a concurrency problem that the corresponding implementation classes are not flexible enough. As you can see from the above example, you need to use iterators to get all the specific implementation classes that need to know the interface, so you have to load and instantiate all the implementation classes every time.
This is the end of the analysis on how to carry out the Java SPI mechanism. I hope the above content can be of some help to you and learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.