public S first() {
        final Iterator<Class<S>> it = classIterator();
        Class<S> first = null;
        while (it.hasNext()) {
            final Class<S> cls = it.next();
            if (first == null) {
                first = cls;
            } else {
                final SPI currSpi = first.getAnnotation(SPI.class);
                final SPI nextSpi = cls.getAnnotation(SPI.class);

                final int currPriority = currSpi == null ? 0 : currSpi.priority();
                final int nextPriority = nextSpi == null ? 0 : nextSpi.priority();

                if (nextPriority > currPriority) {
                    first = cls;
                }
            }
        }

        if (first == null) {
            throw fail(this.service, "could not find any implementation for class");
        }

        final S ins = this.providers.get(first.getName());
        if (ins != null) {
            return ins;
        }

        return newProvider(first);
    }
A simple service-provider loading facility (SPI).

@author jiachun.fjc
jraft-core/src/main/java/com/alipay/sofa/jraft/util/JRaftServiceLoader.java