Spring MVC, as many other web frameworks, is designed around the front controller pattern where a central Servlet, the DispatcherServlet, provides a shared algorithm for request processing, while actual work is performed by configurable delegate components. This model is flexible and supports diverse workflows.

Context Hierarchy

FC는 MVC에서 아예 디자인 패턴으로 분류하는 듯함.

클라이언트로부터의 요청 처리는 FC가 자체 알고리즘으로 수행한다. 요청에 맞는 실제의 작업은 configurable delegate 컴포넌트가 수행한다.

The DispatcherServlet, as any Servlet, needs to be declared and mapped according to the Servlet specification by using Java configuration or in web.xml. In turn, the DispatcherServlet uses Spring configuration to discover the delegate components it needs for request mapping, view resolution, exception handling, and more.

FC는 서블릿 스펙에 맞는 web.xml이나 java config에 맞춰서 매핑되어야 한다…. 물론 이건 당연한 이야기임. FC가 아니어도 서블릿은 요청과 매핑되어야 한다.

스프링의 DispatcherServlet은 요청에 맞는 위임할 컴포넌트 찾기 위해 스프링의 Config를 사용한다.

여기서 내 생각보다 더 나아가는건, DispatcherServlet이 매핑시키는건 단순 컨트롤러, 핸들러가 아니라 뷰 리졸버, 그리고 예외 처리 객체들가지 매핑시켜준다고 함…

이렇게 다양한 종류 객체를 Dispatcher Servlet이 매핑시켜주기 위해 컨트롤러를 인터페이스화 하고 어댑터 패턴을 적용하는듯…?

아래 코드는 DispatcherServlet을 등록하고 초기화하는 클래스이다. Initializer라고 부름.

이 코드는 두가지 일을 수행한다.

  1. 스프링 컨텍스트를 생성, 초기화한다.
  2. DispatcherServlet을 생성, 초기화한다.

참고로 아래 코드는 xml이 아니라 java config를 이용하는 코드임. (전에 배운 AppConfig.java)

public class MyWebApplicationInitializer implements WebApplicationInitializer {

	@Override
	public void onStartup(ServletContext servletContext) {

		// Load Spring web application configuration
		AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
		context.register(AppConfig.class);

		// Create and register the DispatcherServlet
		DispatcherServlet servlet = new DispatcherServlet(context);
		ServletRegistration.Dynamic registration = servletContext.addServlet("app", servlet);
		registration.setLoadOnStartup(1);
		registration.addMapping("/app/*");
	}
}

위 코드를 보면, 스프링 컨테이너를 관리하는 context객체를 이 단에서 생성해주고 있다.

그리고 context를 초기화해야한다. 이를 context.register(AppConfig.class) 로 가능케 하고 있음.

이후 Dispatcher서블릿을 생성하고 초기화하는데, 여기서 재미있는건, 이 DispatcherSerlvet객체의 인자에 context를 주고 있다….!!!! 이렇게 FC에 context를 전달하도록 스프링은 DS를 설계했다…