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

 

转载于:https://www.cnblogs.com/heben/p/7230783.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值