Framework & Platform/Spring

spring - ServletContext

linuxism 2015. 7. 16. 11:11


public interface 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. (A "web application" is a collection of servlets and content installed under a specific subset of the server's URL namespace such as /catalog and possibly installed via a .war file.)

In the case of a web application marked "distributed" in its deployment descriptor, there will be one context instance for each virtual machine. In this situation, the context cannot be used as a location to share global information (because the information won't be truly global). Use an external resource like a database instead.

The ServletContext object is contained within the ServletConfig object, which the Web server provides the servlet when the servlet is initialized.



source - http://docs.oracle.com/javaee/7/api/javax/servlet/ServletContext.html




* tomcat 구현 

org.apache.catalina.core

Class ApplicationContextFacade

  • All Implemented Interfaces:
    ServletContext


    public class ApplicationContextFacade
    extends Object
    implements ServletContext
    Facade object which masks the internal ApplicationContext object from the web application.

org.apache.catalina.core

Class ApplicationContext

  • All Implemented Interfaces:
    ServletContext
    Direct Known Subclasses:
    ReplicatedContext.ReplApplContext


    public class ApplicationContext
    extends Object
    implements ServletContext
    Standard implementation of ServletContext that represents a web application's execution environment. An instance of this class is associated with each instance of StandardContext.

- HttpSession.getServletContext() 호출 시 ApplicationContextFacade를 얻을 수 있음. ApplicationContextFacade 생성자 호출 시 ApplicationContext를 인자로 넘김

ApplicationContext.attributes(protected Map<String,Objectattributes = new ConcurrentHashMap<>())에서 web.xml 정보 확인할 수 있음


source - https://tomcat.apache.org/tomcat-8.0-doc/api/org/apache/catalina/core/ApplicationContextFacade.html

https://tomcat.apache.org/tomcat-8.0-doc/api/org/apache/catalina/core/ApplicationContext.html



* spring에서 WebApplicationContext를 얻기(Find a custom WebApplicationContext for this web app.)

org.springframework.web.context

Interface WebApplicationContext

source - http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/context/WebApplicationContext.html


- org.springframework.web.context.support.WebApplicationContextUtils.getWebApplicationContext(ServletContext sc, String attrName) 호출

- sc는 ApplicationContextFacade에 해당하고 attrName은 web.xml에서 설정한 DispatcherServlet 서블릿 이름

- web.xml 설정을 javaConfig(WebApplicationInitializer)로 대체한 경우 ContextLoaderListener으로 등록되어 WebApplicationContextUtils.getWebApplicationContext(ServletContext sc)을 호출해야하며 이 메서드는 attrName을 org.springframework.web.context.WebApplicationContext.ROOT으로 하여 WebApplicationContextUtils.getWebApplicationContext(ServletContext sc, String attrName)를 호출함

- 소스코드 

Object attr = sc.getAttribute(attrName);

return (WebApplicationContext) attr;

- 이후 WebApplicationContext를 통해서 bean을 얻을 수 있음 

UserService userService = (UserService) webApplicationContext.getBean("userService");