您的位置:首页 > 编程语言 > Java开发

Spring之ContextLoaderListener的作用 分享

2013-06-28 22:54 381 查看
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可以由ContextLoaderListener和ContextLoaderServlet生成。如果查看ContextLoaderServlet的API,可以看到它也关联了ContextLoader这个类而且它继承了HttpServlet

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

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

第三段,讲如何部署applicationContext的xml文件,如果在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.xml,applicationContext-action.xml,applicationContext-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代码



<listener>

<listener-class>

org.springframework.web.context.ContextLoaderListener

</listener-class>

</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代码



<context-param>

<param-name>

contextConfigLocation

</param-name>

<param-value>

classpath*:applicationContext.xml

</param-value>

</context-param>

常见可能是这样:/WEB-INF/classes/applicationContext-*.xml

总之它是使用spring必须要配置的元素,一定不要少了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: