有关Spring web.xml <context-param> 和标签的一些信息<listener>(参考Hello World示例)



我对Spring MVC世界还是个新手。今天我在学习STS生成的简单的"Hello World"示例:File--->Spring Template Project--->Spring MVC Project

在web.xml中,我有DispatcherServlet的声明和它处理的请求映射

在web.xml中,我还有这部分代码:

<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

在阅读关于ContextLoaderListener的Spring文档时,我读到这个类执行侦听器的引导程序来启动Spring的根WebApplicationContext,但是。。。它到底是什么意思?

另一个疑问是我传递给上下文的contextConfigLocation参数。。。究竟是什么?打开/WEB-INF/spring/root-context.xml文件时,它似乎不包含任何配置。。。它是一个由我的模板项目创建过程自动创建的void配置文件吗?Spring项目中应该包含什么样的配置?

我认为这个Hello World项目中没有使用tath和这些标签,因为如果我删除这些标签,项目仍然运行良好。。。。对吗?

ContextLoaderListener是一个启动Spring容器的类。基本上,每个Spring应用程序都由几个bean和连线组成(对哪些bean相互依赖的声明性描述)。这个描述在历史上是用XML编写的(现在我们有注释、Java配置、CLASSPATH扫描等)

没有Spring容器,您的bean只是Java类,而Spring配置文件只是一个无用的XML文档。ContextLoaderListener读取该文件,找到您的类,实例化它们并连接。然后把你所有的豆子都放在一个容器里。

此外,ContextLoaderListener在应用程序关闭时关闭上下文(如果需要清理,则关闭所有bean)。