ServletContext란
Defines a set of methods that a servlet uses to communicate with its servlet container, for example, to get the MIME type of a file, dispatch requests, or write to a log file.
There is one context per "web application" per Java Virtual Machine.
출처: https://docs.oracle.com/javaee/7/api/javax/servlet/ServletContext.html
The object of ServletContext provides an interface between the container and servlet.
The ServletContext object can be used to get configuration information from the web.xml file.
출처: https://www.javatpoint.com/servletcontext
이를 정리하면
ServletContext 인터페이스: servlet과 servletcontainer 사이의 상호작용에 사용되는 인터페이스
ServletContext 객체: Web Application당 하나만 존재하며 web.xml 정보를 가져올 수 있게 하는 객체
라 생각된다.
ApplicationContext
여러 구성정보를 제공하는 인터페이스
ApplicationContext 객체는 BeanFactory와 함께 Spring IoC Container이다.(Spring container = ApplicationContext)
ApplicationContext 생성되면서 인스턴스를 로딩하는 Pre-Loading, BeanFactory는 요청을 받을때 인스턴스를 로딩하는 Lazy-Loading 방식.
ApplicationContext는 BeanFactory에 비해 다음과 같은 엔터프라이즈용 기능을 추가로 제공한다.
- Property 파일을 확인하여 등록된 리스너에 이벤트 게시
- application components에 접근하는 메소드
- 국제화 기능
- File resources 로딩
WebApplicationContext
WebApplicationContext는 ApplicationContext를 확장한 하위 인터페이스로 getServletContext() 메소드를 추가하여 컨테이너와 통신할 수 있게 설계되었다. web.xml에 정의된 application context들이 Web Application context이다.
구현체로는 Xml 설정 파일을 이용하는 XmlWebApplicationContext, 애너테이션 기반의 AnnotationConfigWebApplicationContext 등이 있습니다.
용어 정리
Spring Context: 빈 관리 기능 + 추가 기능
Servlet Context: dispatcherservlet과 같은 서블릿들의 빈을 관리하는 Context, 주로 view와 관련된 빈을 관리(ViewResolver, HandlerMapping, Controller), ServletContext 인터페이스가 어플케이션 전체의 Context라면 Servlet Context는 서블릿의 Context(여기를 보면 "ServletContext는 WebAppContext나 ApplicationContext 불려야 한다"고 주장하는 글이 있음)
Root Context = Parent Context: 각 servlet들의 공통적인 부분(Service, Repository)을 모아서 관리하는 Context, Servlet Context의 부모로 Servlet Context에서 Root Context를 참조 가능(반대는 불가능)
WebApplicationContext: 웹 애플리케이션에서 사용하는 Context, Servlet Context와 Root Context는 WebApplicationContext 인터페이스를 사용하여 구현되어 있음
References
docs spring WebApplicationContext
What is the difference between ApplicationContext and WebApplicationContext in Spring MVC?