I'm learning about Spring MVC with Java configuration (no xml) and I have a simple question. I see 2 approaches of making Spring bean configuration:
approach 1:
@Configuration
@EnableWebMvc
@ComponentScan(basePackages="com.demo.springmvc")
public class DemoAppConfig {
// define a bean for ViewResolver
@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setPrefix("/WEB-INF/view/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
}
approach 2:
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.example")
public class SpringConfig implements WebMvcConfigurer{
@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setViewClass(JstlView.class);
viewResolver.setPrefix("/WEB-INF/pages/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
}
So one way is by implementing the WebMvcConfigurer interface and another way in not implementing the WebMvcConfigurer interface. I want to ask you what is the difference? What's happen when I implement this interface and what's happen when I don't implement it. Any feedback will be appreciated.