spi工厂-使用

使用spi

public final class SysParam {

    private static ISysConfig config = SpiFactory.getSingleBean(ISysConfig.class);
     /**
     * 参数键名定义接口
     * 
     * @author yucan.zhang
     */
    public interface ParamKey {

        String smsLoginTemplate = "sms.logval";
        String smsRegisterTemplate = "sms.regval";
        String smsConfig = "smsConfig";
        String appId = "appid";
        String secret = "secret";
        String grant_type = "grant_type";
        String codeExchangSessionkey = "codeExchangSessionkey";
        String CAS_CASSERVERURLPREFIX="casServerUrlPrefix";
        //前端域名
        String domain="domain";
        String mchId="mch_id";
        String apiKey="api_key";
        String certpathH5="certpath_h5";
        String certpathApplet="certpath_applet";
        String tomcat_log="tomcat_log";
        String mch_appid="mch_appid";
        String mch_secret="mch_secret";
        String public_appid="public_appid";
        String public_secret="public_secret";
        String public_token="public_token";
        String public_mch_id="public_mch_id";
        String public_api_key="public_api_key";
        String public_certpath="public_certpath";
        String app_appid="app_appid";
        String app_secret="app_secret";
       
        String app_mch_id="app_mch_id";
        String app_api_key="app_api_key";
        String app_certpath_app="app_certpath_applet";
        String ATTACHMENT_METHOD = "attachment.manager";
    }

    /**
     * 
     * 
     * @return
     */
    public static String getGrant_type() {
        return config.getTextParam(ParamKey.grant_type);
    }
    .....
    }

spi工厂

package org.jflame.toolkit.reflect;

import java.util.Iterator;
import java.util.ServiceLoader;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
 * 使用SPI机制加载实现类工厂
 * 
 * @author yucan.zhang
 */
public class SpiFactory {

    private static ConcurrentMap serviceBeanMap = new ConcurrentHashMap<>();

    /**
     * 加载接口第一个实现类,如果已经加载过返回原实例,实现类单例形式存在
     * 
     * @param serviceClazz 接口类型
     * @return
     */
    @SuppressWarnings("unchecked")
    public static  T getSingleBean(Class serviceClazz) {
        String key = serviceClazz.getName();
        T instance = null;
        if (!serviceBeanMap.containsKey(key)) {
            ServiceLoader serviceLoader = ServiceLoader.load(serviceClazz);
            Iterator it = serviceLoader.iterator();
            if (it.hasNext()) {
                instance = it.next();
                serviceBeanMap.put(key, instance);
            }
        } else {
            instance = (T) serviceBeanMap.get(key);
        }

        return instance;
    }

    /**
     * 加载接口第一个实现类,每次都是新实例
     * 
     * @param serviceClazz 接口类型
     * @return
     */
    public static  T getBean(Class serviceClazz) {
        ServiceLoader serviceLoader = ServiceLoader.load(serviceClazz);
        Iterator it = serviceLoader.iterator();
        if (it.hasNext()) {
            return it.next();
        }
        return null;
    }

    /**
     * 加载接口所有实现类
     * 
     * @param serviceClazz 接口类型
     * @return
     */
    public static  Iterator getBeans(Class serviceClazz) {
        ServiceLoader serviceLoader = ServiceLoader.load(serviceClazz);
        return serviceLoader.iterator();
    }
}

你可能感兴趣的:(spi工厂-使用)