SpringMVC @ControllerAdvice 注解的官方解释

@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 都会被作用。

Leave a Comment

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