001/* 002 * Copyright 2002-2017 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.web.servlet.config.annotation; 018 019import java.lang.annotation.Documented; 020import java.lang.annotation.ElementType; 021import java.lang.annotation.Retention; 022import java.lang.annotation.RetentionPolicy; 023import java.lang.annotation.Target; 024 025import org.springframework.context.annotation.Import; 026 027/** 028 * Adding this annotation to an {@code @Configuration} class imports the Spring MVC 029 * configuration from {@link WebMvcConfigurationSupport}, e.g.: 030 * 031 * <pre class="code"> 032 * @Configuration 033 * @EnableWebMvc 034 * @ComponentScan(basePackageClasses = MyConfiguration.class) 035 * public class MyConfiguration { 036 * 037 * } 038 * </pre> 039 * 040 * <p>To customize the imported configuration, implement the interface 041 * {@link WebMvcConfigurer} and override individual methods, e.g.: 042 * 043 * <pre class="code"> 044 * @Configuration 045 * @EnableWebMvc 046 * @ComponentScan(basePackageClasses = MyConfiguration.class) 047 * public class MyConfiguration implements WebMvcConfigurer { 048 * 049 * @Override 050 * public void addFormatters(FormatterRegistry formatterRegistry) { 051 * formatterRegistry.addConverter(new MyConverter()); 052 * } 053 * 054 * @Override 055 * public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { 056 * converters.add(new MyHttpMessageConverter()); 057 * } 058 * 059 * } 060 * </pre> 061 * 062 * <p><strong>Note:</strong> only one {@code @Configuration} class may have the 063 * {@code @EnableWebMvc} annotation to import the Spring Web MVC 064 * configuration. There can however be multiple {@code @Configuration} classes 065 * implementing {@code WebMvcConfigurer} in order to customize the provided 066 * configuration. 067 * 068 * <p>If {@link WebMvcConfigurer} does not expose some more advanced setting that 069 * needs to be configured consider removing the {@code @EnableWebMvc} 070 * annotation and extending directly from {@link WebMvcConfigurationSupport} 071 * or {@link DelegatingWebMvcConfiguration}, e.g.: 072 * 073 * <pre class="code"> 074 * @Configuration 075 * @ComponentScan(basePackageClasses = { MyConfiguration.class }) 076 * public class MyConfiguration extends WebMvcConfigurationSupport { 077 * 078 * @Override 079 * public void addFormatters(FormatterRegistry formatterRegistry) { 080 * formatterRegistry.addConverter(new MyConverter()); 081 * } 082 * 083 * @Bean 084 * public RequestMappingHandlerAdapter requestMappingHandlerAdapter() { 085 * // Create or delegate to "super" to create and 086 * // customize properties of RequestMappingHandlerAdapter 087 * } 088 * } 089 * </pre> 090 * 091 * @author Dave Syer 092 * @author Rossen Stoyanchev 093 * @since 3.1 094 * @see org.springframework.web.servlet.config.annotation.WebMvcConfigurer 095 * @see org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport 096 * @see org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration 097 */ 098@Retention(RetentionPolicy.RUNTIME) 099@Target(ElementType.TYPE) 100@Documented 101@Import(DelegatingWebMvcConfiguration.class) 102public @interface EnableWebMvc { 103}