ApplicationContext and ServletContext

做~自己de王妃 提交于 2019-12-29 11:35:31

问题


I get confused between the two ApplicationContext and ServletContext when it comes to Spring MVC Application. I know that There is just only One ApplicationContext per Spring Web Application and there is also just only One ServletContext per web application. To initiate the value for both ApplicationContext and ServletContext, in web.xml, we will add something in context-param tag.

That is the point that makes me confused. What are the differences between these two (i know ApplicationContext has some methods to work with beans)? and When we would use ApplicationContext and When we would use ServletContext?


回答1:


Servlet Context:

It is initialized when an Servlet application is deployed. Servlet Context holds all the configurations (init-param, context-params etc) of the whole servlet application.

Application Context:

It is a Spring specific thing. It is initialized by Spring. It holds all the bean definitions and life-cycle of the of the beans that is defined inside the spring configuration files. Servlet-Context has no idea about this things.

There are two types of contexts in Spring parent and child.

Spring Parent Context (Application Context / Root Context )

  <listener>
        <listener-lass> 
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
  </listener>
  <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/service-context.xml,
            /WEB-INF/dao-context.xml,
            /WEB-INF/was-context.xml,
            /WEB-INF/jndi-context.xml,
            /WEB-INF/json-context.xml
        </param-value>
  </context-param>

role-purpose-of-contextloaderlistener-in-spring
Spring-ContextLoaderListener-And-DispatcherServlet-Concepts
When spring container start ups, it reads all the bean definitions from the configuration files and creates beans objects and manages life cycle of the beans objects. This configuration is totally optional.

DispatcherServlet vs ContextLoaderListener
/declaring-spring-bean-in-parent-context-vs-child-context

Spring Child Context ( WebApplicationContext / Child Context )

<servlet>
    <servlet-name>myWebApplication</servlet-name>
    <servlet-class>
         org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>myWebApplication</servlet-name>
    <url-pattern>/app/*</url-pattern>
</servlet-mapping>

When spring web application starts it will look for spring bean configuration file myWebApplication-servlet.xml. It will read all the bean definitions and create and manages the bean objects life cycle. If parent spring context is available it will merge the child spring context with the parent spring context. If there is no Spring parent context available the application will only have the child spring context.




回答2:


They are separate things. Every Java web applications based on Servlet technology will have a servlet context, whether it's a spring application or not. In contrast, the ApplicationContext is a Spring thing; in very simple terms, it's a container to hold Spring beans.

To initiate the value for both ApplicationContext and ServletContext, in web.xml, we will add something in context-param tag.

It would help if you quote an example for this, because, as far as I know, context-param is used for ServletContext, and not ApplicationContext.

Update:

You can use a context-param to provide the locations of the root application context configuration files, as below.

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /WEB-INF/root-context.xml
        /WEB-INF/applicationContext-security.xml
    </param-value>
</context-param>



回答3:


ApplicationContext is Spring's container.

It's used to wire the configurations from Spring beans together and use them for the application.

Use ApplicationContext if you want to retrieve the information of Spring beans.

Use ServletContext if you want to get/set attribute those shared to all Servlet.




回答4:


In Spring, to read a specific initialisation configuration file, we use the context-param with the predefined name called contextConfigLocation.

<context-param>
  <description>WebFlow context configuration</description>
  <param-name>contextConfigLocation</param-name>
  <param-value>/WEB-INF/test-context.xml</param-value>
</context-param> 

But in case of Plain J2EE web application without including any frameworks, the context-param can read from any where in the application i.e. any servlet, filter.

The difference between ApplicationContext and ServletContext, sanjay explained




回答5:


ServletContext is distinguished from it's 'enclosing' ApplicationContext. The Java doc says the below for ServletContext

There is one [servlet] 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.)

Since there can be more than one "web application" under the same AppBase, each with their own DocBase, WEB-INF/web.xml etc., there is definitely a common environment/context that is shared by all "web applications", which is being referred to as the ApplicationContext. In case of JSF, PortletContext is the counter part of ServletContext and the ApplicationContext is referred to as the ExternalContext.



来源:https://stackoverflow.com/questions/31931848/applicationcontext-and-servletcontext

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!