private static List<Factory> discoverFactories(ClassLoader classLoader) { //spi机制加载类 final Iterator<Factory> serviceLoaderIterator = ServiceLoader.load(Factory.class, classLoader).iterator(); final List<Factory> loadResults = new ArrayList<>(); while (true) { try { // error handling should also be applied to the hasNext() call because service // loading might cause problems here as well if (!serviceLoaderIterator.hasNext()) { break; } loadResults.add(serviceLoaderIterator.next()); } catch (Throwable t) { if (t instanceof NoClassDefFoundError) { LOG.debug( "NoClassDefFoundError when loading a " + Factory.class.getCanonicalName() + ". This is expected when trying to load factory but no implementation is loaded.", t); } else { throw new RuntimeException( "Unexpected error when trying to load service provider.", t); } } } return loadResults; }
spi service实现类加载代码
最新推荐文章于 2024-11-08 23:27:36 发布