16
15
|
What is the difference between Application Context and Web Application Context?
I am aware that
WebApplicationContext is used for Spring MVC architecture oriented applications?
I want to know what is the use of
ApplicationContext in MVC applications? And what kind of beans are defined in ApplicationContext ? | |||
36
|
Web Application context extended Application Context which is designed for work with the standardjavax.servlet.ServletContext so it's able to communicate with the container.
Beans, instantiated in WebApplicationContext will also be able to use ServletContext if they implement ServletContextAware interface
There many things possible to do with the ServletContext instance, for example accessing WEB-INF resources(xml configs and etc.) by calling the getResourceAsStream() method. Typically all application contexts defined in web.xml in a servlet Spring application are Web Application contexts, this goes both to the root webapp context and the servlet's app context.
Also, depending on web application context capabilities may make your application a little harder to test, and you may need to use MockServletContext class for testing.
Difference between servlet and root context Spring allows you to build multilevel application context hierarchies, so the required bean will be fetched from the parent context if it's not present in the current aplication context. In web apps as default there are two hierarchy levels, root and servlet contexts:
such thing allows you to run some services as the singletons for the entire application(Spring Security beans and basic database access services typically reside here) and another as separated services in the corresponding servlets to avoid name clashes between beans. For example one servlet context will be serving the web pages and another will be implementing a stateless web service.
This two level separation comes out of the box when you use the spring servlet classes: to configure the root application context you should use context-param tag in your web.xml
|
(the root application context is created by ContextLoaderListener which is declared in web.xml
org.springframework.web.context.ContextLoaderListener
) and servlet tag for the sevlet application contexts
myservlet
please note that if init-param will be omitted, then spring will use myservlet-servlet.xml in this example.
web.xml
file; this question is asking about some Spring classes. – Raedwald Jul 5 at 10:41web.xml
but it's talking about the Spring XML bean configuration variants ofApplicationContext
andWebApplicationContext
. All bean definitions inapplicationContext.xml
will be available in theApplicationContext
while all bean definitions in*-servlet.xml
will be available in aWebApplicationContext
. – Dimitri M Jul 5 at 12:39