在@Configuration注解的配置类中添加,用于为该应用添加SpringMVC的功能,即添加之后可以在项目中,可以使用@RequestMapping,@Controller等注解来定义请求处理与请求uri的映射和其他SpringMvc提供的功能。使用方法如下:
@Configuration @EnableWebMvc @ComponentScan(basePackageClasses = MyConfiguration.class) public class MyConfiguration { }
提供了SpringMVC的默认配置。
如果存在多个@Configuration注解的类,则只能在其中一个类添加这个注解。而其他注解类如果需要自定义SpringMVC的配置来替代默认配置,则可以通过实现WebMvcConfigurer接口来支持。
或者在@EnableWebMvc注解的类自身来实现WebMvcConfigurer接口,然后在该类中重写需要覆盖的默认配置所对应的方法或者添加相关配置。
最佳实践:整个项目对于SpringMVC相关的配置,只使用一个配置类,在这个配置类使用@EnableWebMvc注解,并实现WebMvcConfigurer接口来自定义需要重写的默认配置,如通过addResourceHandlers方法来自定义静态资源的访问,默认是所有请求都是使用DispatcherSerlvet来处理,静态资源访问会404。如下:
public void addResourceHandlers(final ResourceHandlerRegistry registry) { // addResourceHandler:配置匹配的静态资源的请求路径 // addResourceLocations:配置静态资源的位置 // 即请求路径为/static/**的请求都在/resources/目录查找 registry.addResourceHandler("/static/**").addResourceLocations("/resources/"); }
除了可以在配置类加上@EnableWebMvc注解之外,也可以直接继承WebMvcConfigurationSupport或DelegatingWebMvcConfiguration,而不使用@EnableWebMvc注解,因为@EnableWebMvc内部也是使用WebMvcConfigurationSupport来完成SpringMVC默认配置的添加的。
如果是使用继承这两个类的方式,则需要在该子类中添加上@Configuration注解。对于覆盖的方法,如果父类方法存在@Bean注解,则在该子类对应的方法也要加上@Bean注解。
继承WebMvcConfigurationSupport:如果项目中没有通过使用WebMvcConfigurer接口的实现类来提供SpringMVC的配置,则可以只使用一个WebMvcConfigurationSupport的子类来启动和自定义SpringMVC的功能。因为@EnableWebMvc其实还有一个功能是汇集项目中所有实现了WebMvcConfigurer接口的类。而WebMvcConfigurationSupport是没有汇集项目中WebMvcConfigure接口实现类的功能的。示例如下:
@Configuration @ComponentScan(basePackageClasses = { MyConfiguration.class }) public class MyConfiguration extends WebMvcConfigurationSupport { @Override public void addFormatters(FormatterRegistry formatterRegistry) { formatterRegistry.addConverter(new MyConverter()); } @Bean public RequestMappingHandlerAdapter requestMappingHandlerAdapter() { // Create or delegate to "super" to create and // customize properties of RequestMappingHandlerAdapter } }
继承DelegatingWebMvcConfiguration:DelegatingWebMvcConfiguration是WebMvcConfigurationSupport的拓展子类,如果项目中也存在其他实现WebMvcConfigurer接口来提供配置的类,则可以继承DelegatingWebMvcConfiguration来替代@EnableWebMvc,两者提供的功能是一样的。
基于Java编程方式,使用@EnableWebMvc注解启用SpringMvc;与基于XML方式,在dispatcher-servlet.xml(即DispatcherServlet所在的ApplicationContext对应的配置文件)中,添加 <mvc:annotation-driven />标签的方式效果是等价的。
从注解定义可知使用了@Import来导入DelegatingWebMvcConfiguration.class。
@EnableWebMvc在内部是使用WebMvcConfigurationSupport来实现SpringMVC功能的支持和提供默认配置。而DelegatingWebMvcConfiguration是的作用就是汇总项目中所有实现了WebMvcConfigurer接口的类,自身也是使用了@Configuration注解。
@Configuration public class DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport { ... }
WebMvcConfigurationSupport在项目中自动创建了以下这些SpringMvc中请求处理的核心组件:
HandlerMapping:请求处理器与请求uri映射HandlerAdapter:请求执行器HandlerExceptionResolverComposite:请求处理异常处理器AntPathMatcher:路径匹配其他功能子组件:ContentNegotiationManager,DefaultFormattingConversionService,OptionalValidatorFactoryBean,HttpMessageConverter