配置MVC的两种方式【mvc:annotation-driven】
配置使用MVC的两种方式【注解】以及【XML】
注解方式【EnableWebMvc】
在Configuration注解的类中,使用EnableWebMvc以启用MVC 配置
1 @Configuration 2 @EnableWebMvc 3 public class WebConfig { 4 5 }
XML方式【mvc:annotation-driven】
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:mvc="http://www.springframework.org/schema/mvc" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xsi:schemaLocation=" 6 http://www.springframework.org/schema/beans 7 http://www.springframework.org/schema/beans/spring-beans.xsd 8 http://www.springframework.org/schema/mvc 9 http://www.springframework.org/schema/mvc/spring-mvc.xsd"> 10 11 <mvc:annotation-driven/> 12 13 </beans>
作用
上述配置作用一致,都是注册了Bean,RequestMappingHandlerMapping,RequestMappingHandlerAdapter,ExceptionHandlerExceptionResolver
以上bean都是用来使用@RequestMapping
, @ExceptionHandler等注解来处理Controller中的方法。
在老的版本中,是自动注册DefaultAnnotationHandlerMapping 与AnnotationMethodHandlerAdapter 这两个bean,但Spring在版本3.2中已经废弃两个类。
并通过文档得知,其注册的是上述三个类。