JAVA SPI实现机制与原理分析

前言

       最近要做业务接口,需要在多个模块根据需要调用不同的实现,立马就想到了SPI机制,但是Java自带的SPI又不能满足要求,使用dubbo的SPI就能达到目的,但这样就需要强依赖dubbo的jar,就想自己定制一个简单的实现,首先看看java的SPI如何实现。

1. demo

public interface SPInterface {

    void hello();
}

public class Hello1 implements SPInterface {
    public void hello() {
        System.out.println("111111111111111111111111111");
    }
}

public class Hello2 implements SPInterface {
    public void hello() {
        System.out.println("222222222222222222222222222");
    }
}

main方法

import java.util.ServiceLoader;

public class MainClass {
    public static void main(String[] args) {
        ServiceLoader<SPInterface> loader = ServiceLoader.load(SPInterface.class, Thread.currentThread().getContextClassLoader());
        for (SPInterface spInterface : loader) {
            spInterface.hello();
        }
    }
}

指定class loader很重要,否则动态加载的jar或者class就会不能使用SPI。

需要配置在classpath下创建

META-INF/services/${package.interfaceName}

文件,etg

运行main方法

 说明SPI已生效,这种方式开发非常常见,典型的是数据库驱动

比如MySQL

package com.mysql.cj.jdbc;

import java.sql.SQLException;

/**
 * The Java SQL framework allows for multiple database drivers. Each driver should supply a class that implements the Driver interface
 * 
 * <p>
 * The DriverManager will try to load as many drivers as it can find and then for any given connection request, it will ask each driver in turn to try to
 * connect to the target URL.
 * 
 * <p>
 * It is strongly recommended that each Driver class should be small and standalone so that the Driver class can be loaded and queried without bringing in vast
 * quantities of supporting code.
 * 
 * <p>
 * When a Driver class is loaded, it should create an instance of itself and register it with the DriverManager. This means that a user can load and register a
 * driver by doing Class.forName("foo.bah.Driver")
 */
public class Driver extends NonRegisteringDriver implements java.sql.Driver {

2. 原理分析

本质就是通过文本文件加载class实现面向接口编程,核心是ServiceLoader

 我们反编译Main方法,可以看到for each是用迭代器实现的。

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.feng.spi.demo;

import com.feng.spi.demo.hello.SPInterface;
import java.util.Iterator;
import java.util.ServiceLoader;

public class MainClass {
    public MainClass() {
    }

    public static void main(String[] args) {
        ServiceLoader<SPInterface> loader = ServiceLoader.load(SPInterface.class, Thread.currentThread().getContextClassLoader());
        Iterator var2 = loader.iterator();

        while(var2.hasNext()) {
            SPInterface spInterface = (SPInterface)var2.next();
            spInterface.hello();
        }

    }
}

而ServiceLoader实现了迭代接口

public final class ServiceLoader<S>
    implements Iterable<S>

    //前缀地址
    private static final String PREFIX = "META-INF/services/";

    // The class or interface representing the service being loaded
    //SPI的接口类
    private final Class<S> service;

    // The class loader used to locate, load, and instantiate providers
    //类加载器
    private final ClassLoader loader;

    // The access control context taken when the ServiceLoader is created
    //访问控制上下文
    private final AccessControlContext acc;

    // Cached providers, in instantiation order
    //缓存SPI的实现bean,key是完整类名
    private LinkedHashMap<String,S> providers = new LinkedHashMap<>();

    // The current lazy-lookup iterator
    //懒迭代器,ServiceLoader就是使用它实现懒加载的,需要的时候加载,配合缓存提高效率
    private LazyIterator lookupIterator;

看看几个属性,已经说的很明显了。看看load方法

    public static <S> ServiceLoader<S> load(Class<S> service,
                                            ClassLoader loader)
    {
        //每次load,创建一个对象
        return new ServiceLoader<>(service, loader);
    }

    public static <S> ServiceLoader<S> load(Class<S> service) {
        ClassLoader cl = Thread.currentThread().getContextClassLoader();
        return ServiceLoader.load(service, cl);
    }

就是new ServiceLoader对象,参数是接口class与classloader

    public void reload() {
        //清理缓存
        providers.clear();
        //初始化懒迭代器
        lookupIterator = new LazyIterator(service, loader);
    }

    private ServiceLoader(Class<S> svc, ClassLoader cl) {
        //not null
        service = Objects.requireNonNull(svc, "Service interface cannot be null");
        //找到class loader
        loader = (cl == null) ? ClassLoader.getSystemClassLoader() : cl;
        //访问控制器上下文
        acc = (System.getSecurityManager() != null) ? AccessController.getContext() : null;
        reload();
    }

ServiceLoader实现了迭代接口,下面看看

iterator()

方法

    public Iterator<S> iterator() {
        return new Iterator<S>() {

            Iterator<Map.Entry<String,S>> knownProviders
                = providers.entrySet().iterator();

            public boolean hasNext() {
                //缓存hasNext
                if (knownProviders.hasNext())
                    return true;
                //缓存没有,使用懒迭代器
                return lookupIterator.hasNext();
            }

            public S next() {
                //同理,缓存获取
                if (knownProviders.hasNext())
                    return knownProviders.next().getValue();
                //然后懒迭代器
                return lookupIterator.next();
            }

            //不能删除
            public void remove() {
                throw new UnsupportedOperationException();
            }

        };
    }

原理都是通过懒迭代器实现的,通过上面的代码reload里面初始化的

    public void reload() {
        providers.clear();
        lookupIterator = new LazyIterator(service, loader);
    }

追踪LazyIterator

private class LazyIterator
        implements Iterator<S>
    {
        //接口类
        Class<S> service;
        //类加载器
        ClassLoader loader;
        //读取文件URL
        Enumeration<URL> configs = null;
        //缓存文件的实现类
        Iterator<String> pending = null;
        //迭代的缓存next方法的实现类,加快迭代
        String nextName = null;

        private LazyIterator(Class<S> service, ClassLoader loader) {
            this.service = service;
            this.loader = loader;
        }

        private boolean hasNextService() {
            //缓存了,所以直接true
            if (nextName != null) {
                return true;
            }
            if (configs == null) {
                try {
                    //文件的路径,前缀+接口完整名称
                    String fullName = PREFIX + service.getName();
                    //载入URL,传入classloader,否则使用系统的
                    if (loader == null)
                        configs = ClassLoader.getSystemResources(fullName);
                    else
                        configs = loader.getResources(fullName);
                } catch (IOException x) {
                    fail(service, "Error locating configuration files", x);
                }
            }
            //尚未缓存实现类名称
            while ((pending == null) || !pending.hasNext()) {
                //判断是否读取到文件
                if (!configs.hasMoreElements()) {
                    return false;
                }
                //缓存文件读取的实现类名称
                pending = parse(service, configs.nextElement());
            }
            //缓存next方法迭代的实现类,提高效率
            nextName = pending.next();
            return true;
        }

        private S nextService() {
            //校验
            if (!hasNextService())
                throw new NoSuchElementException();
            //使用缓存
            String cn = nextName;
            //清理缓存next的实现类名称
            nextName = null;
            Class<?> c = null;
            try {
                //创建类
                c = Class.forName(cn, false, loader);
            } catch (ClassNotFoundException x) {
                fail(service,
                     "Provider " + cn + " not found");
            }
            //如果不是接口的实现
            if (!service.isAssignableFrom(c)) {
                fail(service,
                     "Provider " + cn  + " not a subtype");
            }
            try {
                //创建实现类的实例,转换接口类型
                S p = service.cast(c.newInstance());
                //本地缓存,使用完整类名做key,缓存实例
                providers.put(cn, p);
                return p;
            } catch (Throwable x) {
                fail(service,
                     "Provider " + cn + " could not be instantiated",
                     x);
            }
            throw new Error();          // This cannot happen
        }

        //hasNext,不解释
        public boolean hasNext() {
            //访问控制上下文,默认null;通过System.setSecurityManager(final SecurityManager s)设置
            if (acc == null) {
                return hasNextService();
            } else {
                PrivilegedAction<Boolean> action = new PrivilegedAction<Boolean>() {
                    public Boolean run() { return hasNextService(); }
                };
                return AccessController.doPrivileged(action, acc);
            }
        }

        //迭代器next
        public S next() {
            //访问控制上下文,默认是没有控制的
            if (acc == null) {
                return nextService();
            } else {
                PrivilegedAction<S> action = new PrivilegedAction<S>() {
                    public S run() { return nextService(); }
                };
                return AccessController.doPrivileged(action, acc);
            }
        }

        //不能删除
        public void remove() {
            throw new UnsupportedOperationException();
        }

    }

cast,判断后强转

    public T cast(Object obj) {
        if (obj != null && !isInstance(obj))
            throw new ClassCastException(cannotCastMsg(obj));
        return (T) obj;
    }

下面看看解析文件方法

parse(service, configs.nextElement());

    private Iterator<String> parse(Class<?> service, URL u)
        throws ServiceConfigurationError
    {
        InputStream in = null;
        BufferedReader r = null;
        //这个就是缓存的实现类完整名称列表
        ArrayList<String> names = new ArrayList<>();
        try {
            //读取并包装流
            in = u.openStream();
            r = new BufferedReader(new InputStreamReader(in, "utf-8"));
            int lc = 1;
            while ((lc = parseLine(service, u, r, lc, names)) >= 0);
        } catch (IOException x) {
            fail(service, "Error reading configuration file", x);
        } finally {
            try {
                if (r != null) r.close();
                if (in != null) in.close();
            } catch (IOException y) {
                fail(service, "Error closing configuration file", y);
            }
        }
        return names.iterator();
    }

核心调用

parseLine(service, u, r, lc, names)

    private int parseLine(Class<?> service, URL u, BufferedReader r, int lc,
                          List<String> names)
        throws IOException, ServiceConfigurationError
    {
        //按行读取
        String ln = r.readLine();
        if (ln == null) {
            return -1;
        }
        //去#
        int ci = ln.indexOf('#');
        if (ci >= 0) ln = ln.substring(0, ci);
        ln = ln.trim();
        int n = ln.length();
        if (n != 0) {
            if ((ln.indexOf(' ') >= 0) || (ln.indexOf('\t') >= 0))
                fail(service, u, lc, "Illegal configuration-file syntax");
            int cp = ln.codePointAt(0);
            //鉴定是否Java类名
            if (!Character.isJavaIdentifierStart(cp))
                fail(service, u, lc, "Illegal provider-class name: " + ln);
            for (int i = Character.charCount(cp); i < n; i += Character.charCount(cp)) {
                cp = ln.codePointAt(i);
                if (!Character.isJavaIdentifierPart(cp) && (cp != '.'))
                    fail(service, u, lc, "Illegal provider-class name: " + ln);
            }
            //缓存没有,加入缓存
            if (!providers.containsKey(ln) && !names.contains(ln))
                names.add(ln);
        }
        return lc + 1;
    }

总结

       Java的SPI非常简单,实现类解耦的思想,让接口与实现分离,通过协议(配置文件)来达到调用的目的。缺陷也很明显

1. 不能按需加载实现类,必须迭代

2. 获取提供的实现,不能安装参数获取,还是要迭代

3. 没有线程安全的考虑

ServiceLoader每次load会新创建一个对象,当多个线程对迭代时,里面使用的缓存都是非线程安全的ArrayList与LinkedHashMap,不保证原子性。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值