엔지니어로 가는 길

spring web mvc에서 서블릿 설정하기 본문

프로그래밍/Spring

spring web mvc에서 서블릿 설정하기

탐p슨 2020. 3. 9. 22:44
728x90

서블릿 3.0부터 서블릿 컨테이너를 프로그래밍적으로 설정할지 web.xml를 이용하여 설정할지 선택할 수 있다.

 

WebApplicationInitializer는 Spring MVC에서 제공하는 인터페이스로 이 인터페이스를 구현하면 서블릿 컨테이너를 초기화할 수 있다. 아래의 예시를 참고하자.

 

https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#mvc-container-config

 

AbstractDispatcherServletInitializer는 WebApplicationInitializer를 구현한 추상 클래스로, 서블릿 매핑과 DispatcherServlet 설정의 위치를 구체화하는 메소드를 제공하기 때문에 이를 이용한다면 DispatcherServlet을 더 쉽게 등록할 수 있다.

 

 

 

자바를 기반으로한 스프링 설정 애플리케이션에서는 아래의 코드를 추천한다.

 

https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#mvc-container-config

 

만약 XML 기반 스프링 설정을 사용한다면 아래의 코드와 같이 AbstractDispatcherServletInitializer를 직접 상속받아야 한다.

 

https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#mvc-container-config

 

AbstractDispatcherServletInitializer는 아래와 같이 손쉽게 필터를 추가하고 DispatcherServlet에 자동으로 매핑시키는 손쉬운 방법을 제공한다.

 

https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#mvc-container-config

 

필터는 타입에 기반한 기본이름으로 추가되며 DispatcherServlet에 자동으로 추가된다.

 

만약 DispatcherServlet를 커스터마이징하고 싶다면 createDispatcherServlet를 오버라이드 하면 된다.

 

참고자료

https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#mvc-servlet-config

728x90
Comments