1

I have this code:

package biz.tugay.springJuly18.config;
/* User: koray@tugay.biz Date: 18/07/15 Time: 15:09 */

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

@SuppressWarnings(value = "unused")
public class MyWebAppInnit extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class<?>[]{RootConfigClass.class};
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class<?>[]{ServletConfigClass.class};
    }

    @Override
    protected String[] getServletMappings() {
        return new String[]{"/"};
    }

}

and the ServletConfig.class

package biz.tugay.springJuly18.config;
/* User: koray@tugay.biz Date: 18/07/15 Time: 15:10 */

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

@Configuration
@ComponentScan(basePackages = "biz.tugay.springJuly18.web")
public class ServletConfigClass {

    @Bean
    public ViewResolver viewResolver() {
        InternalResourceViewResolver resolver =
                new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/views/");
        resolver.setSuffix(".jsp");
        resolver.setExposeContextBeansAsAttributes(true);
        return resolver;
    }

}

and finally a Controller:

package biz.tugay.springJuly18.web;
/* User: koray@tugay.biz Date: 18/07/15 Time: 12:53 */

import biz.tugay.springJuly18.service.MyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class MyWeb {

    @Autowired
    private MyService myService;

    @RequestMapping("/test")
    public ModelAndView helloWorld() {
        ModelAndView mv = new ModelAndView();
        mv.addObject("message", myService.sayHello());
        mv.setViewName("koko");
        return mv;
    }

}

When I deploy this to Tomcat and go to /test I will be redirected to koko.jsp . So why does this work without @EnableWebMvc?

According to this answer here it should not?

Here is RootConfig.java

package biz.tugay.springJuly18.config;
/* User: koray@tugay.biz Date: 18/07/15 Time: 15:10 */

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan(basePackages = "biz.tugay.springJuly18.service")
public class RootConfigClass {
}
Community
  • 1
  • 1
Koray Tugay
  • 22,894
  • 45
  • 188
  • 319

1 Answers1

5

The DispatcherServlet has two fields

/** List of HandlerMappings used by this servlet */
private List<HandlerMapping> handlerMappings;

/** List of HandlerAdapters used by this servlet */
private List<HandlerAdapter> handlerAdapters;

whose values decide how a request will be handled (among other things). These fields are initialized and populated with values when the DispatcherServlet is initialized by the container. The values come from the context configuration you specify. If your context configuration doesn't provide at least one value of the appropriate type, the DispatcherServlet uses some defaults.

These defaults are included in a file named DispatcherServlet.properties which is in the same package as DispatcherServlet.

For HandlerMapping, the values (class names) are

org.springframework.web.servlet.HandlerMapping=org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping,\
    org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping

org.springframework.web.servlet.HandlerAdapter=org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter,\
    org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter,\
    org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter

The javadoc of AnnotationMethodHandlerAdapter states

Implementation of the HandlerAdapter interface that maps handler methods based on HTTP paths, HTTP methods and request parameters expressed through the RequestMapping annotation.

In other words, this implementation also uses methods annotated with @RequestMapping.

However, AnnotationMethodHandlerAdapter is deprecated

in Spring 3.2 in favor of RequestMappingHandlerAdapter

@EnabledWebMvc instead registers a RequestMappingHandlerAdapter, which is much more sophisticated.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724