This is the 11th day of my participation in Gwen Challenge

Today, 4ye will share this SPI mechanism with your friends. SPI is everywhere! Is one of the embodiment of hot plug, more and parental delegation mechanism has a little relationship, is a spoiler ๐Ÿ˜œ

preface

This section introduces the SPI mechanism in Java.

Springboot SPI mechanism we will talk about in the following Springboot automatic assembly ~ ๐Ÿ˜ hey hey

As for [[Dubbo’s SPI mechanism]], I haven’t had time to get into it, but I simply know its SPI adaptive extension mechanism, and the following extensions ~ (super multi-extension ~) ๐Ÿท


One unit!

What is SPI?

SPI, or Service Provider Interface, is a Service discovery mechanism. It automatically loads the classes defined in the file by looking for them in the meta-INF /services folder on the ClassPath path.

It is a set of interfaces provided by Java that can be implemented or extended by third parties

JAVA SPI practice

Steps:

  1. Define an interface and its implementation class

  2. Create a new plain text folder named the full name of the interface, and add the full name of the implementation class of the interface to the text

  3. Finally, call the Serviceloader.load method in your code

Project structure drawing

Spi file contents

The source code is as follows

interface

public interface IJava4ye {

    void say(a);

}

public class Java4yeImpl implements IJava4ye{

    @Override
    public void say(a) {
        System.out.println([Java4ye] Jay Jay!); }}Copy the code

The test class

public class SpiTest {

    @Test
    public void spiTest(a){
        ServiceLoader<IJava4ye> load = ServiceLoader.load(IJava4ye.class);

        for(IJava4ye iJava4ye : load) { iJava4ye.say(); }}}Copy the code

The results of

Is not very convenient, and the feeling of IOC ha ha ๐Ÿ˜„

JDBC instance

So practice, let’s take a look at JDBC this small case ๐Ÿ˜

You can see an example of this in the MySQL JDBC package we use ~๐Ÿท

The java.sql.Driver file defines an implementation class ~๐Ÿ‘‡

com.mysql.cj.jdbc.Driver
Copy the code

Driver source code

You can see that there is a static code block that registers the Driver Driver directly into DriverManager as the class is loaded.

What does that do?

A quick look at the JDBC notation shows that previously we had to write this manually

Class. ForName to load the drive, but with SPI, you can omit this line of code ~๐Ÿ˜„ haha ๐Ÿท

Later we defines what all need not tube manufacturers, direct use of DriverManager. GetConnection (url, username, password); You can get this connection, the code will not appear in the hard coding ~ much more flexible ๐Ÿท (of course, do not develop the framework or their own work seems not to use it ๐Ÿ˜ ha ha ha)

JAVA SPI principle exploration

confusion

This SPI must be loaded using serviceloader. load.

I don’t know if 4ye is haha, but that’s how ๐Ÿ˜ is used in DriverManager

Please see ๐Ÿ‘‡ below for details

Break the parent delegate mechanism

We all know that there is a parent delegate mechanism in Java, and doing something like DriverManager above breaks the parent delegate mechanism.

Because this implementation class belongs to the third party class library mysql

The Driver interface is in the JDK’s own lib — rt.jar

It is loaded by the root class loader, BootstrapClassloader, while its implementation class is in a third-party package, so BootstrapClassloader cannot load.

So how do you load classes in third-party packages?

Just look at the ServiceLoader class ~ ๐Ÿ˜

ServiceLoader source

You can see that there is a very prominent sentence here

ClassLoader cl = Thread.currentThread().getContextClassLoader();
Copy the code

Load through the context classloader of the current thread!

Which classLoader is used in the context classLoader of the current thread?

After searching the source code, we see a code in the Launcher Launcher that assigns AppClassLoader to the ClassLoader variable and sets it to the current thread. ๐Ÿ˜‹

SPI uses the application classloader to load classes in third-party packages.

conclusion

Java provides this service discovery mechanism SPI, which can be easily extended to a certain interface to achieve hot plug module, but the disadvantage is that flexibility is not high, the number of implementation classes in the configuration file, will be loaded into memory, whether or not used ~๐Ÿท

Schematic diagram is attached

The last

Welcome friends to discuss the question ~

If you think this article is good, please give it a thumbs-up ๐Ÿ˜

Let’s start this unexpected meeting! ~

Welcome to leave a message! Thanks for your support! ใƒพ(โ‰งโ–ฝโ‰ฆ*)o go!!

I’m 4ye and I’ll see you soon next time!!