Programming

Spring MVC에서 ApplicationContext와 WebApplicationContext의 차이점은 무엇입니까?

procodes 2020. 5. 21. 21:20
반응형

Spring MVC에서 ApplicationContext와 WebApplicationContext의 차이점은 무엇입니까?


응용 프로그램 컨텍스트와 웹 응용 프로그램 컨텍스트의 차이점은 무엇입니까?

WebApplicationContextSpring MVC 아키텍처 지향 응용 프로그램에 사용되는 것을 알고 있습니까?

ApplicationContextMVC 응용 프로그램에서 어떤 용도로 사용되는지 알고 싶습니다 . 그리고 어떤 종류의 콩이 정의되어 ApplicationContext있습니까?


웹 애플리케이션 컨텍스트 확장 애플리케이션 컨텍스트는 표준 javax.servlet.ServletContext 와 작동하도록 설계되어 컨테이너와 통신 할 수 있습니다.

public interface WebApplicationContext extends ApplicationContext {
    ServletContext getServletContext();
}

WebApplicationContext에서 인스턴스화 된 Bean은 ServletContextAware 인터페이스를 구현하는 경우 ServletContext를 사용할 수도 있습니다.

package org.springframework.web.context;
public interface ServletContextAware extends Aware { 
     void setServletContext(ServletContext servletContext);
}

getResourceAsStream () 메소드를 호출하여 WEB-INF 자원 (xml 구성 등)에 액세스하는 것과 같이 ServletContext 인스턴스와 관련하여 가능한 많은 작업이 있습니다. 일반적으로 서블릿 스프링 애플리케이션에서 web.xml에 정의 된 모든 애플리케이션 컨텍스트는 웹 애플리케이션 컨텍스트이며 이는 루트 웹 애플리케이션 컨텍스트와 서블릿의 애플리케이션 컨텍스트 둘 다로 간다.

또한 웹 응용 프로그램 컨텍스트 기능에 따라 응용 프로그램을 테스트하기가 약간 어려워 질 수 있으며 테스트를 위해 MockServletContext 클래스 를 사용해야 할 수도 있습니다 .

서블릿과 루트 컨텍스트의 차이점 Spring을 사용하면 다중 레벨 애플리케이션 컨텍스트 계층을 빌드 할 수 있으므로 현재 애플리케이션 컨텍스트에없는 경우 필요한 Bean을 상위 컨텍스트에서 가져옵니다. 웹 앱에는 기본적으로 루트 및 서블릿 컨텍스트의 두 가지 계층 레벨이 있습니다 서블릿 및 루트 컨텍스트.

이를 통해 일부 서비스를 전체 애플리케이션의 싱글 톤 (Spring Security Bean 및 기본 데이터베이스 액세스 서비스가 여기에 상주 함)으로 실행하고 다른 서비스를 해당 서블릿에서 분리 된 서비스로 실행하여 Bean 간의 이름 충돌을 피할 수 있습니다. 예를 들어 한 서블릿 컨텍스트는 웹 페이지를 제공하고 다른 서블릿 컨텍스트는 상태 비 저장 웹 서비스를 구현합니다.

이 두 레벨 분리는 스프링 서블릿 클래스를 사용할 때 즉시 사용 가능합니다. 루트 애플리케이션 컨텍스트를 구성하려면 web.xml에서 context-param 태그 를 사용해야합니다.

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /WEB-INF/root-context.xml
            /WEB-INF/applicationContext-security.xml
    </param-value>
</context-param>

(루트 애플리케이션 컨텍스트는 web.xml에 선언 ContextLoaderListener에 의해 작성됩니다.

<listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener> 

) 및 서블릿 애플리케이션 컨텍스트의 서블릿 태그

<servlet>
   <servlet-name>myservlet</servlet-name>
   <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
   <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>app-servlet.xml</param-value>
   </init-param>
</servlet>

init-param이 생략되면, 스프링은이 예제에서 myservlet-servlet.xml을 사용한다는 점에 유의하십시오.

Spring Framework에서 applicationContext.xml과 spring-servlet.xml의 차이점 도 참조하십시오.


Going back to Servlet days, web.xml can have only one <context-param>, so only one context object gets created when server loads an application and the data in that context is shared among all resources (Ex: Servlets and JSPs). It is same as having Database driver name in the context, which will not change. In similar way, when we declare contextConfigLocation param in <contex-param> Spring creates one Application Context object.

 <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>com.myApp.ApplicationContext</param-value>
 </context-param>

You can have multiple Servlets in an application. For example you might want to handle /secure/* requests in one way and /non-seucre/* in other way. For each of these Servlets you can have a context object, which is a WebApplicationContext.

<servlet>
    <servlet-name>SecureSpringDispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextClass</param-name>
        <param-value>com.myapp.secure.SecureContext</param-value>
    </init-param>
</servlet>
<servlet-mapping>
    <servlet-name>SecureSpringDispatcher</servlet-name>
    <url-pattern>/secure/*</url-pattern>
</servlet-mapping>
<servlet>
    <servlet-name>NonSecureSpringDispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextClass</param-name>
        <param-value>com.myapp.non-secure.NonSecureContext</param-value>
    </init-param>
</servlet>
<servlet-mapping>
    <servlet-name>NonSecureSpringDispatcher</servlet-name>
    <url-pattern>/non-secure/*</url-patten>
</servlet-mapping>

The accepted answer is through but there is official explanation on this:

The WebApplicationContext is an extension of the plain ApplicationContext that has some extra features necessary for web applications. It differs from a normal ApplicationContext in that it is capable of resolving themes (see Using themes), and that it knows which Servlet it is associated with (by having a link to the ServletContext). The WebApplicationContext is bound in the ServletContext, and by using static methods on the RequestContextUtils class you can always look up the WebApplicationContext if you need access to it.

Cited from Spring web framework reference

By the way servlet and root context are both webApplicationContext:

Spring Web MVC의 일반적인 컨텍스트 계층


ApplicationContext (Root Application Context) : Every Spring MVC web application has an applicationContext.xml file which is configured as the root of context configuration. Spring loads this file and creates an applicationContext for the entire application. This file is loaded by the ContextLoaderListener which is configured as a context param in web.xml file. And there will be only one applicationContext per web application.

WebApplicationContext : WebApplicationContext is a web aware application context i.e. it has servlet context information. A single web application can have multiple WebApplicationContext and each Dispatcher servlet (which is the front controller of Spring MVC architecture) is associated with a WebApplicationContext. The webApplicationContext configuration file *-servlet.xml is specific to a DispatcherServlet. And since a web application can have more than one dispatcher servlet configured to serve multiple requests, there can be more than one webApplicationContext file per web application.


Web application context, specified by the WebApplicationContext interface, is a Spring application context for a web applications. It has all the properties of a regular Spring application context, given that the WebApplicationContext interface extends the ApplicationContext interface, and add a method for retrieving the standard Servlet API ServletContext for the web application.

In addition to the standard Spring bean scopes singleton and prototype, there are three additional scopes available in a web application context:

  • request - scopes a single bean definition to the lifecycle of a single HTTP request; that is, each HTTP request has its own instance of a bean created off the back of a single bean definition
  • session - scopes a single bean definition to the lifecycle of an HTTP Session
  • application - scopes a single bean definition to the lifecycle of a ServletContext

참고 URL : https://stackoverflow.com/questions/11708967/what-is-the-difference-between-applicationcontext-and-webapplicationcontext-in-s

반응형