zoukankan      html  css  js  c++  java
  • SpringBank 开发日志 一种简单的拦截器设计实现

    当交易由Action进入Service之前,需要根据不同的Service实际负责业务的不同,真正执行Service的业务逻辑之前,做一些检查工作。这样的拦截器应该是基于配置的,与Service关联起来的。

    /**
     * @author wangxin
     * @contact 408657544@qq.com
     * @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;
        }
    }
  • 相关阅读:
    Django模型
    Django视图
    Django入门
    Python之安装第三方扩展库
    【转载】python计算文件的行数和读取某一行内容的实现方法
    easymock入门
    Head First Python之4持久存储
    Selenium2+python自动化之读取Excel数据(xlrd)
    Selenium2+python自动化之数据驱动(ddt)
    Head First Python之3文件与异常
  • 原文地址:https://www.cnblogs.com/heben/p/7230783.html
Copyright © 2011-2022 走看看