标签:http interface width idt img 引用 lib man target
spi是jdk内置功能,dubbo也用了spi机制,我目前觉着,说白了就是多态思想,我定义个接口 ,这个是规范,具体实现你们来整,我不管。
先定一个规范发布项目:
代码:
package com.liaoyang; public interface DriverInteface { void sayHello(String msg); }
再弄个项目,这个项目是来实现规范的
这里能实现spi_driver的接口,是因为把他的项目在maven里引用过来了,这里不写pom了
接下来,需要创建目录,写个文件,文件里面写上实现类的全路径
然后去规范方去试试,要在规范方引入实现方的依赖(这里只是为了演示,循环依赖实际不随便搞)
public class App { public static void main( String[] args ) { ServiceLoader<DriverInteface> serviceLoader = ServiceLoader.load(DriverInteface.class); for (DriverInteface driverInteface:serviceLoader) { driverInteface.sayHello("liaoyang"); } } }
运行:
C:\Java\jdk1.8.0_181\bin\java.exe "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2018.2\lib\idea_rt.jar=59860:C:\Program Files\JetBrains\IntelliJ IDEA 2018.2\bin" -Dfile.encoding=UTF-8 -classpath C:\Java\jdk1.8.0_181\jre\lib\charsets.jar;C:\Java\jdk1.8.0_181\jre\lib\deploy.jar;C:\Java\jdk1.8.0_181\jre\lib\ext\access-bridge-64.jar;C:\Java\jdk1.8.0_181\jre\lib\ext\cldrdata.jar;C:\Java\jdk1.8.0_181\jre\lib\ext\dnsns.jar;C:\Java\jdk1.8.0_181\jre\lib\ext\jaccess.jar;C:\Java\jdk1.8.0_181\jre\lib\ext\jfxrt.jar;C:\Java\jdk1.8.0_181\jre\lib\ext\localedata.jar;C:\Java\jdk1.8.0_181\jre\lib\ext\nashorn.jar;C:\Java\jdk1.8.0_181\jre\lib\ext\sunec.jar;C:\Java\jdk1.8.0_181\jre\lib\ext\sunjce_provider.jar;C:\Java\jdk1.8.0_181\jre\lib\ext\sunmscapi.jar;C:\Java\jdk1.8.0_181\jre\lib\ext\sunpkcs11.jar;C:\Java\jdk1.8.0_181\jre\lib\ext\zipfs.jar;C:\Java\jdk1.8.0_181\jre\lib\javaws.jar;C:\Java\jdk1.8.0_181\jre\lib\jce.jar;C:\Java\jdk1.8.0_181\jre\lib\jfr.jar;C:\Java\jdk1.8.0_181\jre\lib\jfxswt.jar;C:\Java\jdk1.8.0_181\jre\lib\jsse.jar;C:\Java\jdk1.8.0_181\jre\lib\management-agent.jar;C:\Java\jdk1.8.0_181\jre\lib\plugin.jar;C:\Java\jdk1.8.0_181\jre\lib\resources.jar;C:\Java\jdk1.8.0_181\jre\lib\rt.jar;E:\gupaovip\spi_driver\target\classes;D:\my_maven_local_repository\com\liaoyang\com.liaoyang\1.0-SNAPSHOT\com.liaoyang-1.0-SNAPSHOT.jar com.liaoyang.App
liaoyang say hello to spi world Process finished with exit code 0
标签:http interface width idt img 引用 lib man target
原文地址:https://www.cnblogs.com/andydlz/p/13287217.html