Spring 配置的2个常见问题
2015-07-09 14:28
197 查看
在搭建spring的开发环境时经常会出现一些问题。
一、
严重: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
这个异常是因为 在导入jar包时 缺少2个jar包 分别是 commons-logging-1.1.1.jar和commons-logging.jar 。因为spring的日志是基于log4j的。
二、
applicationContext.xml 文件存放位置不同 web.xml 的配置也会稍有不同。一般applicationContext.xml的默认路径为WEB-INFO目录下。
当applicationContext.xml 在WEB-INFO目录下时,web.xml 只需要配置Spring监听即可。
web.xml
而 当applicationContext.xml不在WEB-INFO目录下时。比如在src目录下时(src也是一个经常存放applicationContext.xml的目录)在web.xml配置监听后,还需要指定一下applicationContext.xml所在的目录。web.xml 要加入下列代码
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/applicationContext.xml</param-value>
</context-param>
因为ContextLoaderListener监听在服务器启动时会先到默认路径WEB-INFO下找,没有在获取<context-param>的配置路径。
applicationContext.xml在src下时的web.xml
当ContextLoaderListener无法找到applicationContext.xml时会报异常
严重: Context initialization failed
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]
一、
严重: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
这个异常是因为 在导入jar包时 缺少2个jar包 分别是 commons-logging-1.1.1.jar和commons-logging.jar 。因为spring的日志是基于log4j的。
二、
applicationContext.xml 文件存放位置不同 web.xml 的配置也会稍有不同。一般applicationContext.xml的默认路径为WEB-INFO目录下。
当applicationContext.xml 在WEB-INFO目录下时,web.xml 只需要配置Spring监听即可。
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>springStudy</display-name> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> </web-app>
而 当applicationContext.xml不在WEB-INFO目录下时。比如在src目录下时(src也是一个经常存放applicationContext.xml的目录)在web.xml配置监听后,还需要指定一下applicationContext.xml所在的目录。web.xml 要加入下列代码
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/applicationContext.xml</param-value>
</context-param>
因为ContextLoaderListener监听在服务器启动时会先到默认路径WEB-INFO下找,没有在获取<context-param>的配置路径。
applicationContext.xml在src下时的web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>springStudy</display-name> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/classes/applicationContext.xml</param-value> </context-param> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> </web-app>
当ContextLoaderListener无法找到applicationContext.xml时会报异常
严重: Context initialization failed
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]
相关文章推荐
- Java MessageFormat.format的用法
- java设置session过期时间的实现方法
- 修改Eclipse中buildpath中的Classpath Variables M2_REPO
- java--反射
- J2EE框架(Struts&Hibernate&Spring)的理解
- java--正则表达式
- Java异常与异常处理小记
- Java中的集合排序
- java--线程
- Eclipse背景颜色设置 保护眼睛从这开始
- java--设计模式
- java--关键字
- java--string
- java--IO 使用带缓冲功能的字节流复制文件
- JAVA--集合
- java--内部类的总结
- java--异常及异常的处理机制
- JAVA--IO流(三)
- JAVA--IO流(二)
- java--线程的生命周期