1,定义接口
public interface Logger { void info(); }
2,定义实现类
public class Logback implements Logger { @Override public void info() { System.out.println("lockback print info"); } }
public class Log4j implements Logger { @Override public void info() { System.out.println("Log4j print info--"); } }
3,创建 在resources 目录下 创建META-INF\services\com.ky.kfk.spi.Logger配置
com.ky.kfk.spi.Log4j
4,测试类
import java.util.ServiceLoader; public class Main { public static void main(String[] args) { ServiceLoader<Logger> logs = ServiceLoader.load(Logger.class); for (Logger logger : logs) { logger.info(); } } }
标签:info,void,介绍,class,spi,Logger,public From: https://www.cnblogs.com/dongma/p/17103049.html