Spring之ContextLoaderListener的作用 分享

Spring ContextLoaderListener在Web应用中的作用与配置
本文详细介绍了Spring的ContextLoaderListener的作用,包括其如何在启动Web容器时自动装配Spring applicationContext.xml配置信息,以及如何通过web.xml配置文件来部署applicationContext的xml文件,包括默认路径、自定义文件名以及处理多个配置文件的方法。

Spring org.springframework.web.context.ContextLoaderListener

public class ContextLoaderListener

extends Object implements ServletContextListener

作用:在启动Web容器时,自动装配SpringapplicationContext.xml的配置信息。

因为它实现了ServletContextListener这个接口,在web.xml配置这个监听器,启动容器时,就会默认执行它实现的方法。在ContextLoaderListener中关联了ContextLoader这个类,所以整个加载配置过程由ContextLoader来完成。

看看ContextLoader的API说明

第一段说明ContextLoader可以由ContextLoaderListenerContextLoaderServlet生成。如果查看ContextLoaderServletAPI,可以看到它也关联了ContextLoader这个类而且它继承了HttpServlet

第二段,ContextLoader创建的是XmlWebApplicationContext这样一个类,它实现的接口是WebApplicationContext->ConfigurableWebApplicationContext->ApplicationContext->

BeanFactory这样一来spring中的所有bean都由这个类来创建

第三段,讲如何部署applicationContextxml文件,如果在web.xml中不写任何参数配置信息,默认的路径是"/WEB-INF/applicationContext.xml,在WEB-INF目录下创建的xml文件的名称必须是applicationContext.xml。如果是要自定义文件名可以在web.xml里加入contextConfigLocation这个context参数:

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/classes/applicationContext-*.xml
</param-value>
</context-param>

<param-value> </param-value>里指定相应的xml文件名,如果有多个xml文件,可以写在一起并一“,”号分隔。上面的applicationContext-*.xml采用通配符,比如这那个目录下有applicationContext-ibatis-base.xmlapplicationContext-action.xmlapplicationContext-ibatis-dao.xml等文件,都会一同被载入。

-----------------------------------------------------------------------------------------

例子,在web.xml文件中加入下面代码

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

如果applicationContext.xml文件没有在/WEB-INF/下,或文件名不一致,或存在多个Spring配置文件,在web.xml文件中根据下面代码修改

  <context-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>classpath*:applicationContext-*.xml,/WEB-INF/applicationContext.xml,/WEB-INF/classes/applicationContext-*.xml
</param-value>
  </context-param>

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
1 Spring之ContextLoaderListener的作用 
http://lei2006.blog.sohu.com/116206469.html  

使用spring除了添加必要的jar包,另外在web.xml一定要加上启动spring的监听器,这样配置在xml文件中的bean才会初始化 

如你在web.xml这样作了配置:(web.xml 2.4) 
Java代码   收藏代码
  1. <listener>  
  2.         <listener-class>  
  3.             org.springframework.web.context.ContextLoaderListener  
  4.         </listener-class>  
  5.     </listener>     

它会默认查找位于:WEB-INF/下的是否有一个文件名称为:applicationContext.xml 
如果没有就会报错: 
严重: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener 
org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from ServletContext resource [/WEB-INF/applicationContext.xml]; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/applicationContext.xml] 
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:341)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:174)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:209)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:180)
at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:125)
at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:94)
at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:131)
at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:509)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:427) 
at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:294) 
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:215) 
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:47) 
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4521) 
at org.apache.catalina.core.StandardContext$1.call(StandardContext.java:5004) 
at org.apache.catalina.core.StandardContext$1.call(StandardContext.java:4999) 
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303) 
at java.util.concurrent.FutureTask.run(FutureTask.java:138) 
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) 
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) 
at java.lang.Thread.run(Thread.java:619) 
Caused by: java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/applicationContext.xml] 
at org.springframework.web.context.support.ServletContextResource.getInputStream(ServletContextResource.java:117)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:328)
... 20 more
 
但在很多项目中可能会把配置文件集中管理,常见位置在:classpath下面,这样的话,你要在web.xml中在配置另外一个节点名称: 
Java代码   收藏代码
  1. <context-param>  
  2.         <param-name>  
  3.             contextConfigLocation  
  4.         </param-name>  
  5.         <param-value>  
  6.             classpath*:applicationContext.xml  
  7.         </param-value>  
  8.     </context-param>  

常见可能是这样: /WEB-INF/classes/applicationContext-*.xml  
总之它是使用spring必须要配置的元素,一定不要少了
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值