Spring3.2新注解@ControllerAdvice
@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)
- @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注解的单个控制器。
Spring3.2新注解@ControllerAdvice的更多相关文章
- SpringMVC重要注解 @ControllerAdvice
@ControllerAdvice,是Spring3.2提供的新注解,从名字上可以看出大体意思是控制器增强.让我们先看看@ControllerAdvice的实现: package org.spring ...
- Spring3.1新特性(转)
一.Spring2.5之前,我们都是通过实现Controller接口或其他实现来定义我们的处理器类. 二.Spring2.5引入注解式处理器支持,通过@Controller 和 @RequestMap ...
- Spring3.1新特性
一.Spring2.5之前,我们都是通过实现Controller接口或其实现来定义我们的处理器类. 二.Spring2.5引入注解式处理器支持,通过@Controller 和 @RequestMa ...
- Spring3.1新特性介绍
Spring3.1新特性 一.Spring2.5之前,我们都是通过实现Controller接口或其实现来定义我们的处理器类. 二.Spring2.5引入注解式处理器支持,通过@Controller ...
- Spring系列之新注解配置+Spring集成junit+注解注入
Spring系列之注解配置 Spring是轻代码而重配置的框架,配置比较繁重,影响开发效率,所以注解开发是一种趋势,注解代替xml配置文件可以简化配置,提高开发效率 你本来要写一段很长的代码来构造一个 ...
- Spring3.1新属性管理API:PropertySource、Environment、Profile
Spring3.1提供了新的属性管理API,而且功能非常强大且很完善,对于一些属性配置信息都应该使用新的API来管理.虽然现在Spring已经到4版本了,这篇文章来的晚点. 新的属性管理API Pro ...
- JUnit扩展:引入新注解Annotation
发现问题 JUnit提供了Test Suite来帮助我们组织case,还提供了Category来帮助我们来给建立大的Test Set,比如BAT,MAT, Full Testing. 那么什么情况下, ...
- [转载] Spring3.1 Cache注解
需要感慨一下,spring3.0时丢弃了2.5时的spring-modules-cache.jar,致使无法使用spring来方便的管理cache注解,好在3.1.M1中增加了对cache注解的支持, ...
- 在Spring3中使用注解(@Scheduled)创建计划任务
Spring3中加强了注解的使用,其中计划任务也得到了增强,现在创建一个计划任务只需要两步就完成了: 创建一个Java类,添加一个无参无返回值的方法,在方法上用@Scheduled注解修饰一下: 在S ...
随机推荐
- iOS8中的动态文本
原文链接 : Swift Programming 101: Mastering Dynamic Type in iOS 8 原文作者 : Kevin McNeish Apple声称鼓励第三方App能够 ...
- 【Android 应用开发】Android 开发 之 JNI入门 - NDK从入门到精通
NDK项目源码地址 : -- 第一个JNI示例程序下载 : GitHub - https://github.com/han1202012/NDKHelloworld.git -- Java传递参数给C ...
- frameset导航框架
1.制作导航框架(注意"name='mainframe'") <html> <frameset cols="25%,75%"> < ...
- lucene特殊字符处理
这是个郁闷的问题,今天遇到了,但在lucene中查询的关键字保护有特殊字符,譬如--,会出现如下异常: org.apache.lucene.queryParser.ParseException: Ca ...
- 学习笔记——Windows下cocos2d-x,eclipse中自编译
cocos2d-x创建的安卓项目导入eclipse后. 在项目属性中配置Builders. 在eclipse编译还需要配置相应的变量,即后面提到的cygwin编译中要添加的变量. D:/cygdriv ...
- Linux一些命令
1.查看系统安装软件 rpm -qa //(不包括绿色安装的软件程序,也就是直接在安装目录中启动的不包括) rpm -qa |grep gcc 参数解释:q ——询问 a —— 查询全部 l — ...
- 关于this指针理解
1. this指针的用处: 一个对象的this指针并不是对象本身的一部分,不会影响sizeof(对象)的结果.this作用域是在类内部,当在类的非静态成员函数中访问类的非静态成员的时候,编译器会自动将 ...
- jquery选择器 之 获取父级元素、同级元素、子元素(转)
一.获取父级元素 1. parent([expr]): 获取指定元素的所有父级元素 <div id="par_div"><a id="href_fir& ...
- 看详细的tomcat报错信息
WEB-INF/classes目录下新建一个文件叫logging.properties handlers = org.apache.juli.FileHandler, java.util.loggin ...
- Servlet程序开发--WEB开发模式(Mode I, Mode II)
Mode I: 就是在开发中,将显示层,控制层,数据层的操作统一交给JSP或JavaBean来进行处理. 客户端通过访问JSP,调用里面的JavaBean,而通过JavaBean调用数据库,在Java ...