zoukankan      html  css  js  c++  java
  • SPI

    SPI有两个不同的扩展名。

    第一种 :SPI(Serial Peripheral interface)串行外围设备接口(总线)。

    SPI优点

    支持全双工通信
    通信简单
    数据传输速率块

    缺点
    没有指定的流控制,没有应答机制确认是否接收到数据,所以跟IIC总线协议比较在数据 
    可靠性上有一定的缺陷。

    特点
    1):高速、同步、全双工、非差分、总线式
    2):主从机通信模式

    第二种 SPI(Service Provider Interface)服务提供接口。

    简单来讲,即通过注册接口的实现类调用对应的类。mysql-connector驱动包就是使用SPI机制。

    比如有个接口AI,然后有三个实现类A1,A2,A3.我们在META-INF文件夹下建立一个Service,然后在下面建立一个与接口同名的文件AI,在

    此文件中写入A2,A3的类路径地址。这样在开始调用时,通过ServiceLoader从Service下获取接口对应的需要载入的类路径,然后通过反射机制获取对应加载的类,

    如果不想用A2,A3,想用A1了直接修改文件即可,实现快速插拔。

    /**
     * Spi(service procedure interface)
     * @author DennyZhao
     *
     */
    public class SpiTest {
    
        public static void main(String[] args) {
            ServiceLoader<Animals> serviceLoader =  ServiceLoader.load(Animals.class);
            Iterator<Animals> iterator = serviceLoader.iterator();
            while(iterator.hasNext()) {
                Animals animals = iterator.next();
                System.out.println(animals.getAnimalName());
            }
        }
    }

    接口和实现类以及Service结构,由于ServiceLoader默认使用接口的路径和类名查找文件,因此确保文件名和接口路径一致:

    在文件中填写:

    com.test.animal.impl.Sheep
    com.test.animal.impl.Tiger

    即完成了实现类的注入。

  • 相关阅读:
    MySql 用户 及权限操作
    MAC 重置MySQL root 密码
    在mac系统安装Apache Tomcat的详细步骤[转]
    Maven:mirror和repository 区别
    ES6 入门系列
    转场动画CALayer (Transition)
    OC 异常处理
    Foundation 框架
    Enum枚举
    Invalid App Store Icon. The App Store Icon in the asset catalog in 'xxx.app' can’t be transparent nor contain an alpha channel.
  • 原文地址:https://www.cnblogs.com/DennyZhao/p/9292725.html
Copyright © 2011-2022 走看看