zoukankan      html  css  js  c++  java
  • Java的SPI机制浅析与简单示例

    一、SPI机制

            这里先说下SPI的一个概念,SPI英文为Service Provider Interface单从字面可以理解为Service提供者接口,正如从SPI的名字去理解SPI就是Service提供者接口;我对SPI的定义:提供给服务提供厂商与扩展框架功能的开发者使用的接口。

           在我们日常开发的时候都是对问题进行抽象成Api然后就提供各种Api的实现,这些Api的实现都是封装与我们的Jar中或框架中的虽然当我们想要提供一种Api新实现时可以不修改原来代码只需实现该Api就可以提供Api的新实现,但我们还是生成新Jar或框架(虽然可以通过在代码里扫描某个目录已加载Api的新实现,但这不是Java的机制,只是hack方法),而通过Java SPI机制我们就可以在不修改Jar包或框架的时候为Api提供新实现。

         很多框架都使用了java的SPI机制,如java.sql.Driver的SPI实现(MySQL驱动、oracle驱动等)、common-logging的日志接口实现、dubbo的扩展实现等等框架;

    SPI机制的约定:

    1)         在META-INF/services/目录中创建以接口全限定名命名的文件该文件内容为Api具体实现类的全限定名

    2)         使用ServiceLoader类动态加载META-INF中的实现类

    3)         如SPI的实现类为Jar则需要放在主程序classPath中

    4)         Api具体实现类必须有一个不带参数的构造方法

                                    SPI机制结构图

    二、SPI机制示例

                            实例结构图

    IOperation接口:

    [html] view plain copy
     
    1. package org.nercita.ltxx.spiTest;  
    2. /**  
    3.  * this is a Interface for two data   
    4.  * @author zhangwenchao  
    5.  *  
    6.  */  
    7. public interface IOperation {  
    8.      public int operation(int numberA, int numberB);  
    9. }  

    SPI接口的实现类:PlusOperationImpl

    [html] view plain copy
     
    1. package org.nercita.ltxx.spiTest;  
    2.   
    3. public class PlusOperationImpl implements IOperation {  
    4.     public int operation(int numberA, int numberB) {  
    5.   
    6.         return numberA + numberB;  
    7.   
    8.     }  
    9.   
    10. }  

    SPI接口的实现类:DivisionOperationImpl

    [html] view plain copy
     
    1. package org.nercita.ltxx.spiTest;  
    2.   
    3. public class DivisionOperationImpl implements IOperation{  
    4.     public int operation(int numberA, int numberB) {  
    5.   
    6.         return numberA / numberB;  
    7.   
    8.     }  
    9. }  

    META-INF/Services目录中的文件:

    文件名:org.nercita.ltxx.spiTest.IOperation

    内容:

           org.nercita.ltxx.spiTest.DivisionOperationImpl
           org.nercita.ltxx.spiTest.PlusOperationImpl

    测试工程引入上述jar包,测试Main类如下:

    [html] view plain copy
     
    1. package org.nercita.itxx.spiClient;  
    2.   
    3. import java.util.Iterator;  
    4. import java.util.ServiceLoader;  
    5.   
    6. import org.nercita.ltxx.spiTest.DivisionOperationImpl;  
    7. import org.nercita.ltxx.spiTest.IOperation;  
    8. import org.nercita.ltxx.spiTest.PlusOperationImpl;  
    9.   
    10. public class Main {  
    11.       
    12.     public static void main(String[] args) {  
    13.         IOperation plus = new PlusOperationImpl();  
    14.   
    15.         IOperation division = new DivisionOperationImpl();  
    16.   
    17.         System.out.println(plus.operation(6, 3));  
    18.   
    19.         System.out.println(division.operation(6, 3));  
    20.   
    21.         ServiceLoader<IOperationoperations = ServiceLoader.load(IOperation.class);  
    22.   
    23.         Iterator<IOperationoperationIterator = operations.iterator();  
    24.   
    25.         System.out.println("classPath:"+System.getProperty("java.class.path"));  
    26.   
    27.         while (operationIterator.hasNext()) {  
    28.   
    29.             IOperation operation = operationIterator.next();  
    30.   
    31.             System.out.println(operation.operation(6, 3));  
    32.   
    33.         }  
    34.     }  
    35.   
    36. }  

    运行结果:

    9
    2
    classPath:D:WorkspacesprojectsspiClientin;D:WorkspacesprojectsspiClientlibspiTest-0.0.1-SNAPSHOT.jar
    2
    9

  • 相关阅读:
    VS的代码分析工具
    时间管理-SMART原则
    时间管理-该怎样进行时间管理
    ASP.NET Identity V2
    SQL Server 事务隔离级别的查看及更改
    GAC(Global Assembly Cache)注册/卸载 dll
    基于小米即时消息云服务(MIMC)的Web IM
    Spring Boot MyBatis配置多种数据库
    解决easyui combobox赋值boolean类型的值时,经常出现的内容显示的value而不是text的bug
    thymeleaf-extras-db 0.0.1发布,select标签加载数据的新姿势
  • 原文地址:https://www.cnblogs.com/fengmao/p/8487267.html
Copyright © 2011-2022 走看看