启用 MVC 配置

您可以使用@EnableWebMvc注解以使用编程配置启用 MVC 配置,或者<mvc:annotation-driven>使用 XML 配置,如下例所示:spring-doc.cadn.net.cn

@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 等的有效负载转换器)。spring-doc.cadn.net.cn