@Controller

您可以使用标准 Spring bean 定义定义控制器 bean。@Controller 刻板印象允许自动检测,并且与 Spring 对检测类路径中的 @Component 类和自动为它们注册 bean 定义的一般支持保持一致。它还充当了被注释类的刻板印象,表明了其作为 Web 组件的角色。

You can define controller beans by using a standard Spring bean definition. The @Controller stereotype allows for auto-detection and is 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:

Java
@Configuration
@ComponentScan("org.example.web") (1)
public class WebConfiguration {

	// ...
}
1 Scan the org.example.web package.
Kotlin
@Configuration
@ComponentScan("org.example.web") (1)
class WebConfiguration {

	// ...
}
2 Scan the org.example.web package.

@RestController 是一个 composed annotation,它本身带有 @Controller@ResponseBody 的元注解,它指示一个控制器,其每个方法都将继承类型级别的 @ResponseBody 注解,因此将直接写入响应主体,而不是使用 HTML 模板进行视图解析和呈现。

@RestController is a composed annotation that is itself meta-annotated with @Controller and @ResponseBody, indicating 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 WebFlux 不再仅仅基于接口上的类型级 @RequestMapping 注释检测控制器。请启用基于类的代理,否则接口还必须有一个 @Controller 注释。

Keep in mind that as of 6.0, with interface proxying, Spring WebFlux no longer detects controllers based solely on a type-level @RequestMapping annotation on the interface. Please, enable class based proxying, or otherwise the interface must also have an @Controller annotation.