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; } }