WebApplicationContext 계층
DispatcherServlet은 그 자체가 서블릿이기 때문에 한 개 이상의 DispatcherServlet을 설정하는 것이 가능하다. 예를 들어 다음과 같이 웹 페이지를 위한 DispatcherServlet과 REST 기반의 웹 서비스 연동을 위한 DispatcherServlet을 나누어 설정했다고 하자.
<servlet>
<servlet-name>front</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/front.xml
</param-value>
</init-param>
</servlet>
<servlet>
<servlet-name>rest</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/rest.xml
</param-value>
</init-param>
</servlet>
위 설정에서 두 DispatcherSerlvet은 각각 별도의 WebApplicationContext를 생성하게 된다. 이때 front.xml에서는 rest.xml에 설정된 빈 객체를 사용할 수 없게 된다.
서로 다른 DispatchServlet이 공통 빈을 필요로 하는 경우, ContextLoaderListener를 사용하여 공통으로 사용될 빈을 설정할 수 있게 된다. 다음과 같이 ContextLoaderListener를 ServletListener로 등록하고 contextConfigLocation 컨텍스트 파라미터를 이용하여 공통으로 사용될 빈 정보를 담고 있는 설정 파일 목록을 지정하면 된다.
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/front.xml, /WEB-INF/rest.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet>
<servlet-name>front</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
</servlet>
<servlet>
<servlet-name>rest</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
</servlet>
ContextLoaderListener가 생성하는 WebApplicationContext는 웹 어플리케이션에서 루트 컨텍스트가 되며, DispatcherServlet이 생성하는 WebApplicationContext는 루트 컨텍스트를 부모로 사용하는 자식 컨텍스트가 된다. 이때 자식은 루트가 제공하는 빈을 사용할 수 있기 때문에 공통으로 필요로 하는 빈을 ContextLoaderListener를 이용하여 설정하는 것이다.
ContextLoaderListener는 conetxtConfigLocation 컨텍스트 파라미터를 명시하지 않으면 /WEB-INF/applicationContext.xml을 설정 파일로 사용한다. 또한 클래스패스에 위치한 파일로부터 설정 정보를 읽어 오고 싶다면 "classpath:" 접두어를 사용하여 설정 파일을 명시하면 된다.
@Configuration 설정 클래스를 사용한다면, 다음과 같이 contextClass 컨텍스트 파라미터를 이용해서 WebApplicationContext 구현체로 AnnotationConfigWebApplicationContext를 지정해주고, contextConfigLocation 컨텍스트 파라미터의 값으로 사용할 자바 설정 클래스를 지정해주면 된다.
<context-param>
<param-name>contextClass</param-name>
<param-value>
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
com.mypackage.ApplicationConfig
</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
Reference
- 웹 개발자를 위한 Spring 4.0 프로그래밍 (최범균 저)
'Spring' 카테고리의 다른 글
| [Spring] 스프링 MVC(17) - 핸들러, HandlerMapping, HandlerAdapter (0) | 2022.04.07 |
|---|---|
| [Spring] 스프링 MVC(16) - DelegatingFilterProxy (0) | 2022.04.06 |
| [Spring] 스프링 MVC(14) - HandlerInterceptor (0) | 2022.04.05 |
| [Spring] 스프링 MVC(13) - 추가 설정 (0) | 2022.03.17 |
| [Spring] 스프링 MVC(12) - 컨트롤러의 파라미터와 리턴 타입 (0) | 2022.03.16 |