启用 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 配置 API 部分 以及 Spring Boot 官方文档。
前面的示例注册了一些 Spring MVC 基础设施 Bean,并根据类路径上可用的依赖项进行适配(例如,用于 JSON、XML 等的有效负载转换器)。