SpringMVC @ControllerAdvice 注解的官方解释

5
(3)
@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 都会被作用。

这篇文章有用吗?

平均评分 5 / 5. 投票数: 3

到目前为止还没有投票!成为第一位评论此文章。

很抱歉,这篇文章对您没有用!

让我们改善这篇文章!

告诉我们我们如何改善这篇文章?

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据