首页 > 代码库 > SpringBank 开发日志 一种简单的拦截器设计实现

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

 

SpringBank 开发日志 一种简单的拦截器设计实现