ApplicationContext 、WebApplicationContext、ContextLoaderListener

一般在用spring管理时,web.xml是这样配置的

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         version="2.5">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!--
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring-servlet.xml</param-value>
        </init-param>
        -->
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

一直不怎么明白他们之间的关系,后来去查了查,觉得有所感悟
文章一
(最后会贴上来)
stackoverflow上相关问题

自己理解如下:
1、 首先,一个web应用拥有一个单例的applicationContext
2、 而这个单例的applicationContext由contextLoaderListener负责初始化
3、 当然,程序在加载时,会默认去WEB-INF下去查找名为applicationContext.xml的配置文件并加载
4、第三点解释了为什么不现实的配置web.xml中的contextLoaderListenner,而只配置DispacherServlet,就能跑起来的原因
5、若希望自定义路径或者applicationContext的配置文件名字,那么,这时候就需要显示的配置contextLoaderListener,因为它会去<context-param> 节点中查找用户自定义的 <param-name>contextConfigLocation</param-name><param-value>/WEB-INF/applicationContext.xml</param-value> 用这个配置文件去初始化整个web应用单例的applicationContext。
6、而一个applicationContext可以对应多个webApplicationContext。
7、webApplicationContext需要在Servlet中声明,默认的配置文件是WEB-INF下的xxx-servlet.xml,xxx表示servlet节点中自定义的servlet-name
8、若希望自定义webApplicationContext配置文件的名字或路径,可以在<servlet> 节点中加入 <ini-param> 节点,具体见上web.xml的例子中注释掉的部分

最后,上文给的文章一连接的内容如下:

Understanding the difference between ApplicationContext and WebApplicationContext for Spring developers is important. There are various context names available in the spring framework that are used based on the type of application. The most common ambiguity due to the two different context configuration files used for configuring the spring applications. The question comes to developers mind is that why would we need two different context configurations files and when these files are loaded by the spring container?. This tutorial summarizes some of the important facts about both the context configurations files.
Spring application has two types of context configuration files for Spring MVC module:
ApplicationContext (default name for this file is applicationContext.xml)
WebApplicationContext (default name for this file is xxx-servlet.xml where xxx is the DispatcherServlet name in web.xml)

ApplicationContext

  • applicationContext.xml is the root context configuration for every web application.
  • Spring loads applicationContext.xml file and creates the ApplicationContext for the whole application.
  • There will be only one application context per web application.
  • If you are not explicitly declaring the context configuration file - name in web.xml using the contextConfigLocation param, Spring will search for the applicationContext.xml under WEB-INF folder and throw FileNotFoundException if it could not find this file.

WebApplicationContext

  • Apart from ApplicationContext, there can be multiple WebApplicationContext in a single web application.
  • In simple words, each DispatcherServlet associated with single WebApplicationContext.
  • xxx-servlet.xml file is specific to the DispatcherServlet and a web application can have more than one DispatcherServlet configured to handle the requests.
  • In such scenarios, each DispatcherServlet would have a separate xxx-servlet.xml configured. But, applicationContext.xml will be common for all the servlet configuration files.
  • Spring will by default load file named “xxx-servlet.xml” from your webapps WEB-INF folder where xxx is the servlet name in web.xml.
  • If you want to change the name of that file name or change the location, add initi-param with contextConfigLocation as param name.

ContextLoaderListener

  • Performs the actual initialization work for the root application context.
  • Reads a “contextConfigLocation” context-param and passes its value to the context instance, parsing it into potentially multiple file paths which can be separated by any number of commas and spaces, e.g. “WEB-INF/applicationContext1.xml, WEB-INF/applicationContext2.xml”.
  • ContextLoaderListener is optional. Just to make a point here: you can boot up a Spring application without ever configuring ContextLoaderListener, just a basic minimum web.xml with DispatcherServlet.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值