美文网首页
java-spi开发

java-spi开发

作者: fan_xing | 来源:发表于2019-12-04 15:03 被阅读0次

    SPI(Service Provide Interface)即服务提供接口规范,其功能是为了实现可插拔的服务提供,比如javax的validator框架就是借此实现的,java只是定义接口规范,具体实现方之一由hibernate提供。

    参考

    名词解释

    Service Implementer: 服务的实现
    Service Provide: 服务的提供
    Service Interface: 服务的接口

    开发流程

    • 定义服务接口(工程exchange-rate-api)

    package com.baeldung.rate.spi;
    
    import com.baeldung.rate.api.QuoteManager;
    
    public interface ExchangeRateProvider {
        QuoteManager create();
    }
    
    • 服务实现(工程exchange-rate-impl)

    package com.baeldung.rate.impl;
    
    import com.baeldung.rate.api.QuoteManager;
    import com.baeldung.rate.spi.ExchangeRateProvider;
    
    public class YahooFinanceExchangeRateProvider implements ExchangeRateProvider {
        @Override
        public QuoteManager create() {
            return new YahooQuoteManagerImpl();
        }
    }
    
    • 服务的提供或发布(工程exchange-rate-impl)

    在META-INF/service目录下定义服务接口文件com.baeldung.rate.spi.ExchangeRateProvider,内容为服务实现com.baeldung.rate.impl.YahooFinanceExchangeRateProvider


    image.png
    com.baeldung.rate.impl.YahooFinanceExchangeRateProvider
    
    • 服务调用

    通过ServiceLoader.load()去加载调用

    package com.baeldung.rate;
    
    import com.baeldung.rate.exception.ProviderNotFoundException;
    import com.baeldung.rate.spi.ExchangeRateProvider;
    
    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.List;
    import java.util.ServiceLoader;
    
    public class ExchangeRate {
    
        private static final String DEFAULT_PROVIDER = "com.baeldung.rate.spi.YahooFinanceExchangeRateProvider";
    
        public static List<ExchangeRateProvider> providers() {
            List<ExchangeRateProvider> services = new ArrayList<>();
            ServiceLoader<ExchangeRateProvider> loader = ServiceLoader.load(ExchangeRateProvider.class);
            loader.forEach(exchangeRateProvider -> {
                services.add(exchangeRateProvider);
            });
            return services;
        }
    
        public static ExchangeRateProvider provider() {
            return provider(DEFAULT_PROVIDER);
        }
    
        private static ExchangeRateProvider provider(String providerName) {
            ServiceLoader<ExchangeRateProvider> loader = ServiceLoader.load(ExchangeRateProvider.class);
            Iterator<ExchangeRateProvider> it = loader.iterator();
            while (it.hasNext()) {
                ExchangeRateProvider provider = it.next();
                if (providerName.equals(provider.getClass().getName())) {
                    return provider;
                }
            }
            throw new ProviderNotFoundException("Exchange Rate provider " + providerName + " not found");
        }
    }
    
    

    相关文章

      网友评论

          本文标题:java-spi开发

          本文链接:https://www.haomeiwen.com/subject/raclgctx.html