public interface ServletContext
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 구현
Class ApplicationContextFacade
- java.lang.Object
- org.apache.catalina.core.ApplicationContextFacade
- All Implemented Interfaces:
- ServletContext
public class ApplicationContextFacade extends Object implements ServletContext
Facade object which masks the internalApplicationContext
object from the web application.
Class ApplicationContext
- java.lang.Object
- org.apache.catalina.core.ApplicationContext
- All Implemented Interfaces:
- ServletContext
- Direct Known Subclasses:
- ReplicatedContext.ReplApplContext
public class ApplicationContext extends Object implements ServletContext
Standard implementation ofServletContext
that represents a web application's execution environment. An instance of this class is associated with each instance ofStandardContext
.
- HttpSession.getServletContext() 호출 시 ApplicationContextFacade를 얻을 수 있음. ApplicationContextFacade 생성자 호출 시 ApplicationContext를 인자로 넘김
- ApplicationContext.attributes(protected Map<String,Object> attributes = 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.)
Interface WebApplicationContext
- All Superinterfaces:
- ApplicationContext, ApplicationEventPublisher, BeanFactory, EnvironmentCapable, HierarchicalBeanFactory, ListableBeanFactory, MessageSource, ResourceLoader, ResourcePatternResolver
- All Known Subinterfaces:
- ConfigurablePortletApplicationContext, ConfigurableWebApplicationContext
- All Known Implementing Classes:
- AbstractRefreshablePortletApplicationContext, AbstractRefreshableWebApplicationContext, AnnotationConfigWebApplicationContext, GenericWebApplicationContext, GroovyWebApplicationContext,StaticPortletApplicationContext, StaticWebApplicationContext, XmlPortletApplicationContext, XmlWebApplicationContext
public interface WebApplicationContext extends ApplicationContext
Interface to provide configuration for a web application. This is read-only while the application is running, but may be reloaded if the implementation supports this.This interface adds a
getServletContext()
method to the generic ApplicationContext interface, and defines a well-known application attribute name that the root context must be bound to in the bootstrap process.Like generic application contexts, web application contexts are hierarchical. There is a single root context per application, while each servlet in the application (including a dispatcher servlet in the MVC framework) has its own child context.
In addition to standard application context lifecycle capabilities, WebApplicationContext implementations need to detect
ServletContextAware
beans and invoke thesetServletContext
method accordingly.- Since:
- January 19, 2001
- Author:
- Rod Johnson, Juergen Hoeller
- See Also:
ServletContextAware.setServletContext(javax.servlet.ServletContext)
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");
'Framework & Platform > Spring' 카테고리의 다른 글
ThreadPoolTaskScheduler initialize method (0) | 2020.03.29 |
---|---|
spring - 4.2.0.RELEASE AsyncAnnotationBeanPostProcessor NoUniqueBeanDefinitionException (0) | 2015.08.04 |
spring - headers="Accept=*/*" (0) | 2014.12.26 |
spring - Get current ApplicationContext (0) | 2014.10.06 |
spring - restTemplate example (0) | 2014.09.12 |