Java in the SPI

SPI is a service provider interface shorthand. This article will explain what to do next SPI is used, and how to use. SPI this thing is still very easy to use.

What is SPI

SPI is mainly to solve the coupling problem. We know oriented programming interface is a good way of programming, Java will be defined in advance some of the specifications, a number of interfaces, and implementation of the interface can be implemented by a third party, as long as they follow the interface specification can be. For example, we are more familiar with JDBC is the case.

SPI mentioned general class loader must mention ClassLoader. We know that the JVM is composed of three class loader, BootstrapClassLoader, ExtClassLoader, AppClassLoader. Their parents adopt delegated mode. Meanwhile JDK also suggest that we use this method in the class load time. But it can cause problems if the SPI can not find the class this way.

Simple JDBC class to explain the use of delegated parents modes:

  • First of all, we know that the interface is a Java JDBC core package, in rt.jar, this jar is BootstrapClassLoadre be loaded.
  • Secondly, SPI loading implement rules to find the corresponding class files in accordance with the services jar package META-INF. In the services under the META-INF defines a file, the file name is a full type interface class, and the contents of the file is the full class name of the implementation class.
  • Again, to achieve loaded class is in the DriverManager to call, by Class.forName to load the implementation class, and class Class.forName using the current class loader, the current class of the class loader is BootstrapClassLoader, we know BootstrapClassLoader default rt.jar the load. Third-party implementation is not obvious rt.jar
  • Finally, how to solve this problem, SPI is using ContextClassLoader to load third-party implementation class, thus avoiding the parent ClassLoader-BootstrapClassLoader to be loaded by loading child ClassLoader-AppClassLoader class. The ContextClassLoader is designed to solve this problem.

SPI how to use.

  • First, implement interfaces

  • Second, get hold of interface implementation class

  • SPI need to build a file in the services of a jar of META-INF, SPI implementation class is to find the basis of this document.

文件名:接口的全类名
文件内容: 实现类的全类名
复制代码
  • Finally, by loading the ServiceLoader, oriented programming interface, the purpose of decoupling.
Hello hello = null;
ServiceLoader<Hello> serviceLoader = ServiceLoader.load(Hello.class);
Iterator<Hello> iter = serviceLoader.iterator();
if (iter.hasNext()) {
    hello = iter.next();
}
System.out.println(hello.say());
复制代码

to sum up

Through the above description, see SPI should be a lot of useful definition interface specification, and implementation of the interface may be implemented by various third parties, while in the process of programming can also be oriented programming interface, write code that It is very elegant.

Guess you like

Origin juejin.im/post/5df1d3eff265da33ed411c5d
SPI