/*
 * Copyright 2002-2017 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.web.servlet.config.annotation;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.springframework.context.annotation.Import;

Adding this annotation to an @Configuration class imports the Spring MVC configuration from WebMvcConfigurationSupport, e.g.:
@Configuration
@EnableWebMvc
@ComponentScan(basePackageClasses = MyConfiguration.class)
public class MyConfiguration {
}

To customize the imported configuration, implement the interface WebMvcConfigurer and override individual methods, e.g.:

@Configuration
@EnableWebMvc
@ComponentScan(basePackageClasses = MyConfiguration.class)
public class MyConfiguration implements WebMvcConfigurer {
	   @Override
	   public void addFormatters(FormatterRegistry formatterRegistry) {
        formatterRegistry.addConverter(new MyConverter());
	   }
	   @Override
	   public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.add(new MyHttpMessageConverter());
	   }
}

Note: only one @Configuration class may have the @EnableWebMvc annotation to import the Spring Web MVC configuration. There can however be multiple @Configuration classes implementing WebMvcConfigurer in order to customize the provided configuration.

If WebMvcConfigurer does not expose some more advanced setting that needs to be configured consider removing the @EnableWebMvc annotation and extending directly from WebMvcConfigurationSupport or DelegatingWebMvcConfiguration, e.g.:

@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
   }
}
Author:Dave Syer, Rossen Stoyanchev
See Also:
Since:3.1
/** * Adding this annotation to an {@code @Configuration} class imports the Spring MVC * configuration from {@link WebMvcConfigurationSupport}, e.g.: * * <pre class="code"> * &#064;Configuration * &#064;EnableWebMvc * &#064;ComponentScan(basePackageClasses = MyConfiguration.class) * public class MyConfiguration { * * } * </pre> * * <p>To customize the imported configuration, implement the interface * {@link WebMvcConfigurer} and override individual methods, e.g.: * * <pre class="code"> * &#064;Configuration * &#064;EnableWebMvc * &#064;ComponentScan(basePackageClasses = MyConfiguration.class) * public class MyConfiguration implements WebMvcConfigurer { * * &#064;Override * public void addFormatters(FormatterRegistry formatterRegistry) { * formatterRegistry.addConverter(new MyConverter()); * } * * &#064;Override * public void configureMessageConverters(List&lt;HttpMessageConverter&lt;?&gt;&gt; converters) { * converters.add(new MyHttpMessageConverter()); * } * * } * </pre> * * <p><strong>Note:</strong> only one {@code @Configuration} class may have the * {@code @EnableWebMvc} annotation to import the Spring Web MVC * configuration. There can however be multiple {@code @Configuration} classes * implementing {@code WebMvcConfigurer} in order to customize the provided * configuration. * * <p>If {@link WebMvcConfigurer} does not expose some more advanced setting that * needs to be configured consider removing the {@code @EnableWebMvc} * annotation and extending directly from {@link WebMvcConfigurationSupport} * or {@link DelegatingWebMvcConfiguration}, e.g.: * * <pre class="code"> * &#064;Configuration * &#064;ComponentScan(basePackageClasses = { MyConfiguration.class }) * public class MyConfiguration extends WebMvcConfigurationSupport { * * &#064;Override * public void addFormatters(FormatterRegistry formatterRegistry) { * formatterRegistry.addConverter(new MyConverter()); * } * * &#064;Bean * public RequestMappingHandlerAdapter requestMappingHandlerAdapter() { * // Create or delegate to "super" to create and * // customize properties of RequestMappingHandlerAdapter * } * } * </pre> * * @author Dave Syer * @author Rossen Stoyanchev * @since 3.1 * @see org.springframework.web.servlet.config.annotation.WebMvcConfigurer * @see org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport * @see org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration */
@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) @Documented @Import(DelegatingWebMvcConfiguration.class) public @interface EnableWebMvc { }