|
此版本仍在开发中,尚未稳定。如需最新的稳定版本,请使用 Spring Framework 7.0.6! |
启用MVC配置
您可以使用@0注解通过编程方式启用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>
| 自7.0版本起,Spring MVC的XML配置命名空间支持已被弃用。 目前尚无完全移除的计划,但XML配置将不会更新以遵循Java配置模型。 |
在使用Spring Boot时,您可能希望使用@Configuration个类型的WebMvcConfigurer类,但不包含@EnableWebMvc,以保留Spring Boot MVC的自定义设置。更多详细信息,请参阅MVC配置API部分和专门的Spring Boot文档。 |
上一个示例注册了多个Spring MVC 基础结构bean,并适应类路径上的依赖项(例如,用于JSON、XML和其他格式的有效载荷转换器)。