Spring @ControllerAdvice @ExceptionHandler

先来两个连接:

  Spring3.2新注解@ControllerAdvice

  Spring 注解学习手札(八)补遗——@ExceptionHandler

@Controller Class中如果有@ExceptionHandler method,那么该Controller类中的所有异常都会调用该方法。

但是,仅限于当前Controller类!

而@ControllerAdvice Classs中的@ExceptionHandler method则会实现全局异常回调。

如果开启扫描注解,且配置了include-filter,那就需要将@ControllerAdvice加入include-filter中,否则无效【【未验证】】。

如下:

    <context:component-scan base-package="com.sishuok.es" use-default-filters="false">  <!--应该是这个false导致的-->
           <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>  
           <context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>  
    </context:component-scan>  
原文地址:https://www.cnblogs.com/larryzeal/p/5533944.html