码迷,mamicode.com
首页 > 编程语言 > 详细

spring配置文件默认名称及位置,ContextLoaderListener监听器作用

时间:2019-10-13 10:59:37      阅读:212      评论:0      收藏:0      [点我收藏+]

标签:服务   style   参数   加载   控制   初始   应用   color   方法   

spring在web.xml中的配置

由于spring需要启动容器才能为其他框架提供服务,而web应用程序的入口是由web服务器控制的,因此无法在main()方法中通过创建ClassPathXmlApplicationContext对象来启动spring容器。spring提供了org.springframework.web.context.ContextLoaderListener(spring-web依赖下)这个监听器类来解决这个问题。该监听器实现了ServletContextListener接口,可以在web容器启动的时候初始化spring容器。当然,前提是需要在web.xml中配置好这个监听器。如下

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

如果没有指定contextConfigLoaction参数,ContextLoaderListener默认会去查找/WEB-INF/applicationContext.xml。换句话说,如果我们将spring的配置文件命名为applicationContext.xml并存放在WEB-INF目录下,即使不指定contextConfigLocation参数,也能加载配置文件。否则就需要配置如下

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext-jdbc.xml</param-value>
</context-param>

 

spring配置文件默认名称及位置,ContextLoaderListener监听器作用

标签:服务   style   参数   加载   控制   初始   应用   color   方法   

原文地址:https://www.cnblogs.com/yanguobin/p/11665094.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!