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

Example Analysis of Java SPI Mechanism

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

In this issue, the editor will bring you an example analysis of the Java SPI mechanism. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

In the usual development project process, I believe many readers have seen such a directory, / META-INF/services directory, the file name of this directory is the full name of the interface, its content is the specific interface implementation. This is the use of SPI mechanism. Such as:

Mysql-connector

Nio SPI in JDK

For example, logback-classic

Next, let's talk about the java SPI mechanism.

I. SPI concept and specification

1.1 SPI concept

SPI, whose full name is Service Provicder Interface, is a built-in service of JDK that provides discovery capabilities and a mechanism to dynamically replace discovery. For example, to dynamically add an implementation to an interface at run time, you only need to add an implementation. For example, in the database driver module of JDBC, different database connection drivers have the same interface but different implementation classes. Usually, major manufacturers (such as Mysql, Oracle) will develop their own driver implementation logic according to a unified specification (java.sql.Driver). When the client uses jdbc, there is no need to change the code, just introduce different spi interface services.

1.2 SPI specification

Using SPI also needs to follow certain specifications, which mainly include the following points:

/ META-INF/ directory needs to be set up

/ META-INF/services

Put it under classpath

Place configuration files in / META-INF/services/ directory

The file name is the full path name of the interface

Inside the file is the interface implementation class to be implemented

The file is encoded as UTF-8

Using the load method of ServiceLoad

II. SPI example

Write a GreetingService interface

Package com.wangmengjun.tutorial.spi

Public interface GreetingService {

Void sayHello ();}

Write two implementation classes that output English and Chinese respectively

Package com.wangmengjun.tutorial.spi.impl

Import com.wangmengjun.tutorial.spi.GreetingService

Public class EnglishGreetingServiceImpl implements GreetingService {

Public void sayHello () {System.out.println ("Hello, This is SPI");}

} package com.wangmengjun.tutorial.spi.impl

Import com.wangmengjun.tutorial.spi.GreetingService

Public class ChineseGreetingServiceImpl implements GreetingService {

Public void sayHello () {System.out.println ("Hello, this is SPI");}

}

Create the META-INF file directory and set the implementation class

Use ServiceLoader

Package com.wangmengjun.tutorial.spi

Import java.util.Iterator;import java.util.ServiceLoader

Public class SpiMain {

Public static void main (String [] args) {ServiceLoader loader= ServiceLoader.load (GreetingService.class); Iterator greetingIter = loader.iterator (); while (greetingIter.hasNext ()) {GreetingService service= greetingIter.next (); System.out.println (service.getClass (). GetName ()); service.sayHello ();}

Output:

Com.wangmengjun.tutorial.spi.impl.EnglishGreetingServiceImplHello, This is SPIcom.wangmengjun.tutorial.spi.impl.ChineseGreetingServiceImpl. Hello, this is SPI.

After a few steps above, a simple example of spi is complete.

When the ServiceLoader instance is constructed by executing ServiceLoader.load (GreetingService.class), we can see that the value of lookupIterator1 is still null. The implementation class information in the configuration file has not been read at this time. / / The lazy-lookup iterator for iterator operations private Iterator lookupIterator1; private final List instantiatedProviders = new ArrayList ()

When you use an iterator to traverse, the corresponding configuration file is read to parse, and when the hasNext method is called, the configuration file is loaded for parsing. File reading uses BufferedReader's readLine to read and parse.

As you can see from the above example, although ServiceLoader is a lazy load used, it needs to be obtained through iterator iteration, and all configured implementation classes are instantiated. If you don't want to use some implementation class, it's also loaded and instantiated, which is wasteful.

The above is the example analysis of the Java SPI mechanism shared by the editor. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

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

Development

Wechat

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

12
Report