首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >关于Spring web.xml <context-param>和<listener>标签的一些信息(参考Hello World示例)

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

Stack Overflow用户
提问于 2012-12-01 02:06:08
回答 1查看 24.4K关注 0票数 15

我是Spring MVC World的新手。今天我正在学习由STS生成的简单的"Hello World“示例:文件-> Spring模板项目-> Spring MVC项目

在web.xml中,我有DispatcherServlet的声明和it...Up处理的请求映射,在这里一切都好

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

代码语言:javascript
复制
<!-- 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>

在阅读Spring文档中关于ContextLoaderListener的内容时,我读到这个类执行侦听器的引导来启动Spring根WebApplicationContext but...what,这到底是什么意思?

另一个疑问是,我传递给context...what的contextConfigLocation参数到底是什么?打开/WEB-INF/spring/root-context.xml文件,它似乎没有包含任何configuration...is,它是由我的模板项目创建过程自动创建的一个无效配置文件吗?在Spring项目中应该包含什么样的配置?

我认为在这个Hello World项目中没有使用和标签,因为如果我删除了这些标签,程序仍然会运行well....is,对吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-12-01 02:18:58

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

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

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

票数 35
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13650695

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档