Declaration
可以通过在 Servlet 的 WebApplicationContext
中使用标准 Spring Bean 定义来定义 Controller Bean。@Controller
构造型允许自动检测,与 Spring 检测类路径中 @Component
类并自动注册 Bean 定义的通用支持相一致。它还可以作为标注类的构造型,表明其作为 Web 组件的角色。
You can define controller beans by using a standard Spring bean definition in the
Servlet’s WebApplicationContext
. The @Controller
stereotype allows for auto-detection,
aligned with Spring general support for detecting @Component
classes in the classpath
and auto-registering bean definitions for them. It also acts as a stereotype for the
annotated class, indicating its role as a web component.
若要启用此类 @Controller
Bean 的自动检测,可以向 Java 配置添加组件扫描,如下例所示:
To enable auto-detection of such @Controller
beans, you can add component scanning to
your Java configuration, as the following example shows:
include-code::./WebConfiguration[tag=snippet,indent=0]
@RestController
是一个 composed annotation,它本身通过 @Controller
和 @ResponseBody
进行元注释,以指示一个控制器,其每个方法都继承类型级别的 @ResponseBody
注释,并且因此直接写入响应体,而不是视图分辨率,也不使用 HTML 模板进行渲染。
@RestController
is a composed annotation that is
itself meta-annotated with @Controller
and @ResponseBody
to indicate a controller whose
every method inherits the type-level @ResponseBody
annotation and, therefore, writes
directly to the response body versus view resolution and rendering with an HTML template.
AOP Proxies
在某些情况下,你可能需要在运行时使用 AOP 代理装饰 Controller。如果选择在 Controller 上直接使用 @Transactional
标注,则这是一个示例。当出现此类情况,特别是对于 Controller,我们建议使用基于类的代理。对于 Controller 上的此类标注,这种情况会自动发生。
In some cases, you may need to decorate a controller with an AOP proxy at runtime.
One example is if you choose to have @Transactional
annotations directly on the
controller. When this is the case, for controllers specifically, we recommend
using class-based proxying. This is automatically the case with such annotations
directly on the controller.
如果 Controller 实现了某个接口并且需要 AOP 代理,你可能需要显式配置基于类的代理。例如,使用 @EnableTransactionManagement
,可以更改为 @EnableTransactionManagement(proxyTargetClass = true)
,使用 <tx:annotation-driven/>
,可以更改为 <tx:annotation-driven proxy-target-class="true"/>
。
If the controller implements an interface, and needs AOP proxying, you may need to
explicitly configure class-based proxying. For example, with @EnableTransactionManagement
you can change to @EnableTransactionManagement(proxyTargetClass = true)
, and with
<tx:annotation-driven/>
you can change to <tx:annotation-driven proxy-target-class="true"/>
.
请记住,从 6.0 开始,在接口代理中,Spring MVC 不会再仅根据接口上的类型级 |
Keep in mind that as of 6.0, with interface proxying, Spring MVC no longer detects
controllers based solely on a type-level |