@ControllerAdvice,是spring3.2提供的新注解,从名字上可以看出大体意思是控制器增强。让我们先看看@ControllerAdvice的实现:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface ControllerAdvice { }

没什么特别之处,该注解使用@Component注解,这样的话当我们使用<context:component-scan>扫描时也能扫描到。

其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)
@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>

该注解非常简单,大多数时候其实只@ExceptionHandler比较有用,其他两个用到的场景非常少,这样可以把异常处理器应用到所有控制器,而不是@Controller注解的单个控制器。

@ControllerAdvice -- 示例

啦啦啦

最新文章

  1. μC/OS-Ⅲ系统中的任务就续表
  2. iOS调用系统的电话功能
  3. STL学习三:deque容器
  4. iOS_ @property参数分析
  5. 读懂系统负载(Load Avg)的含义 | Devops
  6. CreateThread、_beginthreadex和AfxBeginThread 的区别
  7. ASP.NET MVC+EF框架+EasyUI实现权限管理系列之开篇
  8. WebClient 访问间歇性返回403解决方案
  9. Java 常量池存放的是什么
  10. ARM开发软件ADS教程
  11. 从零开始:一个正式的vue+webpack项目的目录结构是怎么形成的
  12. python Flask
  13. Elasticsearch 安装和配置
  14. register form code(2nd week blog)
  15. MATLAB入门笔记
  16. idea的起步配置
  17. HRBUST - 1818 石子合并 区间dp入门
  18. 洛谷p1732 活蹦乱跳的香穗子 二维DP
  19. Html写作规范
  20. ZSetOperations 操作解释 拷贝过来的 哈哈哈

热门文章

  1. VC 实现文件与应用程序关联(转载)
  2. BZOJ 3589 动态树(子树操作,链查询)
  3. iOS深入学习(再谈block)
  4. c++ float 带 e 的指数
  5. Lambda表达式之Python
  6. ABAP锁、数据库锁
  7. 随机步法A-Z
  8. Python入门-行和缩进
  9. php笔记[2]
  10. thinkphp分页显示