在Spring mvc控制器上使用Spring Aop

sma*_*art 0 java aop spring-mvc

我有一个Spring MVC控制器抛出两种异常:

@RequestMapping(value = "forwardRefundApply",method = RequestMethod.GET)
public ModelAndView forwardRefundApply1(String ticketNbr)throws Exception {
    if(true)
        throw new Exception();
    else
        throw new ApplicationException("test");
}
Run Code Online (Sandbox Code Playgroud)

然后我编写了一个AOP类来处理异常,然后像这样返回Model:

  @Pointcut("execution(public * ..*(..))")
public void getRefundPointCut() {
}

@AfterThrowing(pointcut="getRefundPointCut()", throwing="e")
public ModelAndView throwException(Exception e){
    ModelAndView mav = null;
    if(e instanceof ApplicationException)
    {

        e.printStackTrace();
        mav = new ModelAndView(CommonConstants.ERRORPAGE);
        mav.addObject("errorMsg", "application error");
        return mav;
    }
    else{
        e.printStackTrace();

        mav  = new ModelAndView(CommonConstants.ERRORPAGE);
        mav.addObject("errorMsg", "system error");
        return mav;
    }
}
Run Code Online (Sandbox Code Playgroud)

唉是工作.但结果是错误.系统错误:

org.springframework.web.util.NestedServletException:处理程序处理失败; 嵌套异常是java.lang.NoSuchMethodError

Aspect类是不是可以将ModelAndView返回给Controller?

Mar*_*sch 6

为什么在这种情况下使用AOP呢?Spring提供您需要的一切: