SPI浅析
写点笔记
共 1979字,需浏览 4分钟
·
2021-10-12 15:12
package com.example.demo.spi;
/**
* @Author tianjl
* @Date 2021/10/11 10:11
* @Discription disc
*/
public interface MySpi {
void show();
}
package com.example.demo.spi.imp;
import com.example.demo.spi.MySpi;
/**
* @Author tianjl
* @Date 2021/10/11 10:12
* @Discription disc
*/
public class MySpi1 implements MySpi {
public void show() {
System.out.println("this is myspi one~~~~");
}
}
package com.example.demo.spi.imp;
import com.example.demo.spi.MySpi;
/**
* @Author tianjl
* @Date 2021/10/11 10:12
* @Discription disc
*/
public class MySpi2 implements MySpi {
public void show() {
System.out.println("this is myspi two~~~~");
}
}
public class TestSpi {
public static void main(String[] args) {
Iterator providers= Service.providers(MySpi.class);
ServiceLoader loader=ServiceLoader.load(MySpi.class);
while (providers.hasNext()){
MySpi service=providers.next();
service.show();
}
System.out.println("------");
Iterator iterator=loader.iterator();
while (iterator.hasNext()){
MySpi service=iterator.next();
service.show();
}
}
}
午安~
评论