Java's SPI mechanism experience

Source: Internet
Author: User

First of all, say something.

Yesterday in the look at the JDBC source code when see Drivermanage.getconnection () This method, click into the Drivermanage class see Getconnection () method of the core statement does the following for (Driverinfo Adriver:registereddrivers) {
Connection con = aDriver.driver.connect (URL, info);
Where Driver is a member variable of the Driver type in the Driverinfo class, and Driver (Java.sql.Driver) is an interface, the Connect method that points in is just a method defined in the Driver interface, and there is no implementation class. Where did the implementation class go??? (though I happened to find it in the Com.mysql.fabric.jdbc bag, I didn't know the rationale).

Today in class happened that the teacher mentioned the SPI mechanism, instantly feel and their own problems fully in line with, a small study of their own summary understanding as follows:

1 Why driver is an interface:

Java.sql.Driver interface is a Java externally exposed to a loading driver interface, Java is not implemented, as for the implementation of this interface by the various JDBC vendors to implement the line, the benefits of decoupling, making it more flexible, of course, this is one of the benefits of object-oriented.

In other words, the Java EE is just a standard, just an architecture. The real implementation is provided by different providers.

2 How to find the corresponding implementation class: Under the Mysql-connector-java-5.1.38.jar package under the Meta-inf.services package there is a java.sql.Driver file open file has the following two lines

Com.mysql.jdbc.Driver

Com.mysql.fabric.jdbc.FabricMySQLDriver

Where the Com.mysql.jdbc.Driver class is the implementation of the Java.sql.Driver interface, the following com.mysql.fabric.jdbc.FabricMySQLDriver is the real implementation class,

Specific processes such as:

3 What is the principle:

When the provider of the service provides an implementation of the service interface, a file named after the service interface is created in the meta-inf/services/directory of the jar package. The file is the implementation class that implements the service interface. And when the external program assembles this

module, you can find the specific implementation class name through the configuration file in the jar package meta-inf/services/, and load the instantiation to complete the injection of the module.

Based on such a convention, it is very good to find the implementation class of the service interface without having to make it in code.

A tool class for the JDK to provide service implementation lookups: Java.util.ServiceLoader

Java's SPI mechanism experience

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.