您的位置:首页 > 移动开发 > WebAPP

applicationcontext and webapplicationcontext

2014-08-15 00:00 323 查看
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.
public interface WebApplicationContext extends ApplicationContext { ServletContext getServletContext(); }

Beans, instantiated in WebApplicationContext will also be able to use ServletContext if they implement ServletContextAware interface
package org.springframework.web.context; public interface ServletContextAware extends Aware { void setServletContext(ServletContext servletContext); }

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
<context-param> <param-name>contextConfigLocation</param-name> <param-value> /WEB-INF/root-context.xml /WEB-INF/applicationContext-security.xml </param-value> </context-param>

(the root application context is created by ContextLoaderListener which is declared in web.xml
<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>

) and servlet tag for the sevlet application contexts
<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>

please note that if init-param will be omitted, then spring will use myservlet-servlet.xml in this example.
See also: difference between applicationContext and spring-servlet.xml in spring
Spring lets you define multiple contexts in a parent-child hierarchy.
The applicationContext.xml defines the beans for the "root webapp context", i.e. the context associated with the webapp.
The spring-servlet.xml (or whatever else you call it) defines the beans for one servlet's app context. There can be many of these in a webapp, one per Spring servlet (e.g. spring1-servlet.xmlfor servlet spring1, spring2-servlet.xml for servlet spring2).
Beans in spring-servlet.xml can reference beans in applicationContext.xml, but not vice versa.
All Spring MVC controllers must go in the spring-servlet.xml context.
In most simple cases, the applicationContext.xml context is unnecessary. It is generally used to contain beans that are shared between all servlets in a webapp. If you only have one servlet, then there's not really much point, unless you have a specific use for it.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: