前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >SpringMVC @ControllerAdvice 注解的官方解释

SpringMVC @ControllerAdvice 注解的官方解释

作者头像
前Thoughtworks-杨焱
发布2021-12-08 09:26:15
3300
发布2021-12-08 09:26:15
举报
文章被收录于专栏:杨焱的专栏
代码语言:javascript
复制
@Target(value=TYPE)
@Retention(value=RUNTIME)
@Documented
@Component
public @interface ControllerAdvice

Indicates the annotated class assists a “Controller”. Serves as a specialization of @Component, allowing for implementation classes to be autodetected through classpath scanning.

It is typically used to define @ExceptionHandler, @InitBinder, and @ModelAttribute methods that apply to all @RequestMapping methods.

One of annotations(), basePackageClasses(), basePackages() or its alias value() may be specified to define specific subsets of Controllers to assist. When multiple selectors are applied, OR logic is applied – meaning selected Controllers should match at least one selector.

The default behavior (i.e. if used without any selector), the @ControllerAdvice annotated class will assist all known Controllers.

Note that those checks are done at runtime, so adding many attributes and using multiple strategies may have negative impacts (complexity, performance).

Since: 3.2 Author: Rossen Stoyanchev, Brian Clozel, Sam Brannen

上面的意思是带有@ControllerAdvice的类,作为@Controller类 的组成部分,通常用来定义 @ExceptionHandler, @InitBinder, 和@ModelAttribute 这样的方法,作用的返回由这个注解的 annotations(), basePackageClasses(), basePackages() 或者别名 value()筛选,如果同时设置了这几个值,因为多个条件之间的关系是 OR的关系,所以满足任何一个条件的 Controller 都会被作用。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017-03-14,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档