@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注解的单个控制器。
相关推荐
面试官:Spring 注解 @After,@Around,@Before 的执行顺序是?.zip 面试官:Spring 注解 @After,@Around,@Before 的执行顺序是?.zip 面试官:Spring 注解 @After,@Around,@Before 的执行顺序是?.zip 面试官:...
spring3.2的源代码,至于为什么要下载spring3.2的源代码呢? 个人觉得spring3.2的源代码比较好编译,没啥脾气,像我之前下载的spring的最新版本源代码,还有spring4.0的源代码,不论是使用jdk1.6还是1.7甚至是1.8编译...
Spring之条件注解@Conditional,条件(系统)不同注入的对象也不同。演示用例
spring3.2 源码jar 用于关联我们项目中的jar包,这样就可以在项目中看到spring的源码的,具体怎么使用请参考我的博客 http://blog.csdn.net/lzxadsl
拓薪教育-spring3.2-序【】02.拓薪教育-Spring3.2-介绍IOC上【】03.拓薪教育-spring3.2-介绍IOC下【】04.拓薪教育-spring3.2-AOP和其他功能介绍【】05.拓薪教育-spring3.2-环境搭建【】06.拓薪教育-s 资源太大,传...
这是最新的spring jar包,这里包含了springMVC STRUTS HIBERNIAATE 等等优秀的组合
主要介绍了SpringMVC @ControllerAdvice使用场景,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
spring使用参考文档,从官网下载
struts2+Spring2.5+Spring3.2)实现登录.struts2+Spring2.5+Spring3.2)实现登录.struts2+Spring2.5+Spring3.2)实现登录。
ControllerAdvice spring mvc 3.2 统一错误页面控制器
spring3.2 编译后源码, spring3.2 编译后源码, spring3.2 编译后源码,
spring3.2框架是一个轻量级的解决方案和一个潜在的 一站式为构建企业级应用程序。 然而, spring是模块化的,允许你只使用那些你需要的部分, 无需引入其余。 您可以使用IoC容器, Struts在上,但是你也可以只使用 ...
spring3.2 源码编译后需要的jar,spring3.2 源码编译后需要的jar,spring3.2 源码编译后需要的jar
spring3.2-api.chm格式,本人在网上找了好多chm格式的,都不正常现在,没有办法自己找了一个工具打包成chm格式
Spring注解@Component、@Repository、@Service、@Controller区别.doc
spring3.2 源码编译后需要的jar,spring3.2 源码编译后需要的jar,spring3.2 源码编译后需要的jar
最新的spring3.2文档,spring3.2 spring.chm spring3.2.chm 本人从官网下载制作的chm官方spring文档
spring3.2最全jar包,开发工具,eclipse,与其他开发包兼容
Spring 是一个开源框架,是为了解决企业应用程序开发复杂性而创建的。框架的主要优势之一就是其分层架构,分层架构允许您选择使用哪一个组件,同时为 J2EE 应用程序开发提供集成的框架
最新的Spring3.2 参考文档,高清版,适合于Kindle看