zoukankan      html  css  js  c++  java
  • java常用单例

    public class TerminalServiceHelper {
    // 静态日志对象
    private static final Logger logger = Logger.getLogger(TerminalServiceHelper.class); private TerminalServiceHelper() {      // 私有构造器
          initTerminalService(); }
    // 私有静态实列对象
    private static TerminalServiceHelper instance;
    // 获取实例对象
    public static TerminalServiceHelper getInstance() {
    // 不存在就创建,存在就返回; 如果多线程调用如何保证唯一单例
    if (instance != null) { return instance; } instance = new TerminalServiceHelper(); return instance; } private Map<String, String> serviceMap = new HashMap<String, String>(); public void add(String requestCode, String serviceName) { serviceMap.put(requestCode, serviceName); }

         

        private void initTerminalService() {
          // 初始化bean名称到集合对象中

          add("1001", "cardSignedService");

        }

    public IBaseWebserviceService getService(String requestCode, ServletContext ctx) throws ParamException {
            WebApplicationContext springContext = WebApplicationContextUtils.getWebApplicationContext(ctx);
            IBaseWebserviceService geposAppService = null;
            try {
                geposAppService = (IBaseWebserviceService) springContext.getBean(serviceMap.get(requestCode));
            } catch (Exception e) {
                logger.error(e.getMessage(),e);
                throw new ParamException("请求类型不正常,请确认后重试:"+e.getMessage());
            }
            return geposAppService;
        }
    
    }
  • 相关阅读:
    实现免费WiFi无线共享
    详解spring配置文件
    P1886 滑动窗口 /【模板】单调队列
    P3370 【模板】字符串哈希
    P3371 【模板】单源最短路径(弱化版)
    P3367 【模板】并查集
    P1177 【模板】快速排序
    P3382 【模板】三分法
    P3374 【模板】树状数组 1
    P1226 【模板】快速幂||取余运算
  • 原文地址:https://www.cnblogs.com/zhangmo/p/15566112.html
Copyright © 2011-2022 走看看