前些天發現了一個巨牛的人工智能學習網站,通俗易懂,風趣幽默,忍不住分享一下給大家。點擊跳轉到教程。
@ControllerAdvice,是spring3.2提供的新注解,從名字上可以看出大體意思是控制器增強。讓我們先看看@ControllerAdvice的實現:
?

- @Target(ElementType.TYPE)??
- @Retention(RetentionPolicy.RUNTIME)??
- @Documented??
- @Component??
- public?@interface?ControllerAdvice?{??
- ??
- }??
?沒什么特別之處,該注解使用@Component注解,這樣的話當我們使用<context:component-scan>掃描時也能掃描到,具體可參考【第十二章】零配置 之 12.3 注解實現Bean定義 ——跟我學spring3。
?
其javadoc定義是:
* Indicates the annotated class assists a "Controller".
*
* <p>Serves as a specialization of {@link Component @Component}, allowing for
* implementation classes to be autodetected through classpath scanning.
*
* <p>It is typically used to define {@link ExceptionHandler @ExceptionHandler},
* {@link InitBinder @InitBinder}, and {@link ModelAttribute @ModelAttribute}
* methods that apply to all {@link RequestMapping @RequestMapping} methods.
*
* @author Rossen Stoyanchev
* @since 3.2
*/
即把@ControllerAdvice注解內部使用@ExceptionHandler、@InitBinder、@ModelAttribute注解的方法應用到所有的?@RequestMapping注解的方法。非常簡單,不過只有當使用@ExceptionHandler最有用,另外兩個用處不大。
?
?
接下來看段代碼:
?

- @ControllerAdvice??
- public?class?ControllerAdviceTest?{??
- ??
- ????@ModelAttribute??
- ????public?User?newUser()?{??
- ????????System.out.println("============應用到所有@RequestMapping注解方法,在其執行之前把返回值放入Model");??
- ????????return?new?User();??
- ????}??
- ??
- ????@InitBinder??
- ????public?void?initBinder(WebDataBinder?binder)?{??
- ????????System.out.println("============應用到所有@RequestMapping注解方法,在其執行之前初始化數據綁定器");??
- ????}??
- ??
- ????@ExceptionHandler(UnauthenticatedException.class) ?// 加上這個注解捕獲異常:UnauthenticatedException
- //可以改為其它要捕獲的異常類型,如 ?RuntimeException
- ????@ResponseStatus(HttpStatus.UNAUTHORIZED)??
- ????public?String?processUnauthenticatedException(NativeWebRequest?request,?UnauthenticatedException?e)?{??
- ????????System.out.println("===========應用到所有@RequestMapping注解的方法,在其拋出UnauthenticatedException異常時執行");??
- ????????return?"viewName";?//返回一個邏輯視圖名??
- ????}??
- }??
?
如果你的spring-mvc配置文件使用如下方式掃描bean

- <context:component-scan?base-package="com.sishuok.es"?use-default-filters="false">??
- ???????<context:include-filter?type="annotation"?expression="org.springframework.stereotype.Controller"/>??
- ???</context:component-scan>??
?需要把@ControllerAdvice包含進來,否則不起作用:

- <context:component-scan?base-package="com.sishuok.es"?use-default-filters="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>??
?
1、@ModelAttribute注解的方法作用請參考SpringMVC強大的數據綁定(2)——第六章 注解式控制器詳解——跟著開濤學SpringMVC中的【二、暴露表單引用對象為模型數據】,作用是一樣的,只不過此處是對所有的@RequestMapping注解的方法都起作用。當需要設置全局數據時比較有用。
2、@InitBinder注解的方法作用請參考SpringMVC數據類型轉換——第七章 注解式控制器的數據驗證、類型轉換及格式化——跟著開濤學SpringMVC,同1類似。當需要全局注冊時比較有用。
3、@ExceptionHandler,異常處理器,此注解的作用是當出現其定義的異常時進行處理的方法,其可以使用springmvc提供的數據綁定,比如注入HttpServletRequest等,還可以接受一個當前拋出的Throwable對象。可以參考javadoc或snowolf的Spring 注解學習手札(八)補遺——@ExceptionHandler。
?
該注解非常簡單,大多數時候其實只@ExceptionHandler比較有用,其他兩個用到的場景非常少,這樣可以把異常處理器應用到所有控制器,而不是@Controller注解的單個控制器。
?