对于最新稳定版本,请使用 Spring Framework 7.0.6spring-doc.cadn.net.cn

启用 MVC 配置

在 Java 配置中,您可以使用 @EnableWebMvc 注解来启用 MVC 配置,如下例所示:spring-doc.cadn.net.cn

@Configuration
@EnableWebMvc
public class WebConfig {
}
@Configuration
@EnableWebMvc
class WebConfig

在 XML 配置中,您可以使用 <mvc:annotation-driven> 元素来启用 MVC 配置,如下例所示:spring-doc.cadn.net.cn

<?xml version="1.0" encoding="UTF-8"?>
<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 MVC 基础设施 Bean,并根据类路径上可用的依赖项进行适配(例如,用于 JSON、XML 等的负载转换器)。spring-doc.cadn.net.cn