This is an example context configuration for DispatchServlet:

@ComponentScan(basePackages = { "com" })
public class WebMvcContextConfiguration extends WebMvcConfigurerAdapter {
    public void addResourceHandlers(final ResourceHandlerRegistry registry) {
    public void addViewControllers(final ViewControllerRegistry registry) {
    public void configureDefaultServletHandling(final DefaultServletHandlerConfigurer configurer) {
    public void addInterceptors(InterceptorRegistry registry) {
    public HandlerInterceptor localeChangeInterceptor() {
        LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor();
        return localeChangeInterceptor;
    public LocaleResolver localeResolver() {
        return new CookieLocaleResolver();

@EnableWebMvc: annotation used together with @Configuration enables default Spring MVC configuration, equivalent to mvc:annotation-driven .

The mvc:annotation-driven declares explicit support for annotation-driven MVC controllers like @RequestMapping, @Controller, although support for those is the default behaviour.

The mvc:annotation-driven means that you can define spring beans dependencies without actually having to specify bean elements in xml or implement an interface or extend a base class. For example @Repository to tell spring that a class is a Dao without having to extend JpaDaoSupport or some other subclass of DaoSupport. Similarly @Controller tells spring that the the class specified contains methods that will handle http requests without you having to implement the Controller interface or extend a subclass that implements controller.

@ComponentScan: Make sure your classes annotated with @Controller will be added to the application context. Spring will scan the basePackages for Annotations.

Its telling spring that it should search the class path specified in basePackages for all the classes and look at each class to see if it has a @Controller, or @Repository, or @Service, or @Component annotation and if it does then Spring will register the class with the bean factory as if you had configured bean element in the xml configuration files.

WebMvcConfigurerAdapter: Extending from WebMvcConfigurerAdapter is not required, but it allows for the customization of the default Spring MVC configuration. The adapter enables us to register additional components like formatters, converters, custom validators, interceptors etc.

WebMvcConfigurerAdapter is an empty implementation of interface WebMvcConfigurer. This interface defines options for customizing default Spring MVC configuration. Any class that annotated with @Configuration and implemented this interface has a chance to customize Spring MVC at the start up.

Your class can directly implement WebMvcConfigurer, in this case, all the method defined in the interface has to be implemented. WebMvcConfigurerAdapter is for convenience, extends the empty implementation, you can override interested option, and let the other options keeps the default configuration.

ResourceHandlerRegistry and addResourceHandler: This class contains all registered resource handlers. The handler is represented by ResourceHandlerRegistration, framework will generate the handler according to the information contained in ResourceHandlerRegistration.

The addResourceHandler will return a ResourceHandlerRegistration instance which you can configure the resource handler. In this example, addResourceHandler added a handler based on url /resources/**/*. And then the handler call addResourceLocations to specify resource location from which to serve static content.

In this example, the url /resources/css/style.css will map to classpath: /META-INF/web-resources/css/style.css, the file can be located in a jar file.

setViewName: Its like resource handler, there are also ViewControllerRegistration, ViewControllerRegistry, addViewController.

A view controller does nothing more than return a specified view name and let the framework generate a controller for you. Because the only thing that the controller do is forward to a view such as JSP, write such a controller is trivial. With view controller, you can just specify the url pattern and a view name, the view name then may be resolved to a particular jsp file or htm file.

FormatterRegistry: converter can convert a string id to an entity bean. For example, a field named category in a form, the value is the category id, when controller get the submit request, using @ModelAttribute can convert the id to the category pojo.

LocaleResolver: see here

See also