SpringMVC @ControllerAdvice 注解的官方解释

该注解用于标记协助Controller的类,可以定义全局的异常处理、数据绑定初始化及模型属性方法。适用于所有@RequestMapping方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

tips:

本文由wp2Blog导入,原文链接:http://devonios.com/controller-advice-annotation.html

转载于:https://my.oschina.net/yangyan/blog/859208

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值