2017-06-19 86 views
0

我正在处理Spring应用程序,并且想要以下列方式实现异常处理: 针对异常被转移的特定异常类型实现Aspect类 有这样的方面基于异常类型层次结构的类和实现catch块的行为 例如, 有两个方面类:异常的ExceptionHandler和SQLException的SQLExceptionHandler 两者都是完全相同的切入点表达式。 现在,如果SQLException是由点表达式中涵盖的任何方法引发的,则应执行SQLExceptionHandler的方法。 如果发生FileNotFoundException或任何其他类型的异常,则应执行ExceptionHandler的方法。 任何帮助将不胜感激。使用AOP在Spring中实现“catch block”类似行为

+0

这是一个太宽泛的问题。看起来像一个任务。但要提出一个想法:你可以实现一个'BeanPostProcessor',它将用一个包含方法调用的调用处理程序包装你的带注释的类到try-catch块中。 –

回答

0

您可以尝试扩展OncePerRequestFilter类并覆盖doFilterInternal方法。像下面的东西会做的伎俩:

public void doFilterInternal(final HttpServletRequest request, final HttpServletResponse response, 
      final FilterChain filterChain) throws ServletException, IOException { 

    try { 
     filterChain.doFilter(request, response); 
    } catch (ExceptionType1 e) { 
    // Any exception thrown from your code can be handled or retrhown from here. 
    } catch (ExceptionType2 e) { 
    // Any exception thrown from your code can be handled or retrhown from here. 
    } catch ...... 
}