spi 介绍

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();
        }
    }
}

 

posted @ 2023-02-08 19:24  冬马党  阅读(28)  评论(0编辑  收藏  举报