SpringBank 開發日誌 一種簡單的攔截器設計實現
阿新 • • 發佈:2017-07-24
exp bst 一個 pin factory span 之前 system request
當交易由Action進入Service之前,需要根據不同的Service實際負責業務的不同,真正執行Service的業務邏輯之前,做一些檢查工作。這樣的攔截器應該是基於配置的,與Service關聯起來的。
/** * @author wangxin * @contact [email protected] * @date Jul 22, 2017 * @Description: 所有TransactionController的抽象父類,主要作用為以一種低耦合的方式調用Service */ public abstract class BaseController {private final Logger log = LoggerFactory.getLogger(getClass()); @SuppressWarnings("rawtypes") public Map callService(String service,Map request) { Class<?> clazz; try { clazz = Class.forName("com.springbank.service.service." + service); Method method= clazz.getMethod("execute", Map.class); Service bean = (Service) ApplicationContextUtil.getApplicationContext().getBean(clazz); //先執行攔截器棧 List<Interceptor> interceptorList = (List<Interceptor>) clazz.getMethod("getInterceptorList", null).invoke(bean, null); for (Interceptor interceptor : interceptorList) { interceptor.process(); } //反射非靜態方法,需要把第一個參數設置為對象 return (Map)method.invoke(bean, request); } catch (ClassNotFoundException e) { ExceptionHandler.throwExcep(ExpceptionMapping.SYSTEMERR, e); } catch (NoSuchMethodException e) { ExceptionHandler.throwExcep(ExpceptionMapping.SYSTEMERR, e); } catch (SecurityException e) { ExceptionHandler.throwExcep(ExpceptionMapping.SYSTEMERR, e); } catch (IllegalAccessException e) { ExceptionHandler.throwExcep(ExpceptionMapping.SYSTEMERR, e); } catch (IllegalArgumentException e) { ExceptionHandler.throwExcep(ExpceptionMapping.SYSTEMERR, e); } catch (InvocationTargetException e) { ExceptionHandler.throwExcep(ExpceptionMapping.SYSTEMERR, e); } return null; } }
SpringBank 開發日誌 一種簡單的攔截器設計實現