공식 문서에 @EnableWebMvc를 붙이지 말라고 나와 있다.
이미 Spring MVC 자동설정이 @EnableWebMvc가 설정하는 것을 수행하고 있기에 둘을 같이 사용하지 않아도 된다고 한다.
@EnableWebMVC를 사용하는 경우
자동설정을 사용하지 않고 Spring MVC에 대한 완전한 제어를 원할 때 @Configuration과 함께 @EnableWebMVC를 사용하면 된다고 한다.
동일한 방법으로 @Configuration과 DelegatingWebMvcConfiguration을 사용하는 방법이 있다고 한다.
WebMvcAutoConfiguration에서 @EnableWebMvc와 동일한 역할을 하는 Configuration이 존재한다.
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@Import(DelegatingWebMvcConfiguration.class)
public @interface EnableWebMvc {
}
@EnableWebMvc를 확인해 보면 DelegatingWebMvcConfiguration을 임포트 하는 것을 확인할 수 있다.
A subclass of WebMvcConfigurationSupport that detects and delegates to all beans of type WebMvcConfigurer allowing them to customize the configuration provided by WebMvcConfigurationSupport. This is the class actually imported by @EnableWebMvc.
주석에 따르면 DelegatingWebMvcConfiguration는 사용자가 정의한 WebMvcConfigurer 유형의 모든 빈을 감지하고 대신 변경된 구성을 적용하는 클래스라고 한다.
그리고 주석에서는 해당 클래스는 @EnableWebMvc에 의해 임포트 된다고 한다.
하지만 WebMvcAutoConfiguration에서는 DelegatingWebMvcConfiguration를 상속하는 EnableWebMvcConfiguration을 임포트 하며 @EnableWebMvc와 동일한 역할을 수행하고 있는 것을 확인할 수 있다.
'스프링' 카테고리의 다른 글
BeanPostProcessor (1) | 2025.01.16 |
---|---|
@Async 정리 (0) | 2025.01.02 |
이벤트 리스너 정리 (0) | 2025.01.02 |
Spring에서의 Filter 간단 정리 (2) | 2024.09.19 |
SpringBoot에서 HTTP 요청을 처리하는 과정을 살펴보며 (DispatcherServlet) (0) | 2024.08.26 |