처음부터 차근차근

WebMvcConfigurationSupport과 WebMvcConfigurer의 차이로 알아본 스프링 MVC 자동구성 변경 방법 본문

Framework/Spring

WebMvcConfigurationSupport과 WebMvcConfigurer의 차이로 알아본 스프링 MVC 자동구성 변경 방법

_soyoung 2022. 9. 10. 16:43
반응형

swagger 설정 파일 만들다가 configuration class에 

extends WebMvcConfigurationSupport

implements WebMvcConfigurer

함에 따라 결과가 달라져서 찾아보게 되었다.

 

WebMvcConfigurationSupport

https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupport.html

 

WebMvcConfigurationSupport (Spring Framework 5.3.22 API)

Add custom HandlerMethodArgumentResolvers to use in addition to the ones registered by default. Custom argument resolvers are invoked before built-in resolvers except for those that rely on the presence of annotations (e.g. @RequestParameter, @PathVariable

docs.spring.io

필자의 경우 config class 에 WebMvcConfigurationSupport를 상속받았을 때 controller에서 view를 load 찾아보지 못하고 dispatcherServlet 에러를 냈다.

그런데 상속받은 것을 지우고, WebMvcConfigurer을 implement하자 에러가 해결됐다.

 

WebconfiguraitonSupport를 상속받았을 때 에러가 났던 이유는 때문에 스프링 MVC 자동구성이 비활성화 됐기 때문이다.

스프링 MVC 자동구성은 WebMvcAutoConfiguration이 담당한다.

@Configuration
@ConditionalOnWebApplication(type = Type.SERVLET)
@ConditionalOnClass({ Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class })
@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE + 10)
@AutoConfigureAfter({ DispatcherServletAutoConfiguration.class,
		ValidationAutoConfiguration.class })
public class WebMvcAutoConfiguration {
	....
}

여기서 4번째 줄

@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)

이 어노테이션은 WebMvcConfigurationSupport 클래스의 bean이 BeanFactory에 포함되어 있지 않아야 함(bean이 있지 않아야 함)을 표시한다.

 

@ConditionalOnMissingBean 공식문서를에 보면

https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/autoconfigure/condition/ConditionalOnMissingBean.html

 

ConditionalOnMissingBean (Spring Boot 2.7.3 API)

@Conditional that only matches when no beans meeting the specified requirements are already contained in the BeanFactory. None of the requirements must be met for the condition to match and the requirements do not have to be met by the same bean. When plac

docs.spring.io

@Conditional that only matches when no beans meeting the specified requirements are already contained in the 
BeanFactory. None of the requirements must be met for the condition to match and the requirements do not have to be met by the same bean.

지정된 요구 사항을 충족하는 빈이 이미 BeanFactory에 포함되어 있지 않을 때만 일치하는 @Conditional. 조건이 일치하기 위해 어떤 요구사항도 충족되어서는 안 되며 동일한 Bean이 요구사항을 충족할 필요도 없습니다.

 

이렇게 자세한 내용을 볼 수 있다.

그래서 extends WebMvcConfigurationSupport을 하게되면 상속하는 것 만으로 WebMvcConfigurationSupport 클래서 bean이 생성되기 때문에 WebMvcAutoConfiguration이 활성화되지 않아 에러가 뜨게 된 것이다.

이와 마찬가지로 @EnableWebMvc를 사용하게 되면 WebMvcConfigurationSupport에서 구성한 스프링 MVC 구성을 불러오게 되므로 자동 mvc 구성이 비활성화된다.

 

 

WebMvcConfigurer

https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/servlet/config/annotation/WebMvcConfigurer.html

 

WebMvcConfigurer (Spring Framework 5.3.22 API)

 

docs.spring.io

WebMvcConfigurer은 자동구성된 스프링 MVC 구성에 Formatter, MessageConverter 등을 추가등록할 수 있게 해주는 인터페이스이다.

 

자동 스프링 MVC 구성을 변경하려면 아래와 같은 형태로 구성하면 된다.

@Configuration
public class WebMvcConfig implements WebMvcConfigurer, WebMvcRegistrations {
	// 내용
}

 WebMvcRegistrations : RequestMappingHandlerMappingRequestMappingHandlerAdapter와 ExceptionHandlerExceptionResolver를 재정의할 때 사용

 

결론 : config 클래스에 @Configuration 붙이고, WebMvcConfigurer을 implement 하면 스프링 자동 mvc 구성을 변경할 수 있다. 

 

 

 

참고 : http://honeymon.io/tech/2018/03/13/spring-boot-mvc-controller.html

 

[springboot] Spring MVC 자동구성 제어하기 - I'm honeymon(JiHeon Kim).

자동구성된 스프링 MVC 구성 조작하기 자동구성된 스프링 MVC 구성을 큰 변경없이 추가적인 조작하려면 다음과 같은 형태로 구성한다. 1 2 3 @Configuration public class WebMvcConfig implements WebMvcConfigurer, Web

honeymon.io

 

반응형

'Framework > Spring' 카테고리의 다른 글

스프링 부트의 유효성 검사  (0) 2022.09.18
custom exception  (0) 2022.09.13
controller advice로 404 error 처리하는 법(gradle 사용)  (0) 2022.09.08
JPA(수정 중)  (0) 2022.08.25
JaCoCo를 사용한 code coverage  (0) 2022.08.23
Comments