ApplicationContext vs WebApplicationContext
서블릿 컨테이너는 서블릿의 생명주기를 관리한다. 서블릿 컨테이너 내부에는 여러개의 서블릿이 존재 하는데, 서블릿의 집합이 하나의 웹 앱을 만든다. Application Context란 여러개의 웹 앱들이 공유하는 환경을 의미한다. 따라서 이곳에, Dao와 관련된 빈이나 서비스 로직에 관련된 빈을 모두 모아놓으면 된다. 왜냐면 이러한 빈들은 여러 앱들에서 공유 될 수 있기 때문이다. 즉 root application context이다.
Servlet Application Context == Web Application Context이다. 각 앱 내부에 하나씩 존재한다. 즉, 웹앱컨텍스트는 하나의 서블릿 컨테이너 내부에서 여러개가 존재 할 수 있다.
2.3 Difference between the two contexts
So the best part of this article, is what’s the difference between the two contexts?
Firstly, as already mentioned, ApplicationContext is the root-context, that has bean configurations we might want to use (and re-use) across the entire application as singletons. There is always a single application context in a web application. Whereas, there can be multiple WebApplicationContexts for each of the dispatcher servlets we specify in our application’s web.xml.
It’s always better to keep a clear separation between middle-tier services such as business logic components and data access classes (which we prefer defining in the ApplicationContext XML), and web-related components such as controllers and view-resolvers (which we prefer defining in the respective dispatcher-servlet‘s WebApplicationContext).
Configuring contextLoaderListener is completely optional. We can boot up a Spring application with just the dispatcher-servlet, without even configuring contextLoaderListener (that loads up the root-context).