Spring/Spring boot

[10] Spring Web MVC: Introduction

낙타선생 2020. 6. 15. 13:18
반응형

Spring boot MVC reference site :

https://docs.spring.io/spring/docs/5.0.7.RELEASE/spring-framework-reference/web.html#spring-web

 

Web on Servlet Stack

This part of the reference documentation covers support for Servlet stack, WebSocket messaging that includes raw WebSocket interactions, WebSocket emulation via SockJS, and pub-sub messaging via STOMP as a sub-protocol over WebSocket. 4.1. Introduction The

docs.spring.io

 

 

Spring boot가 제공하는 자동 설정으로 인해 Spring boot test 의존성만 받아두고 별다른 설정 file을 작성하지 않아도 spring web mvc 개발을 바로 시작할 수 있습니다. 아래 그림과 같이 의존성 중에서 spring-boot-autoconfigure/META-INF/spring.factories file에 정의된 package에 해당하는 class들을 사용하여 자동설정을 진행합니다. (자동 설정 참고)

자동설정 위치

WebMvcAutoConfiguration class의 내용을 대강 살펴보면 다음과 같습니다.

...

@Configuration(
    proxyBeanMethods = false
)
@ConditionalOnWebApplication(
    type = Type.SERVLET	//	①
)
@ConditionalOnClass({Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class})
@ConditionalOnMissingBean({WebMvcConfigurationSupport.class})
@AutoConfigureOrder(-2147483638)
@AutoConfigureAfter({DispatcherServletAutoConfiguration.class, TaskExecutionAutoConfiguration.class, ValidationAutoConfiguration.class})
public class WebMvcAutoConfiguration {

...

① WebApplicationType은 servlet. 즉, web application입니다.

 

 

Spring MVC를 확장하기 위해서는 WebMvcConfigurer interface를 구현한 Congirutation을 사용합니다.

@Congfiguration
public class WebConfig implements WebMvcConfigurer {
	//bean 등록을 위한 method 구현
}

 

Spring MVC를 재정의하기 원하면 위의 확장을 위한 class에 @EnableWebMvc를 붙여주면 됩니다. 하지만 직접 web MVC를 정의할 일은 거의 없습니다.

@Congfiguration
@EnableWebMvc	//완전히 새로운 Web Mvc를 정의합니다. Web Mvc관련 모든 설정을 직접 지정해야 합니다.
public class WebConfig implements WebMvcConfigurer {
	//bean 등록을 위한 method 구현
}