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.
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
) and servlet tag for the sevlet application contexts
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.
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.
相关文章推荐
- Spring WebApplicationInitializer and ApplicationContextInitializer confusion
- configureAndRefreshWebApplicationContext
- What is a Context in Java and ApplicationContext vs WebApplicationContext
- [springframework] - DispatcherServlet and WebApplicationContext
- Error creating bean ...No WebApplicationContext found
- No WebApplicationContext found: no ContextLoaderListener registered?
- 从源码看spring applicationContext在web容器中加载过程
- How To Install and Use the Web Application Stress (WAS) Tool
- Web Application Architectures: Simple 2 Layer, Standard 3 Layer and Distributed 3 Layer
- Application Request Routing and the IIS 7.0 Web Management Service
- How to create and delete a SharePoint site by web application
- Step-by-Step:Installation and Configuration of a secure web application using MySQL,OpenSA Web Server, Tomcat and OpenSSL,on a N
- jsp文件得到WebApplicationContext
- No WebApplicationContext found: no ContextLoaderListener registered的解决方法
- Using .Net, Flex, and Red5 to create a flash web application
- Improving Web Application Security: Threats and Countermeasures
- ASP.NET Ver 1.1 Web Application and Windows Authentication – a Case Study
- Grid Application Development: e-Science Application using GT-4 and Web Services (2)
- Spring WebApplicationContext
- No WebApplicationContext found: no ContextLoaderListener registered异常