Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The concept and Application of SPI in JVM

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report