此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 Spring Framework 6.2.0! |
启用 MVC 配置
您可以使用@EnableWebMvc
注解以使用编程配置启用 MVC 配置,或者<mvc:annotation-driven>
使用 XML 配置,如下例所示:
-
Java
-
Kotlin
-
Xml
@Configuration
@EnableWebMvc
public class WebConfiguration {
}
@Configuration
@EnableWebMvc
class WebConfiguration {
}
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<mvc:annotation-driven/>
</beans>
使用 Spring Boot 时,您可能希望使用@Configuration 类型的类WebMvcConfigurer 但没有@EnableWebMvc 保留 Spring Boot MVC 自定义。在 MVC Config API 部分和专用的 Spring Boot 文档中查看更多详细信息。 |
前面的示例注册了许多 Spring MVC 基础结构 bean 并适应依赖项 在 Classpath 上可用(例如,JSON、XML 等的有效负载转换器)。