@EnableWebMvc가 제공하는 빈(DelegatingWebMvcConfiguration.class, WebMvcConfigurationSupport.class)을 커스터마이징할 수 있는 기능을 제공하는 인터페이스
@Configuration
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {
@Override
public void configureViewResolvers(ViewResolverRegistry registry) {
registry.jsp("/WEB-INF/", ".jsp");
}
}
@Configuration
@ComponentScan
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {
@Override
public void configureViewResolvers(ViewResolverRegistry registry) {
registry.jsp("/WEB-INF/", ".jsp");
}
}
@Controller
public class HelloController {
@Autowired
HelloService helloService;
@GetMapping("/hello")
@ResponseBody
public String hello() {
return "Hello, " + helloService.getName();
}
@GetMapping("/sample")
public void sample(){
}
}
public class WebApplication implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.setServletContext(servletContext); // 추가
context.register(WebConfig.class);
context.refresh();
DispatcherServlet dispatcherServlet = new DispatcherServlet(context);
ServletRegistration.Dynamic app = servletContext.addServlet("app", dispatcherServlet);
app.addMapping("/app/*");
}
}
이렇게 @EnableWebMvc + implements WebMvcConfigurer + implements WebApplicationInitializer(DispatcherServlet 등록)이 스프링 부트 없이 스프링 MVC를 사용하는 방법이다.
@EnableWebMvc + implements WebMvcConfigurer를 사용하면 직접 빈을 등록하는 것보다 훨씬 쉽게 빈을 커스터마이징할 수 있다.
참고