Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

The concept and Application of SPI in JVM

Shulou Source: shulou.com Published: 2022-06-02 10:00:43 09月15日 Update

This article mainly explains "the concept and application of SPI in JVM". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn the concept and application of SPI in JVM.

Concept

Service Provider Interface

Rules

Create a file in resource/META-INF/services named after the fully qualified name of the interface, with the fully qualified name of the implementation class written on it

The API implementation class is under the classpath path

The main program dynamically loads the implementation module through java.util.ServiceLoader (scans the configuration file in the META-INF/services directory to find the implementation class, and loads it to JVM)

Benefits

Decoupling, no hard coding between the main program and the implementation class

Example package com.mousycoder.mycode.thinking_in_jvm;/** * @ version 1.0 * @ author: mousycoder * @ date: 2019-09-16 16:14 * / public interface SPIService {void execute ();} package com.mousycoder.mycode.thinking_in_jvm / * * @ version 1.0 * @ author: mousycoder * @ date: 2019-09-16 16:16 * / public class SpiImpl1 implements SPIService {@ Override public void execute () {System.out.println ("SpiImpl1.execute ()");} package com.mousycoder.mycode.thinking_in_jvm / * * @ version 1.0 * @ author: mousycoder * @ date: 2019-09-16 16:16 * / public class SpiImpl2 implements SPIService {@ Override public void execute () {System.out.println ("SpiImpl2.execute ()");}}

Create a file named com.mousycoder.mycode.thinking_in_jvm.SPIService in the resources/META-INF/services/ directory with the content com.mousycoder.mycode.thinking_in_jvm.SpiImpl1 com.mousycoder.mycode.thinking_in_jvm.SpiImpl2

Main program

Package com.mousycoder.mycode.thinking_in_jvm;import sun.misc.Service;import java.util.Iterator;import java.util.ServiceLoader;/** * @ version 1.0 * @ author: mousycoder * @ date: 2019-09-16 16:21 * / public class SPIMain {public static void main (String [] args) {Iterator providers = Service.providers (SPIService.class); ServiceLoader load = ServiceLoader.load (SPIService.class) While (providers.hasNext ()) {SPIService ser = providers.next (); ser.execute ();} System.out.println ("- -"); Iterator iterator = load.iterator (); while (iterator.hasNext ()) {SPIService ser = iterator.next () Ser.execute ();}

Output

SpiImpl1.execute () SpiImpl2.execute ()-SpiImpl1.execute () SpiImpl2.execute () so far, I believe you have a deeper understanding of "the concept and application of SPI in JVM". You might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Concept content file application main program interface directory learning loading practical deeper no between examples interest dynamic benefits practicality practicality simple operation Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Redmi Huawei Xiaomi macOS