Get the App
SLTechnology News&Howtos  ›  Development  › 

How does java SPI define an interface

Shulou Source: shulou.com Published: 2022-05-31 17:50:36 09月10日 Update

This article mainly introduces the relevant knowledge of java SPI how to define the interface, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value. I believe you will gain something after reading this article on how to define the interface in java SPI. Let's take a look.

instructions

After the service provider provides the specific implementation of the interface, create a file named with the fully qualified name of the interface in the META-INF/services directory of the jar package, which is the fully qualified name of the implementation class.

1. The jar package of the interface implementation class is placed in the classpath of the main program. The main program dynamically loads the implementation module through java.util.ServiceLoder, finds the fully qualified name of the implementation class by scanning the configuration file in the META-INF/services directory, and loads the class into JVM.

2. The implementation class of SPI must carry a structure method without parameters.

Example

Public interface IShout {void shout ();} public class Cat implements IShout {@ Override public void shout () {System.out.println ("miao miao");}} public class Dog implements IShout {@ Override public void shout () {System.out.println ("wang wang");}} this is the end of the article on "how java SPI defines interfaces". Thank you for reading! I believe you all have a certain understanding of the knowledge of "how to define the interface of java SPI". If you want to learn more, you are welcome to follow the industry information channel.

Tags: Interface content knowledge main program file directory article value usage description dynamic parameter instance provider easy to operate article method easy to understand more module see Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Huawei MySQL MariaDB Redmi