Spring3.2新注解@ControllerAdvice

前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到教程。

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

 

Java代码   收藏代码
  1. @Target(ElementType.TYPE)  
  2. @Retention(RetentionPolicy.RUNTIME)  
  3. @Documented  
  4. @Component  
  5. public @interface ControllerAdvice {  
  6.   
  7. }  

 没什么特别之处,该注解使用@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最有用,另外两个用处不大。

 

 

接下来看段代码:

 

Java代码   收藏代码
  1. @ControllerAdvice  
  2. public class ControllerAdviceTest {  
  3.   
  4.     @ModelAttribute  
  5.     public User newUser() {  
  6.         System.out.println("============应用到所有@RequestMapping注解方法,在其执行之前把返回值放入Model");  
  7.         return new User();  
  8.     }  
  9.   
  10.     @InitBinder  
  11.     public void initBinder(WebDataBinder binder) {  
  12.         System.out.println("============应用到所有@RequestMapping注解方法,在其执行之前初始化数据绑定器");  
  13.     }  
  14.   
  15.     @ExceptionHandler(UnauthenticatedException.class)  // 加上这个注解捕获异常:UnauthenticatedException
  16. //可以改为其它要捕获的异常类型,如  RuntimeException
  17.     @ResponseStatus(HttpStatus.UNAUTHORIZED)  
  18.     public String processUnauthenticatedException(NativeWebRequest request, UnauthenticatedException e) {  
  19.         System.out.println("===========应用到所有@RequestMapping注解的方法,在其抛出UnauthenticatedException异常时执行");  
  20.         return "viewName"//返回一个逻辑视图名  
  21.     }  
  22. }  

 

如果你的spring-mvc配置文件使用如下方式扫描bean

Java代码   收藏代码
  1. <context:component-scan base-package="com.sishuok.es" use-default-filters="false">  
  2.        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>  
  3.    </context:component-scan>  

 需要把@ControllerAdvice包含进来,否则不起作用:

Java代码   收藏代码
  1. <context:component-scan base-package="com.sishuok.es" use-default-filters="false">  
  2.        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>  
  3.        <context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>  
  4.    </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注解的单个控制器。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值